create-prisma 0.4.1 → 0.4.2-next.37.80.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -35
- package/dist/cli.mjs +1 -1
- package/dist/{create-H6Tk0JlE.mjs → create-CfH21J9-.mjs} +529 -914
- package/dist/index.d.mts +28 -25
- package/dist/index.mjs +3 -3
- package/package.json +2 -2
- package/templates/create/_shared/packages/db/prisma/seed.ts.hbs +43 -0
- package/templates/create/_shared/prisma/seed.ts.hbs +55 -0
- package/templates/create/astro/README.md.hbs +23 -12
- package/templates/create/astro/deno.json.hbs +1 -1
- package/templates/create/astro/prisma/contract.prisma.hbs +44 -0
- package/templates/create/astro/prisma/contract.ts.hbs +85 -0
- package/templates/create/astro/prisma-next.config.ts.hbs +13 -0
- package/templates/create/astro/src/lib/prisma.ts.hbs +49 -43
- package/templates/create/astro/src/pages/api/users.ts.hbs +5 -12
- package/templates/create/astro/src/pages/index.astro.hbs +19 -21
- package/templates/create/elysia/.yarnrc.yml.hbs +3 -0
- package/templates/create/elysia/README.md.hbs +27 -18
- package/templates/create/elysia/deno.json.hbs +1 -1
- package/templates/create/elysia/prisma/contract.prisma.hbs +44 -0
- package/templates/create/elysia/prisma/contract.ts.hbs +85 -0
- package/templates/create/elysia/prisma-next.config.ts.hbs +13 -0
- package/templates/create/elysia/src/index.ts.hbs +12 -7
- package/templates/create/elysia/src/lib/prisma.ts.hbs +49 -46
- package/templates/create/elysia/tsconfig.json +1 -0
- package/templates/create/hono/README.md.hbs +27 -18
- package/templates/create/hono/deno.json.hbs +1 -1
- package/templates/create/hono/prisma/contract.prisma.hbs +44 -0
- package/templates/create/hono/prisma/contract.ts.hbs +85 -0
- package/templates/create/hono/prisma-next.config.ts.hbs +13 -0
- package/templates/create/hono/src/index.ts.hbs +8 -6
- package/templates/create/hono/src/lib/prisma.ts.hbs +49 -46
- package/templates/create/hono/tsconfig.json +1 -0
- package/templates/create/nest/README.md.hbs +28 -18
- package/templates/create/nest/deno.json.hbs +1 -1
- package/templates/create/nest/prisma/contract.prisma.hbs +44 -0
- package/templates/create/nest/prisma/contract.ts.hbs +85 -0
- package/templates/create/nest/prisma-next.config.ts.hbs +13 -0
- package/templates/create/nest/src/lib/prisma.ts.hbs +49 -48
- package/templates/create/nest/src/prisma.service.ts.hbs +6 -5
- package/templates/create/nest/src/users.service.ts.hbs +1 -6
- package/templates/create/nest/tsconfig.json +1 -0
- package/templates/create/next/README.md.hbs +22 -11
- package/templates/create/next/deno.json.hbs +1 -1
- package/templates/create/next/prisma/contract.prisma.hbs +44 -0
- package/templates/create/next/prisma/contract.ts.hbs +85 -0
- package/templates/create/next/prisma-next.config.ts.hbs +13 -0
- package/templates/create/next/src/app/page.tsx.hbs +21 -26
- package/templates/create/next/src/lib/prisma.ts.hbs +49 -43
- package/templates/create/next/tsconfig.json +1 -0
- package/templates/create/nuxt/README.md.hbs +22 -11
- package/templates/create/nuxt/app/pages/index.vue.hbs +14 -12
- package/templates/create/nuxt/deno.json.hbs +1 -1
- package/templates/create/nuxt/nuxt.config.ts +16 -0
- package/templates/create/nuxt/prisma/contract.prisma.hbs +44 -0
- package/templates/create/nuxt/prisma/contract.ts.hbs +85 -0
- package/templates/create/nuxt/prisma-next.config.ts.hbs +13 -0
- package/templates/create/nuxt/server/api/users.get.ts.hbs +3 -9
- package/templates/create/nuxt/server/utils/prisma.ts.hbs +49 -43
- package/templates/create/svelte/README.md.hbs +23 -12
- package/templates/create/svelte/deno.json.hbs +1 -1
- package/templates/create/svelte/prisma/contract.prisma.hbs +44 -0
- package/templates/create/svelte/prisma/contract.ts.hbs +85 -0
- package/templates/create/svelte/prisma-next.config.ts.hbs +13 -0
- package/templates/create/svelte/src/lib/server/prisma.ts.hbs +50 -44
- package/templates/create/svelte/src/routes/+page.server.ts.hbs +3 -9
- package/templates/create/svelte/src/routes/+page.svelte.hbs +21 -16
- package/templates/create/tanstack-start/README.md.hbs +22 -11
- package/templates/create/tanstack-start/deno.json.hbs +1 -2
- package/templates/create/tanstack-start/prisma/contract.prisma.hbs +44 -0
- package/templates/create/tanstack-start/prisma/contract.ts.hbs +85 -0
- package/templates/create/tanstack-start/prisma-next.config.ts.hbs +13 -0
- package/templates/create/tanstack-start/src/lib/prisma.server.ts.hbs +49 -43
- package/templates/create/tanstack-start/src/routes/__root.tsx.hbs +2 -3
- package/templates/create/tanstack-start/src/routes/index.tsx.hbs +27 -38
- package/templates/create/tanstack-start/tsconfig.json +1 -0
- package/templates/create/turborepo/README.md.hbs +25 -14
- package/templates/create/turborepo/apps/api/deno.json.hbs +6 -0
- package/templates/create/turborepo/apps/api/package.json.hbs +7 -0
- package/templates/create/turborepo/apps/api/src/index.ts.hbs +6 -13
- package/templates/create/turborepo/deno.json.hbs +5 -1
- package/templates/create/turborepo/package.json.hbs +12 -4
- package/templates/create/turborepo/packages/db/deno.json.hbs +6 -0
- package/templates/create/turborepo/packages/db/package.json.hbs +5 -0
- package/templates/create/turborepo/packages/db/prisma/contract.prisma.hbs +44 -0
- package/templates/create/turborepo/packages/db/prisma/contract.ts.hbs +85 -0
- package/templates/create/turborepo/packages/db/prisma-next.config.ts.hbs +13 -0
- package/templates/create/turborepo/packages/db/src/client.ts.hbs +56 -44
- package/templates/create/turborepo/packages/db/src/index.ts +1 -1
- package/templates/create/turborepo/packages/db/tsconfig.json +2 -1
- package/templates/create/turborepo/pnpm-workspace.yaml.hbs +5 -0
- package/templates/create/turborepo/turbo.json +9 -3
- package/templates/create/astro/prisma/schema.prisma.hbs +0 -21
- package/templates/create/astro/prisma/seed.ts.hbs +0 -38
- package/templates/create/astro/prisma.config.ts +0 -13
- package/templates/create/elysia/prisma/schema.prisma.hbs +0 -25
- package/templates/create/elysia/prisma/seed.ts.hbs +0 -42
- package/templates/create/elysia/prisma.config.ts.hbs +0 -15
- package/templates/create/hono/prisma/schema.prisma.hbs +0 -25
- package/templates/create/hono/prisma/seed.ts.hbs +0 -42
- package/templates/create/hono/prisma.config.ts.hbs +0 -15
- package/templates/create/nest/prisma/schema.prisma.hbs +0 -25
- package/templates/create/nest/prisma/seed.ts.hbs +0 -44
- package/templates/create/nest/prisma.config.ts.hbs +0 -15
- package/templates/create/next/prisma/schema.prisma.hbs +0 -21
- package/templates/create/next/prisma/seed.ts.hbs +0 -38
- package/templates/create/next/prisma.config.ts +0 -13
- package/templates/create/nuxt/prisma/schema.prisma.hbs +0 -21
- package/templates/create/nuxt/prisma/seed.ts.hbs +0 -38
- package/templates/create/nuxt/prisma.config.ts +0 -13
- package/templates/create/svelte/prisma/schema.prisma.hbs +0 -21
- package/templates/create/svelte/prisma/seed.ts.hbs +0 -87
- package/templates/create/svelte/prisma.config.ts +0 -13
- package/templates/create/tanstack-start/prisma/schema.prisma.hbs +0 -21
- package/templates/create/tanstack-start/prisma/seed.ts.hbs +0 -37
- package/templates/create/tanstack-start/prisma.config.ts +0 -13
- package/templates/create/turborepo/packages/db/prisma/schema.prisma.hbs +0 -21
- package/templates/create/turborepo/packages/db/prisma/seed.ts.hbs +0 -38
- package/templates/create/turborepo/packages/db/prisma.config.ts +0 -13
|
@@ -1,20 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
{{#if (eq schemaPreset "basic")}}
|
|
3
|
-
import
|
|
3
|
+
import { listUsers } from "../lib/prisma";
|
|
4
4
|
|
|
5
5
|
const formatter = new Intl.DateTimeFormat("en", {
|
|
6
6
|
dateStyle: "medium",
|
|
7
7
|
timeStyle: "short",
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
const users = await
|
|
11
|
-
.findMany({
|
|
12
|
-
take: 10,
|
|
13
|
-
orderBy: {
|
|
14
|
-
createdAt: "desc",
|
|
15
|
-
},
|
|
16
|
-
})
|
|
17
|
-
.catch(() => undefined);
|
|
10
|
+
const users = await listUsers(10).catch(() => undefined);
|
|
18
11
|
{{/if}}
|
|
19
12
|
---
|
|
20
13
|
|
|
@@ -29,11 +22,11 @@ const users = await prisma.user
|
|
|
29
22
|
<body>
|
|
30
23
|
<main class="shell">
|
|
31
24
|
<div class="hero">
|
|
32
|
-
<p class="eyebrow">Astro + Prisma
|
|
25
|
+
<p class="eyebrow">Astro + Prisma Next</p>
|
|
33
26
|
{{#if (eq schemaPreset "basic")}}
|
|
34
27
|
<h1>Users from your database, loaded on the server.</h1>
|
|
35
28
|
<p class="lede">
|
|
36
|
-
This page reads from <code>src/pages/index.astro</code> using the Prisma
|
|
29
|
+
This page reads from <code>src/pages/index.astro</code> using the Prisma Next helper in
|
|
37
30
|
<code>src/lib/prisma.ts</code>. An Astro API route is also scaffolded in
|
|
38
31
|
<code>src/pages/api/users.ts</code>.
|
|
39
32
|
</p>
|
|
@@ -47,11 +40,11 @@ const users = await prisma.user
|
|
|
47
40
|
|
|
48
41
|
{!users ? (
|
|
49
42
|
<p class="empty">
|
|
50
|
-
Could not query users yet. Run <code>
|
|
43
|
+
Could not query users yet. Run <code>contract:emit</code> and apply your schema,
|
|
51
44
|
then refresh.
|
|
52
45
|
</p>
|
|
53
46
|
) : users.length === 0 ? (
|
|
54
|
-
<p class="empty">No users yet. Run
|
|
47
|
+
<p class="empty">No users yet. Run db:seed after applying your first migration.</p>
|
|
55
48
|
) : (
|
|
56
49
|
<ul class="users">
|
|
57
50
|
{users.map((user) => (
|
|
@@ -60,9 +53,13 @@ const users = await prisma.user
|
|
|
60
53
|
<strong>{user.name ?? "Unnamed user"}</strong>
|
|
61
54
|
<p>{user.email}</p>
|
|
62
55
|
</div>
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
|
|
56
|
+
{user.createdAt ? (
|
|
57
|
+
<time datetime={user.createdAt.toISOString()}>
|
|
58
|
+
{formatter.format(user.createdAt)}
|
|
59
|
+
</time>
|
|
60
|
+
) : (
|
|
61
|
+
<span class="muted">No timestamp</span>
|
|
62
|
+
)}
|
|
66
63
|
</li>
|
|
67
64
|
))}
|
|
68
65
|
</ul>
|
|
@@ -71,8 +68,8 @@ const users = await prisma.user
|
|
|
71
68
|
{{else}}
|
|
72
69
|
<h1>Your Astro app is ready.</h1>
|
|
73
70
|
<p class="lede">
|
|
74
|
-
Edit
|
|
75
|
-
in Astro pages or API routes with the
|
|
71
|
+
Edit your Prisma Next contract, run <code>contract:emit</code>, then load your data
|
|
72
|
+
in Astro pages or API routes with the helper in <code>src/lib/prisma.ts</code>.
|
|
76
73
|
</p>
|
|
77
74
|
</div>
|
|
78
75
|
|
|
@@ -86,7 +83,7 @@ const users = await prisma.user
|
|
|
86
83
|
<li>
|
|
87
84
|
<div>
|
|
88
85
|
<strong>Prisma client</strong>
|
|
89
|
-
<p>Use the shared
|
|
86
|
+
<p>Use the shared Prisma Next runtime from <code>src/lib/prisma.ts</code>.</p>
|
|
90
87
|
</div>
|
|
91
88
|
</li>
|
|
92
89
|
<li>
|
|
@@ -97,8 +94,8 @@ const users = await prisma.user
|
|
|
97
94
|
</li>
|
|
98
95
|
<li>
|
|
99
96
|
<div>
|
|
100
|
-
<strong>
|
|
101
|
-
<p>Run
|
|
97
|
+
<strong>Manual database setup</strong>
|
|
98
|
+
<p>Run the schema setup commands when you are ready.</p>
|
|
102
99
|
</div>
|
|
103
100
|
</li>
|
|
104
101
|
</ul>
|
|
@@ -171,6 +168,7 @@ const users = await prisma.user
|
|
|
171
168
|
|
|
172
169
|
.panel-header span,
|
|
173
170
|
.empty,
|
|
171
|
+
.muted,
|
|
174
172
|
time {
|
|
175
173
|
color: #888;
|
|
176
174
|
font-size: 0.8rem;
|
|
@@ -8,25 +8,34 @@ Generated by `create-prisma` with the Elysia template.
|
|
|
8
8
|
- `{{runScriptCommand packageManager "build"}}` - {{#if (eq packageManager "deno")}}type-check the app with Deno{{else}}{{#if (eq packageManager "bun")}}type-check the app for Bun{{else}}typecheck and compile{{/if}}{{/if}}
|
|
9
9
|
- `{{runScriptCommand packageManager "start"}}` - {{#if (eq packageManager "deno")}}run the server directly from `src/index.ts` with Deno{{else}}{{#if (eq packageManager "bun")}}run the server directly from `src/index.ts` with Bun{{else}}run compiled server from `dist/`{{/if}}{{/if}}
|
|
10
10
|
|
|
11
|
-
## Prisma
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
`{{runScriptCommand packageManager "db:
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
## Prisma Next
|
|
12
|
+
|
|
13
|
+
Prisma Next setup is scaffolded in:
|
|
14
|
+
|
|
15
|
+
- `prisma/contract{{#if (eq authoring "typescript")}}.ts{{else}}.prisma{{/if}}`
|
|
16
|
+
- `prisma-next.config.ts`
|
|
17
|
+
- `src/lib/prisma.ts`
|
|
18
|
+
|
|
19
|
+
Database helper scripts are added to `package.json`:
|
|
20
|
+
|
|
21
|
+
- `{{runScriptCommand packageManager "contract:emit"}}` - emit contract artifacts after contract changes
|
|
22
|
+
{{#if (eq provider "mongo")}}
|
|
23
|
+
- `{{runScriptCommand packageManager "db:up"}}` - start the local MongoDB replica set
|
|
24
|
+
- `{{runScriptCommand packageManager "db:down"}}` - stop the local MongoDB replica set
|
|
25
|
+
- `{{runScriptCommand packageManager "migration:plan"}}` - create a MongoDB migration plan
|
|
26
|
+
- `{{runScriptCommand packageManager "migration:apply"}}` - apply the planned MongoDB migration
|
|
27
|
+
{{else}}
|
|
28
|
+
- `{{runScriptCommand packageManager "db:init"}}` - initialize database state manually
|
|
29
|
+
- `{{runScriptCommand packageManager "db:update"}}` - update database state manually
|
|
30
|
+
- `{{runScriptCommand packageManager "migration:plan"}}` - create a migration plan
|
|
31
|
+
- `{{runScriptCommand packageManager "migration:apply"}}` - apply a planned migration
|
|
32
|
+
{{/if}}
|
|
33
|
+
{{#if (eq schemaPreset "basic")}}
|
|
34
|
+
- `{{runScriptCommand packageManager "db:seed"}}` - insert sample users manually
|
|
35
|
+
{{/if}}
|
|
27
36
|
|
|
28
|
-
|
|
37
|
+
Node-based Prisma Next projects expect Node.js 24 LTS or newer.
|
|
29
38
|
{{#if (eq schemaPreset "basic")}}
|
|
30
39
|
|
|
31
|
-
The template includes a basic `User` model
|
|
40
|
+
The template includes a basic `User` model and a sample `GET /users` endpoint.
|
|
32
41
|
{{/if}}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{{#if (eq authoring "psl")}}
|
|
2
|
+
// use prisma-next
|
|
3
|
+
{{#if (eq schemaPreset "basic")}}
|
|
4
|
+
{{#if (eq provider "mongo")}}
|
|
5
|
+
|
|
6
|
+
model User {
|
|
7
|
+
id ObjectId @id @map("_id")
|
|
8
|
+
email String @unique
|
|
9
|
+
name String?
|
|
10
|
+
posts Post[]
|
|
11
|
+
|
|
12
|
+
@@map("users")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
model Post {
|
|
16
|
+
id ObjectId @id @map("_id")
|
|
17
|
+
title String
|
|
18
|
+
content String?
|
|
19
|
+
author User @relation(fields: [authorId], references: [id])
|
|
20
|
+
authorId ObjectId
|
|
21
|
+
|
|
22
|
+
@@map("posts")
|
|
23
|
+
}
|
|
24
|
+
{{else}}
|
|
25
|
+
|
|
26
|
+
model User {
|
|
27
|
+
id Int @id @default(autoincrement())
|
|
28
|
+
email String @unique
|
|
29
|
+
name String?
|
|
30
|
+
posts Post[]
|
|
31
|
+
createdAt DateTime @default(now())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
model Post {
|
|
35
|
+
id Int @id @default(autoincrement())
|
|
36
|
+
title String
|
|
37
|
+
content String?
|
|
38
|
+
author User @relation(fields: [authorId], references: [id])
|
|
39
|
+
authorId Int
|
|
40
|
+
createdAt DateTime @default(now())
|
|
41
|
+
}
|
|
42
|
+
{{/if}}
|
|
43
|
+
{{/if}}
|
|
44
|
+
{{/if}}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{{#if (eq authoring "typescript")}}
|
|
2
|
+
{{#if (eq provider "mongo")}}
|
|
3
|
+
import mongoFamily from "@prisma-next/family-mongo/pack";
|
|
4
|
+
import { defineContract } from "@prisma-next/mongo-contract-ts/contract-builder";
|
|
5
|
+
import mongoTarget from "@prisma-next/target-mongo/pack";
|
|
6
|
+
|
|
7
|
+
export const contract = defineContract(
|
|
8
|
+
{ family: mongoFamily, target: mongoTarget },
|
|
9
|
+
({ field, index, model, rel }) => ({
|
|
10
|
+
models: {
|
|
11
|
+
{{#if (eq schemaPreset "basic")}}
|
|
12
|
+
User: model("User", {
|
|
13
|
+
collection: "users",
|
|
14
|
+
fields: {
|
|
15
|
+
_id: field.objectId(),
|
|
16
|
+
email: field.string(),
|
|
17
|
+
name: field.string().optional(),
|
|
18
|
+
},
|
|
19
|
+
indexes: [
|
|
20
|
+
index({ email: 1 }, { unique: true }),
|
|
21
|
+
],
|
|
22
|
+
relations: {
|
|
23
|
+
posts: rel.hasMany("Post", { from: "_id", to: "authorId" }),
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
|
|
27
|
+
Post: model("Post", {
|
|
28
|
+
collection: "posts",
|
|
29
|
+
fields: {
|
|
30
|
+
_id: field.objectId(),
|
|
31
|
+
title: field.string(),
|
|
32
|
+
content: field.string().optional(),
|
|
33
|
+
authorId: field.objectId(),
|
|
34
|
+
},
|
|
35
|
+
indexes: [
|
|
36
|
+
index({ authorId: 1 }),
|
|
37
|
+
],
|
|
38
|
+
relations: {
|
|
39
|
+
author: rel.belongsTo("User", { from: "authorId", to: "_id" }),
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
{{/if}}
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
{{else}}
|
|
47
|
+
import sqlFamily from "@prisma-next/family-sql/pack";
|
|
48
|
+
import { defineContract } from "@prisma-next/sql-contract-ts/contract-builder";
|
|
49
|
+
import postgresPack from "@prisma-next/target-postgres/pack";
|
|
50
|
+
|
|
51
|
+
export const contract = defineContract(
|
|
52
|
+
{ family: sqlFamily, target: postgresPack },
|
|
53
|
+
({ field, model, rel }) => ({
|
|
54
|
+
models: {
|
|
55
|
+
{{#if (eq schemaPreset "basic")}}
|
|
56
|
+
User: model("User", {
|
|
57
|
+
fields: {
|
|
58
|
+
id: field.id.uuidv7(),
|
|
59
|
+
email: field.text().unique(),
|
|
60
|
+
name: field.text().optional(),
|
|
61
|
+
createdAt: field.createdAt(),
|
|
62
|
+
},
|
|
63
|
+
relations: {
|
|
64
|
+
posts: rel.hasMany("Post", { by: "authorId" }),
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
|
|
68
|
+
Post: model("Post", {
|
|
69
|
+
fields: {
|
|
70
|
+
id: field.id.uuidv7(),
|
|
71
|
+
title: field.text(),
|
|
72
|
+
content: field.text().optional(),
|
|
73
|
+
authorId: field.uuid(),
|
|
74
|
+
createdAt: field.createdAt(),
|
|
75
|
+
},
|
|
76
|
+
relations: {
|
|
77
|
+
author: rel.belongsTo("User", { from: "authorId", to: "id" }),
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
{{/if}}
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
{{/if}}
|
|
85
|
+
{{/if}}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
{{#if (eq provider "mongo")}}
|
|
3
|
+
import { defineConfig } from "@prisma-next/mongo/config";
|
|
4
|
+
{{else}}
|
|
5
|
+
import { defineConfig } from "@prisma-next/postgres/config";
|
|
6
|
+
{{/if}}
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
contract: "./prisma/contract{{#if (eq authoring "typescript")}}.ts{{else}}.prisma{{/if}}",
|
|
10
|
+
db: {
|
|
11
|
+
connection: process.env["DATABASE_URL"],
|
|
12
|
+
},
|
|
13
|
+
});
|
|
@@ -7,7 +7,7 @@ import { node } from "@elysiajs/node";
|
|
|
7
7
|
{{/if}}
|
|
8
8
|
import { Elysia } from "elysia";
|
|
9
9
|
{{#if (eq schemaPreset "basic")}}
|
|
10
|
-
import {
|
|
10
|
+
import { listUsers } from "./lib/prisma{{#if (eq packageManager "deno")}}.ts{{/if}}";
|
|
11
11
|
{{/if}}
|
|
12
12
|
|
|
13
13
|
const rawPort = ({{#if (eq packageManager "deno")}}Deno.env.get("PORT"){{else}}process.env.PORT{{/if}} ?? "").trim();
|
|
@@ -22,13 +22,18 @@ const app = new Elysia({{#if (eq packageManager "deno")}}{{else}}{ adapter: node
|
|
|
22
22
|
};
|
|
23
23
|
})
|
|
24
24
|
{{#if (eq schemaPreset "basic")}}
|
|
25
|
-
.get("/users", async () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
createdAt: "desc",
|
|
30
|
-
},
|
|
25
|
+
.get("/users", async ({ set }) => {
|
|
26
|
+
const users = await listUsers(10).catch((error) => {
|
|
27
|
+
console.error("Failed to query users:", error);
|
|
28
|
+
return undefined;
|
|
31
29
|
});
|
|
30
|
+
|
|
31
|
+
if (!users) {
|
|
32
|
+
set.status = 500;
|
|
33
|
+
return { error: "Could not query users yet. Run contract:emit and apply your schema first." };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return users;
|
|
32
37
|
})
|
|
33
38
|
{{/if}}
|
|
34
39
|
{{#if (eq packageManager "deno")}}
|
|
@@ -1,56 +1,59 @@
|
|
|
1
|
-
{{#if (requiresDotenvConfigImport packageManager)}}
|
|
2
1
|
import "dotenv/config";
|
|
3
|
-
{{
|
|
4
|
-
import
|
|
5
|
-
{{#if (eq provider "postgresql")}}
|
|
6
|
-
import { PrismaPg } from "@prisma/adapter-pg";
|
|
7
|
-
{{/if}}
|
|
8
|
-
{{#if (eq provider "cockroachdb")}}
|
|
9
|
-
import { PrismaPg } from "@prisma/adapter-pg";
|
|
10
|
-
{{/if}}
|
|
11
|
-
{{#if (eq provider "mysql")}}
|
|
12
|
-
import { PrismaMariaDb } from "@prisma/adapter-mariadb";
|
|
13
|
-
{{/if}}
|
|
14
|
-
{{#if (eq provider "sqlite")}}
|
|
15
|
-
import { PrismaBetterSqlite3 } from "@prisma/adapter-better-sqlite3";
|
|
16
|
-
{{/if}}
|
|
17
|
-
{{#if (eq provider "sqlserver")}}
|
|
18
|
-
import { PrismaMssql } from "@prisma/adapter-mssql";
|
|
19
|
-
{{/if}}
|
|
20
|
-
|
|
21
|
-
const rawDatabaseUrl = {{#if (eq packageManager "deno")}}Deno.env.get("DATABASE_URL"){{else}}process.env.DATABASE_URL{{/if}};
|
|
22
|
-
{{#if (eq provider "sqlite")}}
|
|
23
|
-
const databaseUrl = (rawDatabaseUrl ?? "").trim() || "file:./dev.db";
|
|
2
|
+
{{#if (eq provider "mongo")}}
|
|
3
|
+
import mongo from "@prisma-next/mongo/runtime";
|
|
24
4
|
{{else}}
|
|
25
|
-
|
|
26
|
-
if (!databaseUrl) {
|
|
27
|
-
throw new Error("DATABASE_URL is required");
|
|
28
|
-
}
|
|
5
|
+
import postgres from "@prisma-next/postgres/runtime";
|
|
29
6
|
{{/if}}
|
|
7
|
+
import type { Contract } from "../../prisma/contract.d";
|
|
8
|
+
import contractJson from "../../prisma/contract.json" with { type: "json" };
|
|
30
9
|
|
|
31
|
-
{{#if (eq provider "
|
|
32
|
-
const
|
|
33
|
-
|
|
10
|
+
{{#if (eq provider "mongo")}}
|
|
11
|
+
export const db = mongo<Contract>({
|
|
12
|
+
contractJson,
|
|
13
|
+
url: process.env["DATABASE_URL"],
|
|
34
14
|
});
|
|
35
|
-
{{
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
{{else}}
|
|
16
|
+
export const db = postgres<Contract>({
|
|
17
|
+
contractJson,
|
|
18
|
+
url: process.env["DATABASE_URL"],
|
|
39
19
|
});
|
|
40
20
|
{{/if}}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
21
|
+
|
|
22
|
+
export type StarterUser = {
|
|
23
|
+
id: string;
|
|
24
|
+
email: string;
|
|
25
|
+
name: string | null;
|
|
26
|
+
createdAt: Date | null;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export async function listUsers(limit = 10): Promise<StarterUser[]> {
|
|
30
|
+
{{#if (eq schemaPreset "basic")}}
|
|
31
|
+
{{#if (eq provider "mongo")}}
|
|
32
|
+
const users: StarterUser[] = [];
|
|
33
|
+
|
|
34
|
+
for await (const user of db.orm.users.select("_id", "email", "name").take(limit).all()) {
|
|
35
|
+
users.push({
|
|
36
|
+
id: String(user._id),
|
|
37
|
+
email: user.email,
|
|
38
|
+
name: user.name ?? null,
|
|
39
|
+
createdAt: null,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return users;
|
|
44
|
+
{{else}}
|
|
45
|
+
const users = await db.orm.User.select("id", "email", "name", "createdAt").take(limit).all();
|
|
46
|
+
|
|
47
|
+
return users.map((user) => ({
|
|
48
|
+
id: String(user.id),
|
|
49
|
+
email: user.email,
|
|
50
|
+
name: user.name ?? null,
|
|
51
|
+
createdAt: user.createdAt,
|
|
52
|
+
}));
|
|
48
53
|
{{/if}}
|
|
49
|
-
{{
|
|
50
|
-
|
|
54
|
+
{{else}}
|
|
55
|
+
return [];
|
|
51
56
|
{{/if}}
|
|
57
|
+
}
|
|
52
58
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
export { prisma };
|
|
56
|
-
export default prisma;
|
|
59
|
+
export default db;
|
|
@@ -8,25 +8,34 @@ Generated by `create-prisma` with the Hono template.
|
|
|
8
8
|
- `{{runScriptCommand packageManager "build"}}` - {{#if (eq packageManager "deno")}}type-check the app with Deno{{else}}{{#if (eq packageManager "bun")}}type-check the app for Bun{{else}}typecheck and compile{{/if}}{{/if}}
|
|
9
9
|
- `{{runScriptCommand packageManager "start"}}` - {{#if (eq packageManager "deno")}}run the server directly from `src/index.ts` with Deno{{else}}{{#if (eq packageManager "bun")}}run the server directly from `src/index.ts` with Bun{{else}}run compiled server from `dist/`{{/if}}{{/if}}
|
|
10
10
|
|
|
11
|
-
## Prisma
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
`{{runScriptCommand packageManager "db:
|
|
25
|
-
|
|
26
|
-
|
|
11
|
+
## Prisma Next
|
|
12
|
+
|
|
13
|
+
Prisma Next setup is scaffolded in:
|
|
14
|
+
|
|
15
|
+
- `prisma/contract{{#if (eq authoring "typescript")}}.ts{{else}}.prisma{{/if}}`
|
|
16
|
+
- `prisma-next.config.ts`
|
|
17
|
+
- `src/lib/prisma.ts`
|
|
18
|
+
|
|
19
|
+
Database helper scripts are added to `package.json`:
|
|
20
|
+
|
|
21
|
+
- `{{runScriptCommand packageManager "contract:emit"}}` - emit contract artifacts after contract changes
|
|
22
|
+
{{#if (eq provider "mongo")}}
|
|
23
|
+
- `{{runScriptCommand packageManager "db:up"}}` - start the local MongoDB replica set
|
|
24
|
+
- `{{runScriptCommand packageManager "db:down"}}` - stop the local MongoDB replica set
|
|
25
|
+
- `{{runScriptCommand packageManager "migration:plan"}}` - create a MongoDB migration plan
|
|
26
|
+
- `{{runScriptCommand packageManager "migration:apply"}}` - apply the planned MongoDB migration
|
|
27
|
+
{{else}}
|
|
28
|
+
- `{{runScriptCommand packageManager "db:init"}}` - initialize database state manually
|
|
29
|
+
- `{{runScriptCommand packageManager "db:update"}}` - update database state manually
|
|
30
|
+
- `{{runScriptCommand packageManager "migration:plan"}}` - create a migration plan
|
|
31
|
+
- `{{runScriptCommand packageManager "migration:apply"}}` - apply a planned migration
|
|
32
|
+
{{/if}}
|
|
33
|
+
{{#if (eq schemaPreset "basic")}}
|
|
34
|
+
- `{{runScriptCommand packageManager "db:seed"}}` - insert sample users manually
|
|
35
|
+
{{/if}}
|
|
27
36
|
|
|
28
|
-
|
|
37
|
+
Node-based Prisma Next projects expect Node.js 24 LTS or newer.
|
|
29
38
|
{{#if (eq schemaPreset "basic")}}
|
|
30
39
|
|
|
31
|
-
The template includes a basic `User` model
|
|
40
|
+
The template includes a basic `User` model and a sample `GET /users` endpoint.
|
|
32
41
|
{{/if}}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{{#if (eq authoring "psl")}}
|
|
2
|
+
// use prisma-next
|
|
3
|
+
{{#if (eq schemaPreset "basic")}}
|
|
4
|
+
{{#if (eq provider "mongo")}}
|
|
5
|
+
|
|
6
|
+
model User {
|
|
7
|
+
id ObjectId @id @map("_id")
|
|
8
|
+
email String @unique
|
|
9
|
+
name String?
|
|
10
|
+
posts Post[]
|
|
11
|
+
|
|
12
|
+
@@map("users")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
model Post {
|
|
16
|
+
id ObjectId @id @map("_id")
|
|
17
|
+
title String
|
|
18
|
+
content String?
|
|
19
|
+
author User @relation(fields: [authorId], references: [id])
|
|
20
|
+
authorId ObjectId
|
|
21
|
+
|
|
22
|
+
@@map("posts")
|
|
23
|
+
}
|
|
24
|
+
{{else}}
|
|
25
|
+
|
|
26
|
+
model User {
|
|
27
|
+
id Int @id @default(autoincrement())
|
|
28
|
+
email String @unique
|
|
29
|
+
name String?
|
|
30
|
+
posts Post[]
|
|
31
|
+
createdAt DateTime @default(now())
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
model Post {
|
|
35
|
+
id Int @id @default(autoincrement())
|
|
36
|
+
title String
|
|
37
|
+
content String?
|
|
38
|
+
author User @relation(fields: [authorId], references: [id])
|
|
39
|
+
authorId Int
|
|
40
|
+
createdAt DateTime @default(now())
|
|
41
|
+
}
|
|
42
|
+
{{/if}}
|
|
43
|
+
{{/if}}
|
|
44
|
+
{{/if}}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{{#if (eq authoring "typescript")}}
|
|
2
|
+
{{#if (eq provider "mongo")}}
|
|
3
|
+
import mongoFamily from "@prisma-next/family-mongo/pack";
|
|
4
|
+
import { defineContract } from "@prisma-next/mongo-contract-ts/contract-builder";
|
|
5
|
+
import mongoTarget from "@prisma-next/target-mongo/pack";
|
|
6
|
+
|
|
7
|
+
export const contract = defineContract(
|
|
8
|
+
{ family: mongoFamily, target: mongoTarget },
|
|
9
|
+
({ field, index, model, rel }) => ({
|
|
10
|
+
models: {
|
|
11
|
+
{{#if (eq schemaPreset "basic")}}
|
|
12
|
+
User: model("User", {
|
|
13
|
+
collection: "users",
|
|
14
|
+
fields: {
|
|
15
|
+
_id: field.objectId(),
|
|
16
|
+
email: field.string(),
|
|
17
|
+
name: field.string().optional(),
|
|
18
|
+
},
|
|
19
|
+
indexes: [
|
|
20
|
+
index({ email: 1 }, { unique: true }),
|
|
21
|
+
],
|
|
22
|
+
relations: {
|
|
23
|
+
posts: rel.hasMany("Post", { from: "_id", to: "authorId" }),
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
|
|
27
|
+
Post: model("Post", {
|
|
28
|
+
collection: "posts",
|
|
29
|
+
fields: {
|
|
30
|
+
_id: field.objectId(),
|
|
31
|
+
title: field.string(),
|
|
32
|
+
content: field.string().optional(),
|
|
33
|
+
authorId: field.objectId(),
|
|
34
|
+
},
|
|
35
|
+
indexes: [
|
|
36
|
+
index({ authorId: 1 }),
|
|
37
|
+
],
|
|
38
|
+
relations: {
|
|
39
|
+
author: rel.belongsTo("User", { from: "authorId", to: "_id" }),
|
|
40
|
+
},
|
|
41
|
+
}),
|
|
42
|
+
{{/if}}
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
{{else}}
|
|
47
|
+
import sqlFamily from "@prisma-next/family-sql/pack";
|
|
48
|
+
import { defineContract } from "@prisma-next/sql-contract-ts/contract-builder";
|
|
49
|
+
import postgresPack from "@prisma-next/target-postgres/pack";
|
|
50
|
+
|
|
51
|
+
export const contract = defineContract(
|
|
52
|
+
{ family: sqlFamily, target: postgresPack },
|
|
53
|
+
({ field, model, rel }) => ({
|
|
54
|
+
models: {
|
|
55
|
+
{{#if (eq schemaPreset "basic")}}
|
|
56
|
+
User: model("User", {
|
|
57
|
+
fields: {
|
|
58
|
+
id: field.id.uuidv7(),
|
|
59
|
+
email: field.text().unique(),
|
|
60
|
+
name: field.text().optional(),
|
|
61
|
+
createdAt: field.createdAt(),
|
|
62
|
+
},
|
|
63
|
+
relations: {
|
|
64
|
+
posts: rel.hasMany("Post", { by: "authorId" }),
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
|
|
68
|
+
Post: model("Post", {
|
|
69
|
+
fields: {
|
|
70
|
+
id: field.id.uuidv7(),
|
|
71
|
+
title: field.text(),
|
|
72
|
+
content: field.text().optional(),
|
|
73
|
+
authorId: field.uuid(),
|
|
74
|
+
createdAt: field.createdAt(),
|
|
75
|
+
},
|
|
76
|
+
relations: {
|
|
77
|
+
author: rel.belongsTo("User", { from: "authorId", to: "id" }),
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
{{/if}}
|
|
81
|
+
},
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
{{/if}}
|
|
85
|
+
{{/if}}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
{{#if (eq provider "mongo")}}
|
|
3
|
+
import { defineConfig } from "@prisma-next/mongo/config";
|
|
4
|
+
{{else}}
|
|
5
|
+
import { defineConfig } from "@prisma-next/postgres/config";
|
|
6
|
+
{{/if}}
|
|
7
|
+
|
|
8
|
+
export default defineConfig({
|
|
9
|
+
contract: "./prisma/contract{{#if (eq authoring "typescript")}}.ts{{else}}.prisma{{/if}}",
|
|
10
|
+
db: {
|
|
11
|
+
connection: process.env["DATABASE_URL"],
|
|
12
|
+
},
|
|
13
|
+
});
|