Skip to content

Commit 446abbc

Browse files
authored
feat(prisma)!: Prisma v7 (#1337)
1 parent cffa7bc commit 446abbc

File tree

165 files changed

+2409
-916
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+2409
-916
lines changed

.changesets/release_notes_major.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ prisma.config.cjs. The config is for the cli.
55
Need to remove it from `schema.prisma` and add it to the config file. Point to
66
Prisma docs for this.
77

8+
## SQLite `DATABASE_URL` path change
9+
10+
If your project uses SQLite, update your `DATABASE_URL` from:
11+
12+
- `file:./dev.db`
13+
14+
to:
15+
16+
- `file:./db/dev.db`
17+
18+
This keeps the SQLite database file in `api/db/dev.db`, which is where Cedar's
19+
Prisma 7 setup expects it to live.
20+
21+
In practice, this means updating the value in the env file your project uses,
22+
for example:
23+
24+
```js
25+
DATABASE_URL=file:./db/dev.db
26+
```
27+
28+
If you leave the old value in place, Prisma CLI commands and runtime database
29+
access can end up pointing at different files, which may show up as errors like
30+
"table does not exist" even though the expected tables exist in another SQLite
31+
file.
32+
833
Testing potentially works differently. If you just have `DATABASE_URL` in your
934
prisma.config.cjs file, it will be replaced by `TEST_DATABASE_URL` just like
1035
before. But if you need a specific `directUrl`, you should put that in your

.github/actions/set-up-rsc-project/setUpRscProject.mts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import type { ExecOptions } from '@actions/exec'
55

66
import { CEDAR_FRAMEWORK_PATH } from '../actionsLib.mjs'
77

8+
const CEDAR_APP_TEMPLATE_PATH = path.join(
9+
CEDAR_FRAMEWORK_PATH,
10+
'packages',
11+
'create-cedar-app',
12+
'templates',
13+
'ts',
14+
)
15+
16+
const PRISMA_SCHEMA_DATASOURCE_URL_OLD = /^\s*url\s*=\s*env\([^)]*\)\s*\n/m
17+
18+
const CEDAR_APP_TEMPLATE_API_PACKAGE_JSON_PATH = path.join(
19+
CEDAR_APP_TEMPLATE_PATH,
20+
'api',
21+
'package.json',
22+
)
23+
24+
const PRISMA_SCHEMA_GENERATOR_OLD = /generator client \{[^}]*\}/s
25+
826
/**
927
* Exec a command.
1028
* Output will be streamed to the live console.
@@ -95,7 +113,100 @@ async function setUpRscProject(
95113
})
96114
console.log()
97115

116+
// TODO: Remove this block once Prisma v7 support has been merged into a
117+
// canary release of create-cedar-app, at which point the created project
118+
// will already be Prisma v7 compatible.
119+
console.log('Updating project for Prisma v7 compatibility')
120+
updateProjectForPrisma7(rscProjectPath)
121+
console.log()
122+
123+
console.log('Installing new Prisma v7 dependencies')
124+
await execInProject('yarn install')
125+
console.log()
126+
98127
console.log(`Building project in ${rscProjectPath}`)
99128
await execInProject(`node ${cedarBinPath} build -v`)
100129
console.log()
101130
}
131+
132+
/**
133+
* @param {string} rscProjectPath
134+
* @returns {void}
135+
*/
136+
function updateProjectForPrisma7(rscProjectPath) {
137+
// Copy prisma.config.cjs from template (Prisma v7 format)
138+
const prismaConfigPath = path.join(rscProjectPath, 'api', 'prisma.config.cjs')
139+
fs.copyFileSync(
140+
path.join(CEDAR_APP_TEMPLATE_PATH, 'api', 'prisma.config.cjs'),
141+
prismaConfigPath,
142+
)
143+
console.log(' Updated api/prisma.config.cjs')
144+
145+
// Update schema.prisma: remove datasource url line and replace generator block
146+
// (Can't copy the template wholesale because the project has extra models from
147+
// the RSC setup steps.)
148+
const schemaPath = path.join(rscProjectPath, 'api', 'db', 'schema.prisma')
149+
const templateSchema = fs.readFileSync(
150+
path.join(CEDAR_APP_TEMPLATE_PATH, 'api', 'db', 'schema.prisma'),
151+
'utf8',
152+
)
153+
const generatorMatch = templateSchema.match(PRISMA_SCHEMA_GENERATOR_OLD)
154+
const templateGenerator = generatorMatch ? generatorMatch[0] : null
155+
if (!templateGenerator) {
156+
throw new Error('Could not find generator block in template schema.prisma')
157+
}
158+
const schemaContent = fs.readFileSync(schemaPath, 'utf8')
159+
const updatedSchema = schemaContent
160+
.replace(PRISMA_SCHEMA_DATASOURCE_URL_OLD, '')
161+
.replace(PRISMA_SCHEMA_GENERATOR_OLD, templateGenerator)
162+
fs.writeFileSync(schemaPath, updatedSchema, 'utf8')
163+
console.log(' Updated api/db/schema.prisma')
164+
165+
// Copy api/src/lib/db.ts from template (Prisma v7 client initialisation)
166+
const dbTsPath = path.join(rscProjectPath, 'api', 'src', 'lib', 'db.ts')
167+
fs.copyFileSync(
168+
path.join(CEDAR_APP_TEMPLATE_PATH, 'api', 'src', 'lib', 'db.ts'),
169+
dbTsPath,
170+
)
171+
console.log(' Updated api/src/lib/db.ts')
172+
173+
// Copy api/tsconfig.json from template (Prisma v7 / Node16 module resolution)
174+
const apiTsconfigPath = path.join(rscProjectPath, 'api', 'tsconfig.json')
175+
fs.copyFileSync(
176+
path.join(CEDAR_APP_TEMPLATE_PATH, 'api', 'tsconfig.json'),
177+
apiTsconfigPath,
178+
)
179+
console.log(' Updated api/tsconfig.json')
180+
181+
// Merge @prisma/adapter-better-sqlite3 and better-sqlite3 from template into
182+
// api/package.json (the canary-generated project won't have these deps)
183+
const apiPackageJsonPath = path.join(rscProjectPath, 'api', 'package.json')
184+
const apiPackageJson = JSON.parse(fs.readFileSync(apiPackageJsonPath, 'utf8'))
185+
const templateApiPackageJson = JSON.parse(
186+
fs.readFileSync(CEDAR_APP_TEMPLATE_API_PACKAGE_JSON_PATH, 'utf8'),
187+
)
188+
const depsToAdd = ['@prisma/adapter-better-sqlite3', 'better-sqlite3']
189+
for (const dep of depsToAdd) {
190+
apiPackageJson.dependencies[dep] = templateApiPackageJson.dependencies[dep]
191+
}
192+
fs.writeFileSync(
193+
apiPackageJsonPath,
194+
JSON.stringify(apiPackageJson, null, 2) + '\n',
195+
'utf8',
196+
)
197+
console.log(' Updated api/package.json')
198+
199+
// Copy scripts/tsconfig.json from template (Prisma v7 / Node16 module resolution)
200+
const scriptsTsconfigPath = path.join(
201+
rscProjectPath,
202+
'scripts',
203+
'tsconfig.json',
204+
)
205+
if (fs.existsSync(scriptsTsconfigPath)) {
206+
fs.copyFileSync(
207+
path.join(CEDAR_APP_TEMPLATE_PATH, 'scripts', 'tsconfig.json'),
208+
scriptsTsconfigPath,
209+
)
210+
console.log(' Updated scripts/tsconfig.json')
211+
}
212+
}

