@@ -2,7 +2,7 @@ import { NuxtConfig } from "@nuxt/schema"
22import { Dependency } from "../../utils/addPackageDependency"
33
44/**
5- * PRISMA FILE CONTENTS, from: `npx prisma init`
5+ * PRISMA FILE CONTENTS
66 */
77const prismaFile = `// This is your Prisma schema file,
88// learn more about it in the docs: https://pris.ly/d/prisma-schema
@@ -29,6 +29,75 @@ const prismaEnvFile = `# Prisma
2929DATABASE_URL=file:./db.sqlite
3030`
3131
32+ const prismaExampleEndpoint = `/**
33+ * Fetch all \`examples\` from the database. Run \`npx prisma generate\` and \`npx prisma db push\` for this to work.
34+ *
35+ * If you are using \`tRPC\` you can access the prisma-client by adding it to the context:
36+ * \`\`\`ts
37+ * export async function createContext(event: H3Event) {
38+ * return { prisma: event.context.prisma }
39+ * }
40+ *
41+ * export type Context = inferAsyncReturnType<typeof createContext>;
42+ * \`\`\`
43+ */
44+ export default defineEventHandler(event => event.context.prisma.example.findMany())
45+ `
46+
47+ const prismaServerMiddleware = `import { PrismaClient } from '@prisma/client'
48+
49+ let prisma: PrismaClient
50+
51+ declare module 'h3' {
52+ interface H3EventContext {
53+ prisma: PrismaClient
54+ }
55+ }
56+
57+ export default eventHandler((event) => {
58+ if (!prisma) {
59+ prisma = new PrismaClient()
60+ }
61+ event.context.prisma = prisma
62+ })
63+ `
64+
65+ const prismaUtils = `import { execSync } from 'child_process'
66+
67+ /**
68+ * Helper to reset the database via a programmatic prisma invocation. Helpful to add to \`beforeEach\` or \`beforeAll\` of your testing setup.
69+ *
70+ * WARNING: Never run this in production.
71+ *
72+ * Taken from https://github.com/prisma/prisma/issues/13549#issuecomment-1144883246
73+ *
74+ * @param databaseUrl Connection URL to database. Inferred from \`process.env.DATABASE_URL\` if not provided
75+ */
76+ export const resetDatabase = (databaseUrl?: string) => {
77+ const url = databaseUrl || process.env.DATABASE_URL
78+ if (!url) {
79+ throw new Error('Cannot reset database - connection string could not be inferred.')
80+ }
81+
82+ if (process.env.NODE_ENV === 'production') {
83+ throw new Error('This utility should not be called in production. It is meant for testing and development')
84+ }
85+
86+ execSync(\`cd \${process.cwd()} && DATABASE_URL=\${url} npx prisma db push --force-reset\`, { stdio: 'inherit' })
87+ }
88+ `
89+
90+ const prismaExamplePage = `<script setup lang="ts">
91+ const { data: examples } = useFetch('/api/examples')
92+ </script>
93+
94+ <template>
95+ <div>
96+ <p>Prisma ORM Data from the database, received {{ examples?.length || 0 }} records: <pre>{{ examples }}</pre></p>
97+ </div>
98+ </template>
99+ `
100+
32101/**
33102 * NUXT AUTH FILE CONTENTS, from: sidebase.io/nuxt-auth/
34103 */
@@ -153,13 +222,10 @@ import type { H3Event } from 'h3'
153222 */
154223export async function createContext(event: H3Event) {
155224 /**
156- * Add any trpc-request context here. E.g., you could add \`prisma\` like this if you've set it up:
225+ * Add any trpc-request context here. E.g., you could add \`prisma\` like this (if you've added it via sidebase):
226+ * \`\`\`ts
227+ * return { prisma: event.context.prisma }
157228 * \`\`\`
158- * const prisma = usePrisma(event)
159- * return { prisma }
160- * \`\`\`
161- *
162- * You can import \`usePrisma\` like this: \`import { usePrisma } from '@sidebase/nuxt-prisma'\`
163229 */
164230 return {}
165231}
@@ -249,27 +315,34 @@ export const moduleConfigs: Record<Modules, ModuleConfig> = {
249315 name : "@prisma/client" ,
250316 version : "^4.8.0" ,
251317 isDev : false
252- } ,
253- {
254- name : "@sidebase/nuxt-prisma" ,
255- version : "^0.1.2" ,
256- isDev : false
257318 }
258319 ] ,
259- nuxtConfig : {
260- extends : [ "@sidebase/nuxt-prisma" ] ,
261- } ,
320+ nuxtConfig : { } ,
262321 files : [ {
263322 path : ".env" ,
264323 content : prismaEnvFile
265324 } , {
266325 path : "prisma/schema.prisma" ,
267326 content : prismaFile
327+ } , {
328+ path : "server/api/examples.get.ts" ,
329+ content : prismaExampleEndpoint
330+ } , {
331+ path : "server/middleware/0.prisma.ts" ,
332+ content : prismaServerMiddleware
333+ } , {
334+ path : "prisma/utils.ts" ,
335+ content : prismaUtils
336+ } , {
337+ path : "pages/prisma.vue" ,
338+ content : prismaExamplePage
268339 } ] ,
269340 tasksPostInstall : [
270341 "- [ ] Prisma: Edit your `prisma/prisma.schema` to your liking" ,
271- "- [ ] Prisma: Run `npx prisma db push` to sync the schema to your database after changing it"
272- ]
342+ "- [ ] Prisma: Run `npx prisma db push` to sync the schema to your database after changing the schema" ,
343+ "- [ ] Prisma: Run `npx prisma generate` to re-generate the client after changing the schema"
344+ ] ,
345+ htmlForIndexVue : "<p>Checkout the Prisma ORM demo page here: <nuxt-link to=\"/prisma\" class=\"underline text-blue\">Click me to test the Prisma ORM setup!</nuxt-link></p>"
273346 } ,
274347 "auth" : {
275348 humanReadableName : "nuxt-auth" ,
0 commit comments