@@ -2,7 +2,7 @@ import { NuxtConfig } from "@nuxt/schema"
2
2
import { Dependency } from "../../utils/addPackageDependency"
3
3
4
4
/**
5
- * PRISMA FILE CONTENTS, from: `npx prisma init`
5
+ * PRISMA FILE CONTENTS
6
6
*/
7
7
const prismaFile = `// This is your Prisma schema file,
8
8
// learn more about it in the docs: https://pris.ly/d/prisma-schema
@@ -29,6 +29,75 @@ const prismaEnvFile = `# Prisma
29
29
DATABASE_URL=file:./db.sqlite
30
30
`
31
31
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
+
32
101
/**
33
102
* NUXT AUTH FILE CONTENTS, from: sidebase.io/nuxt-auth/
34
103
*/
@@ -153,13 +222,10 @@ import type { H3Event } from 'h3'
153
222
*/
154
223
export async function createContext(event: H3Event) {
155
224
/**
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 }
157
228
* \`\`\`
158
- * const prisma = usePrisma(event)
159
- * return { prisma }
160
- * \`\`\`
161
- *
162
- * You can import \`usePrisma\` like this: \`import { usePrisma } from '@sidebase/nuxt-prisma'\`
163
229
*/
164
230
return {}
165
231
}
@@ -249,27 +315,34 @@ export const moduleConfigs: Record<Modules, ModuleConfig> = {
249
315
name : "@prisma/client" ,
250
316
version : "^4.8.0" ,
251
317
isDev : false
252
- } ,
253
- {
254
- name : "@sidebase/nuxt-prisma" ,
255
- version : "^0.1.2" ,
256
- isDev : false
257
318
}
258
319
] ,
259
- nuxtConfig : {
260
- extends : [ "@sidebase/nuxt-prisma" ] ,
261
- } ,
320
+ nuxtConfig : { } ,
262
321
files : [ {
263
322
path : ".env" ,
264
323
content : prismaEnvFile
265
324
} , {
266
325
path : "prisma/schema.prisma" ,
267
326
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
268
339
} ] ,
269
340
tasksPostInstall : [
270
341
"- [ ] 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>"
273
346
} ,
274
347
"auth" : {
275
348
humanReadableName : "nuxt-auth" ,
0 commit comments