-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
generateClient.ts
682 lines (603 loc) · 20.6 KB
/
generateClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
import Debug from '@prisma/debug'
import { overwriteFile } from '@prisma/fetch-engine'
import type { BinaryPaths, ConnectorType, DataSource, DMMF, GeneratorConfig } from '@prisma/generator-helper'
import { assertNever, ClientEngineType, getClientEngineType, pathToPosix, setClassName } from '@prisma/internals'
import { createHash } from 'crypto'
import paths from 'env-paths'
import { existsSync } from 'fs'
import fs from 'fs/promises'
import { ensureDir } from 'fs-extra'
import { bold, dim, green, red } from 'kleur/colors'
import path from 'path'
import pkgUp from 'pkg-up'
import { type O } from 'ts-toolbelt'
import clientPkg from '../../package.json'
import type { DMMF as PrismaClientDMMF } from './dmmf-types'
import { getPrismaClientDMMF } from './getDMMF'
import { BrowserJS, JS, TS, TSClient } from './TSClient'
import { TSClientOptions } from './TSClient/TSClient'
import type { Dictionary } from './utils/common'
const debug = Debug('prisma:client:generateClient')
type OutputDeclaration = {
content: string
lineNumber: number
}
export class DenylistError extends Error {
constructor(message: string) {
super(message)
this.stack = undefined
}
}
setClassName(DenylistError, 'DenylistError')
export interface GenerateClientOptions {
datamodel: string
schemaPath: string
/** Runtime path used in runtime/type imports */
runtimeBase?: string
outputDir: string
generator: GeneratorConfig
dmmf: DMMF.Document
datasources: DataSource[]
binaryPaths: BinaryPaths
testMode?: boolean
copyRuntime?: boolean
copyRuntimeSourceMaps?: boolean
engineVersion: string
clientVersion: string
activeProvider: string
/** When --postinstall is passed via CLI */
postinstall?: boolean
/** When --no-engine is passed via CLI */
copyEngine?: boolean
}
export interface BuildClientResult {
fileMap: Dictionary<string>
prismaClientDmmf: PrismaClientDMMF.Document
}
// eslint-disable-next-line @typescript-eslint/require-await
export async function buildClient({
schemaPath,
runtimeBase,
datamodel,
binaryPaths,
outputDir,
generator,
dmmf,
datasources,
engineVersion,
clientVersion,
activeProvider,
postinstall,
copyEngine,
}: O.Required<GenerateClientOptions, 'runtimeBase'>): Promise<BuildClientResult> {
// we define the basic options for the client generation
const clientEngineType = getClientEngineType(generator)
const baseClientOptions: Omit<TSClientOptions, `runtimeName${'Js' | 'Ts'}`> = {
dmmf: getPrismaClientDMMF(dmmf),
datasources,
generator,
binaryPaths,
schemaPath,
outputDir,
runtimeBase,
clientVersion,
engineVersion,
activeProvider,
postinstall,
copyEngine,
datamodel,
browser: false,
deno: false,
edge: false,
wasm: false,
}
const nodeClientOptions = {
...baseClientOptions,
runtimeNameJs: getNodeRuntimeName(clientEngineType),
runtimeNameTs: `${getNodeRuntimeName(clientEngineType)}.js`,
}
// we create a regular client that is fit for Node.js
const nodeClient = new TSClient(nodeClientOptions)
const defaultClient = new TSClient({
...nodeClientOptions,
reusedTs: 'index',
reusedJs: '.',
})
// we create a client that is fit for edge runtimes
const edgeClient = new TSClient({
...baseClientOptions,
runtimeNameJs: 'edge',
runtimeNameTs: 'library.js',
reusedTs: 'default',
edge: true,
})
const trampolineTsClient = new TSClient({
...nodeClientOptions,
reusedTs: 'index',
reusedJs: '#main-entry-point',
})
// order of keys is important here. bundler/runtime will
// match the first one they recognize, so it is important
// to go from more specific to more generic.
const exportsMapBase = {
node: './index.js',
'edge-light': './wasm.js',
workerd: './wasm.js',
worker: './wasm.js',
browser: './index-browser.js',
default: './index.js',
}
const exportsMapDefault = {
require: exportsMapBase,
import: exportsMapBase,
default: exportsMapBase.default,
}
const pkgJson = {
name: getUniquePackageName(datamodel),
main: 'index.js',
types: 'index.d.ts',
browser: 'index-browser.js',
exports: {
...clientPkg.exports,
// TODO: remove on DA ga
...{ '.': exportsMapDefault },
},
version: clientVersion,
sideEffects: false,
}
// we store the generated contents here
const fileMap: Record<string, string> = {}
fileMap['index.js'] = await JS(nodeClient)
fileMap['index.d.ts'] = await TS(nodeClient)
fileMap['default.js'] = await JS(defaultClient)
fileMap['default.d.ts'] = await TS(defaultClient)
fileMap['index-browser.js'] = await BrowserJS(nodeClient)
fileMap['edge.js'] = await JS(edgeClient)
fileMap['edge.d.ts'] = await TS(edgeClient)
if (generator.previewFeatures.includes('driverAdapters')) {
// The trampoline client points to #main-entry-point (see below). We use
// imports similar to an exports map to ensure correct imports.❗ Before
// going GA, please notify @millsp as some things can be cleaned up:
// - defaultClient can be deleted since trampolineTsClient will replace it.
// - Special handling of . paths in TSClient.ts can also be removed.
// - The main @prisma/client exports map can be simplified:
// - Everything can point to `default.js`, including browser fields.
// - Exports map's `.` entry can be made like the others (e.g. `./edge`).
// - exportsMapDefault can be deleted as it's only needed for defaultClient:
// - #main-entry-point can handle all the heavy lifting on its own.
// - Always using #main-entry-point is kept for GA (small breaking change).
// - exportsMapDefault can be inlined down below and MUST be removed elsewhere.
// In short: A lot can be simplified, but can only happen in GA & P6.
fileMap['default.js'] = await JS(trampolineTsClient)
fileMap['default.d.ts'] = await TS(trampolineTsClient)
fileMap['wasm-worker-loader.js'] = `export default (await import('./query_engine_bg.wasm')).default`
fileMap['wasm-edge-light-loader.js'] = `export default (await import('./query_engine_bg.wasm?module')).default`
pkgJson['browser'] = 'default.js' // also point to the trampoline client otherwise it is picked up by cfw
pkgJson['imports'] = {
// when `import('#wasm-engine-loader')` is called, it will be resolved to the correct file
'#wasm-engine-loader': {
'edge-light': './wasm-edge-light-loader.js',
workerd: './wasm-worker-loader.js',
worker: './wasm-worker-loader.js',
default: './wasm-worker-loader.js',
},
// when `require('#main-entry-point')` is called, it will be resolved to the correct file
'#main-entry-point': exportsMapDefault,
}
const wasmClient = new TSClient({
...baseClientOptions,
runtimeNameJs: 'wasm',
runtimeNameTs: 'library.js',
reusedTs: 'default',
edge: true,
wasm: true,
})
fileMap['wasm.js'] = await JS(wasmClient)
fileMap['wasm.d.ts'] = await TS(wasmClient)
} else {
fileMap['wasm.js'] = fileMap['index-browser.js']
fileMap['wasm.d.ts'] = fileMap['default.d.ts']
}
if (generator.previewFeatures.includes('deno') && !!globalThis.Deno) {
// we create a client that is fit for edge runtimes
const denoEdgeClient = new TSClient({
...baseClientOptions,
runtimeBase: `../${runtimeBase}`,
runtimeNameJs: 'edge-esm',
runtimeNameTs: 'library.d.ts',
deno: true,
edge: true,
})
fileMap['deno/edge.js'] = await JS(denoEdgeClient)
fileMap['deno/index.d.ts'] = await TS(denoEdgeClient)
fileMap['deno/edge.ts'] = `
import './polyfill.js'
// @deno-types="./index.d.ts"
export * from './edge.js'`
fileMap['deno/polyfill.js'] = 'globalThis.process = { env: Deno.env.toObject() }; globalThis.global = globalThis'
}
fileMap['package.json'] = JSON.stringify(pkgJson, null, 2)
return {
fileMap, // a map of file names to their contents
prismaClientDmmf: dmmf, // the DMMF document
}
}
// TODO: explore why we have a special case for excluding pnpm
async function getDefaultOutdir(outputDir: string): Promise<string> {
if (outputDir.endsWith('node_modules/@prisma/client')) {
return path.join(outputDir, '../../.prisma/client')
}
if (
process.env.INIT_CWD &&
process.env.npm_lifecycle_event === 'postinstall' &&
!process.env.PWD?.includes('.pnpm')
) {
// INIT_CWD is the dir, in which "npm install" has been invoked. That can e.g. be in ./src
// If we're in ./ - there'll also be a package.json, so we can directly go for it
// otherwise, we'll go up in the filesystem and look for the first package.json
if (existsSync(path.join(process.env.INIT_CWD, 'package.json'))) {
return path.join(process.env.INIT_CWD, 'node_modules/.prisma/client')
}
const packagePath = await pkgUp({ cwd: process.env.INIT_CWD })
if (packagePath) {
return path.join(path.dirname(packagePath), 'node_modules/.prisma/client')
}
}
return path.join(outputDir, '../../.prisma/client')
}
export async function generateClient(options: GenerateClientOptions): Promise<void> {
const {
datamodel,
schemaPath,
generator,
dmmf,
datasources,
binaryPaths,
testMode,
copyRuntime,
copyRuntimeSourceMaps = false,
clientVersion,
engineVersion,
activeProvider,
postinstall,
copyEngine = true,
} = options
const clientEngineType = getClientEngineType(generator)
const { runtimeBase, outputDir } = await getGenerationDirs(options)
const { prismaClientDmmf, fileMap } = await buildClient({
datamodel,
schemaPath,
runtimeBase,
outputDir,
generator,
dmmf,
datasources,
binaryPaths,
clientVersion,
engineVersion,
activeProvider,
postinstall,
copyEngine,
testMode,
})
const provider = datasources[0].provider
const denylistsErrors = validateDmmfAgainstDenylists(prismaClientDmmf)
if (denylistsErrors) {
let message = `${bold(
red('Error: '),
)}The schema at "${schemaPath}" contains reserved keywords.\n Rename the following items:`
for (const error of denylistsErrors) {
message += '\n - ' + error.message
}
message += `\nTo learn more about how to rename models, check out https://pris.ly/d/naming-models`
throw new DenylistError(message)
}
if (!copyEngine) {
await deleteOutputDir(outputDir)
}
await ensureDir(outputDir)
if (generator.previewFeatures.includes('deno') && !!globalThis.Deno) {
await ensureDir(path.join(outputDir, 'deno'))
}
await Promise.all(
Object.entries(fileMap).map(async ([fileName, file]) => {
const filePath = path.join(outputDir, fileName)
// The deletion of the file is necessary, so VSCode
// picks up the changes.
if (existsSync(filePath)) {
await fs.unlink(filePath)
}
await fs.writeFile(filePath, file)
}),
)
const runtimeDir = path.join(__dirname, `${testMode ? '../' : ''}../runtime`)
// if users use a custom output dir
if (copyRuntime || generator.isCustomOutput === true) {
const copiedRuntimeDir = path.join(outputDir, 'runtime')
await ensureDir(copiedRuntimeDir)
await copyRuntimeFiles({
from: runtimeDir,
to: copiedRuntimeDir,
sourceMaps: copyRuntimeSourceMaps,
runtimeName: getNodeRuntimeName(clientEngineType),
})
}
const enginePath =
clientEngineType === ClientEngineType.Library ? binaryPaths.libqueryEngine : binaryPaths.queryEngine
if (!enginePath) {
throw new Error(
`Prisma Client needs \`${
clientEngineType === ClientEngineType.Library ? 'libqueryEngine' : 'queryEngine'
}\` in the \`binaryPaths\` object.`,
)
}
if (copyEngine) {
if (process.env.NETLIFY) {
await ensureDir('/tmp/prisma-engines')
}
for (const [binaryTarget, filePath] of Object.entries(enginePath)) {
const fileName = path.basename(filePath)
let target: string
// Introduced in https://github.com/prisma/prisma/pull/6527
// The engines that are not needed for the runtime deployment on AWS Lambda
// are moved to `/tmp/prisma-engines`
// They will be ignored and not included in the final build, reducing its size
if (process.env.NETLIFY && !['rhel-openssl-1.0.x', 'rhel-openssl-3.0.x'].includes(binaryTarget)) {
target = path.join('/tmp/prisma-engines', fileName)
} else {
target = path.join(outputDir, fileName)
}
await overwriteFile(filePath, target)
}
}
const schemaTargetPath = path.join(outputDir, 'schema.prisma')
if (schemaPath !== schemaTargetPath) {
await fs.copyFile(schemaPath, schemaTargetPath)
}
// copy the necessary engine files needed for the wasm/driver-adapter engine
if (
generator.previewFeatures.includes('driverAdapters') &&
isWasmEngineSupported(provider) &&
copyEngine &&
!testMode
) {
const suffix = provider === 'postgres' ? 'postgresql' : provider
await fs.copyFile(
path.join(runtimeDir, `query_engine_bg.${suffix}.wasm`),
path.join(outputDir, `query_engine_bg.wasm`),
)
await fs.copyFile(path.join(runtimeDir, `query_engine_bg.${suffix}.js`), path.join(outputDir, `query_engine_bg.js`))
}
try {
// we tell our vscode extension to reload the types by modifying this file
const prismaCache = paths('prisma').cache
const signalsPath = path.join(prismaCache, 'last-generate')
await fs.mkdir(prismaCache, { recursive: true })
await fs.writeFile(signalsPath, Date.now().toString())
} catch {}
}
function isWasmEngineSupported(provider: ConnectorType) {
return provider === 'postgresql' || provider === 'postgres' || provider === 'mysql' || provider === 'sqlite'
}
function validateDmmfAgainstDenylists(prismaClientDmmf: PrismaClientDMMF.Document): Error[] | null {
const errorArray = [] as Error[]
const denylists = {
// A copy of this list is also in prisma-engines. Any edit should be done in both places.
// https://github.com/prisma/prisma-engines/blob/main/psl/parser-database/src/names/reserved_model_names.rs
models: [
// Reserved Prisma keywords
'PrismaClient',
'Prisma',
// JavaScript keywords
'break',
'case',
'catch',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'implements',
'import',
'in',
'instanceof',
'interface',
'let',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typeof',
'var',
'void',
'while',
'with',
'yield',
],
fields: ['AND', 'OR', 'NOT'],
dynamic: [],
}
if (prismaClientDmmf.datamodel.enums) {
for (const it of prismaClientDmmf.datamodel.enums) {
if (denylists.models.includes(it.name) || denylists.fields.includes(it.name)) {
errorArray.push(Error(`"enum ${it.name}"`))
}
}
}
if (prismaClientDmmf.datamodel.models) {
for (const it of prismaClientDmmf.datamodel.models) {
if (denylists.models.includes(it.name) || denylists.fields.includes(it.name)) {
errorArray.push(Error(`"model ${it.name}"`))
}
}
}
return errorArray.length > 0 ? errorArray : null
}
/**
* Get all the directories involved in the generation process.
*
* @returns
*/
async function getGenerationDirs({
runtimeBase,
generator,
outputDir,
datamodel,
schemaPath,
testMode,
}: GenerateClientOptions) {
const isCustomOutput = generator.isCustomOutput === true
let userRuntimeImport = isCustomOutput ? './runtime' : '@prisma/client/runtime'
let userOutputDir = isCustomOutput ? outputDir : await getDefaultOutdir(outputDir)
if (testMode && runtimeBase) {
userOutputDir = outputDir
userRuntimeImport = pathToPosix(runtimeBase)
}
if (isCustomOutput) {
await verifyOutputDirectory(userOutputDir, datamodel, schemaPath)
}
const userPackageRoot = await pkgUp({ cwd: path.dirname(userOutputDir) })
const userProjectRoot = userPackageRoot ? path.dirname(userPackageRoot) : process.cwd()
return {
runtimeBase: userRuntimeImport,
outputDir: userOutputDir,
projectRoot: userProjectRoot,
}
}
async function verifyOutputDirectory(directory: string, datamodel: string, schemaPath: string) {
let content: string
try {
content = await fs.readFile(path.join(directory, 'package.json'), 'utf8')
} catch (e) {
if (e.code === 'ENOENT') {
// no package.json exists, we are good
return
}
throw e
}
const { name } = JSON.parse(content)
if (name === clientPkg.name) {
const message = [`Generating client into ${bold(directory)} is not allowed.`]
message.push('This package is used by `prisma generate` and overwriting its content is dangerous.')
message.push('')
message.push('Suggestion:')
const outputDeclaration = findOutputPathDeclaration(datamodel)
if (outputDeclaration && outputDeclaration.content.includes(clientPkg.name)) {
const outputLine = outputDeclaration.content
message.push(`In ${bold(schemaPath)} replace:`)
message.push('')
message.push(`${dim(outputDeclaration.lineNumber)} ${replacePackageName(outputLine, red(clientPkg.name))}`)
message.push('with')
message.push(`${dim(outputDeclaration.lineNumber)} ${replacePackageName(outputLine, green('.prisma/client'))}`)
} else {
message.push(`Generate client into ${bold(replacePackageName(directory, green('.prisma/client')))} instead`)
}
message.push('')
message.push("You won't need to change your imports.")
message.push('Imports from `@prisma/client` will be automatically forwarded to `.prisma/client`')
const error = new Error(message.join('\n'))
throw error
}
}
function replacePackageName(directoryPath: string, replacement: string): string {
return directoryPath.replace(clientPkg.name, replacement)
}
function findOutputPathDeclaration(datamodel: string): OutputDeclaration | null {
const lines = datamodel.split(/\r?\n/)
for (const [i, line] of lines.entries()) {
if (/output\s*=/.test(line)) {
return { lineNumber: i + 1, content: line.trim() }
}
}
return null
}
function getNodeRuntimeName(engineType: ClientEngineType) {
if (engineType === ClientEngineType.Binary) {
return 'binary'
}
if (engineType === ClientEngineType.Library) {
return 'library'
}
assertNever(engineType, 'Unknown engine type')
}
type CopyRuntimeOptions = {
from: string
to: string
runtimeName: string
sourceMaps: boolean
}
async function copyRuntimeFiles({ from, to, runtimeName, sourceMaps }: CopyRuntimeOptions) {
const files = [
// library.d.ts is always included, as it contains the actual runtime type
// definitions. Rest of the `runtime.d.ts` files just re-export everything
// from `library.d.ts`
'library.d.ts',
'index-browser.js',
'index-browser.d.ts',
'edge.js',
'edge-esm.js',
'wasm.js',
]
files.push(`${runtimeName}.js`)
if (runtimeName !== 'library') {
files.push(`${runtimeName}.d.ts`)
}
if (sourceMaps) {
files.push(...files.filter((file) => file.endsWith('.js')).map((file) => `${file}.map`))
}
await Promise.all(files.map((file) => fs.copyFile(path.join(from, file), path.join(to, file))))
}
/**
* Attempts to delete the output directory.
* @param outputDir
*/
async function deleteOutputDir(outputDir: string) {
try {
debug(`attempting to delete ${outputDir} recursively`)
// we want to make sure that if we delete, we delete the right directory
if (require(`${outputDir}/package.json`).name?.startsWith(GENERATED_PACKAGE_NAME_PREFIX)) {
await fs.rmdir(outputDir, { recursive: true }).catch(() => {
debug(`failed to delete ${outputDir} recursively`)
})
}
} catch {
debug(`failed to delete ${outputDir} recursively, not found`)
}
}
/**
* This function ensures that each generated client has unique package name
* It appends sha256 of the schema to the fixed prefix. That ensures unique schemas
* produce unique generated packages while still keeping `generate` results reproducible.
*
* Without unique package name, if you have several TS clients in the project, TS Compiler
* might merge different `Prisma` namespace declarations together and produce unusable results.
*
* @param datamodel
* @returns
*/
function getUniquePackageName(datamodel: string) {
const hash = createHash('sha256')
hash.write(datamodel)
return `${GENERATED_PACKAGE_NAME_PREFIX}${hash.digest().toString('hex')}`
}
const GENERATED_PACKAGE_NAME_PREFIX = 'prisma-client-'