Skip to content

Commit ec249c4

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

File tree

8 files changed

+34
-30
lines changed

8 files changed

+34
-30
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

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 'fast-glob';
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(" ")}` // Don't add the --schema flag or this will fail
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.

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/schema.prisma",
4646
migrate: true,
4747
directUrlEnvVarName: "DATABASE_URL_UNPOOLED",
4848
clientGenerator: "client",

0 commit comments

Comments
 (0)