Skip to content

Commit fc979ec

Browse files
committed
Support Prisma's official multi-file schema structure
1 parent 0ae59cf commit fc979ec

File tree

10 files changed

+62
-44
lines changed

10 files changed

+62
-44
lines changed

.changeset/unlucky-ghosts-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/build": minor
3+
---
4+
5+
Support Prisma's official multi-file schema structure

docs/config/extensions/prismaExtension.mdx

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ If you are using Prisma, you should use the prisma build extension.
1010
- Generates the Prisma client during the deploy process
1111
- Optionally will migrate the database during the deploy process
1212
- Support for TypedSQL and multiple schema files
13-
- You can use `prismaSchemaFolder` to specify just the directory containing your schema file, instead of the full path
14-
- You can add the extension twice if you have multiple separate schemas in the same project (example below)
13+
- You can set the schema to be a folder to use Prisma's multi-file schema feature (example [below](#multiple-schemas))
1514

1615
You can use it for a simple Prisma setup like this:
1716

@@ -141,17 +140,32 @@ These environment variables are only used during the build process and are not e
141140

142141
### Multiple schemas
143142

144-
If you have multiple separate schemas in the same project you can add the extension multiple times:
143+
Prisma supports splitting your schema into multiple files. To use this, set `schema` to be the folder that contains your root schema (e.g. `schema.prisma`).
145144