.github/actions/set-up-test-project-esm/setUpTestProjectEsm.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ async function setUpTestProjectEsm({ canary }) {
6666

6767
console.log('Running prisma migrate reset')
6868
await execInProject('yarn cedar prisma migrate reset --force')
69+
70+
console.log('Running prisma db seed')
71+
await execInProject('yarn cedar prisma db seed')
6972
}
7073

7174
setUpTestProjectEsm({ canary })

.github/actions/set-up-test-project/setUpTestProject.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ export async function setUpTestProject({
7474

7575
console.log('Running prisma migrate reset')
7676
await execInProject('yarn cedar prisma migrate reset --force')
77+
78+
console.log('Running prisma db seed')
79+
await execInProject('yarn cedar prisma db seed')
7780
}

__fixtures__/empty-project/.env.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# into version control.
55

66
# schema.prisma defaults
7-
DATABASE_URL=file:./dev.db
7+
DATABASE_URL=file:./db/dev.db
88

99
# location of the test database for api service scenarios (defaults to ./.redwood/test.db if not set)
1010
# TEST_DATABASE_URL=file:./.redwood/test.db

__fixtures__/empty-project/api/db/schema.prisma

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
datasource db {
2-
provider = "sqlite"
3-
url = env("DATABASE_URL")
4-
directUrl = env("DIRECT_URL")
2+
provider = "sqlite"
53
}
64

75
generator client {

__fixtures__/esm-test-project/.env.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# into version control.
55

66
# schema.prisma defaults
7-
DATABASE_URL=file:./dev.db
7+
DATABASE_URL=file:./db/dev.db
88

99
# location of the test database for api service scenarios (defaults to ./.redwood/test.db if not set)
1010
# TEST_DATABASE_URL=file:./.redwood/test.db

__fixtures__/esm-test-project/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
.redwood/*
88
!.redwood/README.md
99
dev.db*
10+
api/db/generated/prisma
1011
dist
1112
dist-babel
1213
node_modules

__fixtures__/esm-test-project/api/db/schema.prisma

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
datasource db {
88
provider = "sqlite"
9-
url = env("DATABASE_URL")
109
}
1110

1211
generator client {
13-
provider = "prisma-client-js"
14-
binaryTargets = "native"
12+
provider = "prisma-client"
13+
output = "./generated/prisma"
14+
moduleFormat = "cjs"
15+
generatedFileExtension = "mts"
16+
importFileExtension = "mts"
1517
}
1618

1719
// Define your own datamodels here and run `yarn cedar prisma migrate dev`

__fixtures__/esm-test-project/api/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"dependencies": {
77
"@cedarjs/api": "2.8.0",
88
"@cedarjs/auth-dbauth-api": "2.8.0",
9-
"@cedarjs/graphql-server": "2.8.0"
9+
"@cedarjs/graphql-server": "2.8.0",
10+
"@prisma/adapter-better-sqlite3": "7.4.2",
11+
"better-sqlite3": "12.6.2"
1012
}
1113
}

0 commit comments

Comments
 (0)