Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

Getting data from Prisma directly in SvelteKit

$
0
0

So I'm trying to get data directly into SvelteKit from Prisma without using a API.I know that's not the way you would usually do it, and i've done it "normal" bevor, but I wanted to save some time this time.

What i have so far kind of works, but I have to reload the page for the data to be displayed the first time and it doesn't work when building and previewing it.Is there a better way of doing it?

Here is my code:

<script context="module" lang="ts">    import {PrismaClient, type User} from '@prisma/client'    if (typeof window === "undefined") {        var allUsers: User[] = []        const prisma = new PrismaClient()        async function main() {            allUsers = await prisma.user.findMany()            console.log(allUsers)        }        main()            .catch((e) => {                throw e            })            .finally(async () => {                await prisma.$disconnect()            })    }</script><div>    {#each allUsers as user}<p>{user.email}</p>    {/each}</div>

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>