146-
```ts
147-
prismaExtension({
148-
schema: 'prisma/schema/main.prisma',
149-
version: '6.2.0',
150-
migrate: false,
151-
}),
145+
For example, if your root schema is located at `./prisma/schema.prisma`, you would set the `schema` option to `prisma`:
146+
147+
<CodeGroup>
148+
149+
```ts trigger.config.ts
152150
prismaExtension({
153-
schema: 'prisma/schema/secondary.prisma',
154-
version: '6.2.0',
151+
schema: 'prisma',
152+
version: '6.7.0',
155153
migrate: false,
156154
}),
157155
```
156+
157+
```shell Example structure
158+
./prisma
159+
├── migrations
160+
├── models
161+
│ ├── posts.prisma
162+
│ ├── users.prisma
163+
│ └── ... other `.prisma` files
164+
└── schema.prisma
165+
```
166+
167+
</CodeGroup>
168+
169+
<Note>
170+
To use this feature you must be using `[email protected]` or higher. Their official documentation can be found [here](https://www.prisma.io/docs/orm/prisma-schema/overview/location#multi-file-prisma-schema).
171+
</Note>

packages/build/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
},
7979
"dependencies": {
8080
"@trigger.dev/core": "workspace:4.0.0",
81+
"fast-glob": "^3.3.3",
8182
"pkg-types": "^1.1.3",
8283
"tinyglobby": "^0.2.2",
8384
"tsconfck": "3.1.3"

packages/build/src/extensions/prisma.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BuildManifest, BuildTarget } from "@trigger.dev/core/v3";
22
import { binaryForRuntime, BuildContext, BuildExtension } from "@trigger.dev/core/v3/build";
33
import assert from "node:assert";
4+
import { glob } from 'tinyglobby';
45
import { existsSync } from "node:fs";
56
import { cp, readdir } from "node:fs/promises";
67
import { dirname, join, resolve } from "node:path";
@@ -112,11 +113,18 @@ export class PrismaExtension implements BuildExtension {
112113

113114
context.logger.debug(`PrismaExtension is generating the Prisma client for version ${version}`);
114115

115-
const usingSchemaFolder = dirname(this._resolvedSchemaPath).endsWith("schema");
116+
// Multi-file schemas can be used by specifying a directory instead of a file. https://www.prisma.io/docs/orm/prisma-schema/overview/location#multi-file-prisma-schema
117+
const usingSchemaFolder = !this._resolvedSchemaPath.endsWith(".prisma");
118+
119+
context.logger.debug(`Using schema folder: ${usingSchemaFolder}`);
116120

117121
const commands: string[] = [];
118122

119-
let prismaDir: string | undefined;
123+
const prismaSourceDir = usingSchemaFolder
124+
? this._resolvedSchemaPath
125+
: dirname(this._resolvedSchemaPath);
126+
127+
const prismaDestinationDir = join(manifest.outputPath, "prisma");
120128

121129
const generatorFlags: string[] = [];
122130

@@ -127,26 +135,22 @@ export class PrismaExtension implements BuildExtension {
127135
if (this.options.typedSql) {
128136
generatorFlags.push(`--sql`);
129137

130-
const prismaDir = usingSchemaFolder
131-
? dirname(dirname(this._resolvedSchemaPath))
132-
: dirname(this._resolvedSchemaPath);
133-
134138
context.logger.debug(`Using typedSql`);
135139

136140
// Find all the files prisma/sql/*.sql
137-
const sqlFiles = await readdir(join(prismaDir, "sql")).then((files) =>
141+
const sqlFiles = await readdir(join(prismaSourceDir, "sql")).then((files) =>
138142
files.filter((file) => file.endsWith(".sql"))
139143
);
140144

141145
context.logger.debug(`Found sql files`, {
142146
sqlFiles,
143147
});
144148

145-
const sqlDestinationPath = join(manifest.outputPath, "prisma", "sql");
149+
const sqlDestinationPath = join(prismaDestinationDir, "sql");
146150

147151
for (const file of sqlFiles) {
148152
const destination = join(sqlDestinationPath, file);
149-
const source = join(prismaDir, "sql", file);
153+
const source = join(prismaSourceDir, "sql", file);
150154

151155
context.logger.debug(`Copying the sql from ${source} to ${destination}`);
152156

@@ -155,28 +159,20 @@ export class PrismaExtension implements BuildExtension {
155159
}
156160

157161
if (usingSchemaFolder) {
158-
const schemaDir = dirname(this._resolvedSchemaPath);
159-
160-
prismaDir = dirname(schemaDir);
161-
162-
context.logger.debug(`Using the schema folder: ${schemaDir}`);
162+
context.logger.debug(`Using the schema folder: ${this._resolvedSchemaPath}`);
163163

164164
// Find all the files in schemaDir that end with .prisma (excluding the schema.prisma file)
165-
const prismaFiles = await readdir(schemaDir).then((files) =>
166-
files.filter((file) => file.endsWith(".prisma"))
167-
);
165+
const prismaFiles = await glob(["**/*.prisma"], {
166+
cwd: this._resolvedSchemaPath
167+
})
168168

169169
context.logger.debug(`Found prisma files in the schema folder`, {
170170
prismaFiles,
171171
});
172172

173-
const schemaDestinationPath = join(manifest.outputPath, "prisma", "schema");
174-
175-
const allPrismaFiles = [...prismaFiles];
176-
177-
for (const file of allPrismaFiles) {
178-
const destination = join(schemaDestinationPath, file);
179-
const source = join(schemaDir, file);
173+
for (const file of prismaFiles) {
174+
const destination = join(prismaDestinationDir, file);
175+
const source = join(this._resolvedSchemaPath, file);
180176

181177
context.logger.debug(`Copying the prisma schema from ${source} to ${destination}`);
182178

@@ -186,15 +182,14 @@ export class PrismaExtension implements BuildExtension {
186182
commands.push(
187183
`${binaryForRuntime(
188184
manifest.runtime
189-
)} node_modules/prisma/build/index.js generate ${generatorFlags.join(" ")}` // Don't add the --schema flag or this will fail
185+
)} node_modules/prisma/build/index.js generate --schema ./prisma ${generatorFlags.join(" ")}`
190186
);
191187
} else {
192-
prismaDir = dirname(this._resolvedSchemaPath);
193188
// Now we need to add a layer that:
194189
// Copies the prisma schema to the build outputPath
195190
// Adds the `prisma` CLI dependency to the dependencies
196191
// Adds the `prisma generate` command, which generates the Prisma client
197-
const schemaDestinationPath = join(manifest.outputPath, "prisma", "schema.prisma");
192+
const schemaDestinationPath = join(prismaDestinationDir, "schema.prisma");
198193
// Copy the prisma schema to the build output path
199194
context.logger.debug(
200195
`Copying the prisma schema from ${this._resolvedSchemaPath} to ${schemaDestinationPath}`
@@ -215,8 +210,8 @@ export class PrismaExtension implements BuildExtension {
215210

216211
if (this.options.migrate) {
217212
// Copy the migrations directory to the build output path
218-
const migrationsDir = join(prismaDir, "migrations");
219-
const migrationsDestinationPath = join(manifest.outputPath, "prisma", "migrations");
213+
const migrationsDir = join(prismaSourceDir, "migrations");
214+
const migrationsDestinationPath = join(prismaDestinationDir, "migrations");
220215

221216
context.logger.debug(
222217
`Copying the prisma migrations from ${migrationsDir} to ${migrationsDestinationPath}`

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

references/v3-catalog/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@effect/schema": "^0.75.5",
2121
"@infisical/sdk": "^2.3.5",
2222
"@opentelemetry/api": "1.4.1",
23-
"@prisma/client": "5.19.0",
23+
"@prisma/client": "6.8.2",
2424
"@react-email/components": "0.0.24",
2525
"@react-email/render": "1.0.1",
2626
"@sentry/esbuild-plugin": "^2.22.2",
@@ -87,7 +87,7 @@
8787
"@types/fluent-ffmpeg": "^2.1.26",
8888
"@types/react": "^18.3.1",
8989
"esbuild": "^0.19.11",
90-
"prisma": "5.19.0",
90+
"prisma": "6.8.2",
9191
"prisma-kysely": "^1.8.0",
9292
"trigger.dev": "workspace:*",
9393
"ts-node": "^10.9.2",

references/v3-catalog/prisma/schema/schema.prisma renamed to references/v3-catalog/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
generator client {
88
provider = "prisma-client-js"
9-
previewFeatures = ["prismaSchemaFolder", "typedSql"]
9+
previewFeatures = ["typedSql"]
1010
}
1111

1212
datasource db {

references/v3-catalog/trigger.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default defineConfig({
4242
emitDecoratorMetadata(),
4343
audioWaveform(),
4444
prismaExtension({
45-
schema: "prisma/schema/schema.prisma",
45+
schema: "prisma",
4646
migrate: true,
4747
directUrlEnvVarName: "DATABASE_URL_UNPOOLED",
4848
clientGenerator: "client",

0 commit comments

Comments
 (0)