-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
prisma-schema.ts
245 lines (231 loc) · 8.19 KB
/
prisma-schema.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
import { ScalarDBField, ScalarDBFieldDefault, DatabaseProvider } from '../../types';
import { ResolvedDBField, ListsWithResolvedRelations } from './resolve-relationships';
import { getDBFieldKeyForFieldOnMultiField } from './utils';
function areArraysEqual(a: unknown[], b: unknown[]) {
if (a.length !== b.length) {
return false;
}
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
const modifiers = {
required: '',
optional: '?',
many: '[]',
};
function printIndex(fieldPath: string, index: undefined | 'index' | 'unique') {
return {
none: '',
unique: '@unique',
index: `\n@@index([${fieldPath}])`,
}[index || ('none' as const)];
}
function printNativeType(nativeType: string | undefined, datasourceName: string) {
return nativeType === undefined ? '' : ` @${datasourceName}.${nativeType}`;
}
function printScalarDefaultValue(defaultValue: ScalarDBFieldDefault): string {
if (defaultValue.kind === 'literal') {
if (typeof defaultValue.value === 'string') {
return JSON.stringify(defaultValue.value);
}
return defaultValue.value.toString();
}
if (
defaultValue.kind === 'now' ||
defaultValue.kind === 'autoincrement' ||
defaultValue.kind === 'cuid' ||
defaultValue.kind === 'uuid'
) {
return `${defaultValue.kind}()`;
}
if (defaultValue.kind === 'dbgenerated') {
return `dbgenerated(${JSON.stringify(defaultValue.value)})`;
}
assertNever(defaultValue);
}
function assertNever(arg: never): never {
throw new Error(`expected to never be called but was called with ${arg}`);
}
function printField(
fieldPath: string,
field: Exclude<ResolvedDBField, { kind: 'none' }>,
datasourceName: string,
lists: ListsWithResolvedRelations
): string {
if (field.kind === 'scalar') {
const nativeType = printNativeType(field.nativeType, datasourceName);
const index = printIndex(fieldPath, field.index);
const defaultValue = field.default
? ` @default(${printScalarDefaultValue(field.default)})`
: '';
return `${fieldPath} ${field.scalar}${
modifiers[field.mode]
}${nativeType}${defaultValue}${index}`;
}
if (field.kind === 'enum') {
const index = printIndex(fieldPath, field.index);
const defaultValue = field.default ? ` @default(${field.default})` : '';
return `${fieldPath} ${field.name}${modifiers[field.mode]}${defaultValue}${index}`;
}
if (field.kind === 'multi') {
return Object.entries(field.fields)
.map(([subField, field]) =>
printField(
getDBFieldKeyForFieldOnMultiField(fieldPath, subField),
field,
datasourceName,
lists
)
)
.join('\n');
}
if (field.kind === 'relation') {
if (field.mode === 'many') {
return `${fieldPath} ${field.list}[] @relation("${field.relationName}")`;
}
if (field.foreignIdField === 'none') {
return `${fieldPath} ${field.list}? @relation("${field.relationName}")`;
}
const relationIdFieldPath = `${fieldPath}Id`;
const relationField = `${fieldPath} ${field.list}? @relation("${field.relationName}", fields: [${relationIdFieldPath}], references: [id])`;
const foreignIdField = lists[field.list].resolvedDbFields.id;
assertDbFieldIsValidForIdField(field.list, foreignIdField);
const nativeType = printNativeType(foreignIdField.nativeType, datasourceName);
const index = printIndex(
relationIdFieldPath,
field.foreignIdField === 'owned' ? 'index' : 'unique'
);
const relationIdField = `${relationIdFieldPath} ${foreignIdField.scalar}? @map("${fieldPath}") ${nativeType}${index}`;
return `${relationField}\n${relationIdField}`;
}
// TypeScript's control flow analysis doesn't understand that this will never happen without the assertNever
// (this will still correctly validate if any case is unhandled though)
return assertNever(field);
}
function collectEnums(lists: ListsWithResolvedRelations) {
const enums: Record<string, { values: string[]; firstDefinedByRef: string }> = {};
for (const [listKey, { resolvedDbFields }] of Object.entries(lists)) {
for (const [fieldPath, field] of Object.entries(resolvedDbFields)) {
const fields =
field.kind === 'multi'
? Object.entries(field.fields).map(
([key, field]) => [field, `${listKey}.${fieldPath} (sub field ${key})`] as const
)
: [[field, `${listKey}.${fieldPath}`] as const];
for (const [field, ref] of fields) {
if (field.kind !== 'enum') continue;
const alreadyExistingEnum = enums[field.name];
if (alreadyExistingEnum === undefined) {
enums[field.name] = {
values: field.values,
firstDefinedByRef: ref,
};
continue;
}
if (!areArraysEqual(alreadyExistingEnum.values, field.values)) {
throw new Error(
`The fields ${alreadyExistingEnum.firstDefinedByRef} and ${ref} both specify Prisma schema enums` +
`with the name ${field.name} but they have different values:\n` +
`enum from ${alreadyExistingEnum.firstDefinedByRef}:\n${JSON.stringify(
alreadyExistingEnum.values,
null,
2
)}\n` +
`enum from ${ref}:\n${JSON.stringify(field.values, null, 2)}`
);
}
}
}
}
return Object.entries(enums)
.map(([enumName, { values }]) => `enum ${enumName} {\n${values.join('\n')}\n}`)
.join('\n');
}
function assertDbFieldIsValidForIdField(
listKey: string,
field: ResolvedDBField
): asserts field is ScalarDBField<'Int' | 'String', 'required'> {
if (field.kind !== 'scalar') {
throw new Error(
`id fields must be either a String or Int Prisma scalar but the id field for the ${listKey} list is not a scalar`
);
}
// this may be loosened in the future
if (field.scalar !== 'String' && field.scalar !== 'Int') {
throw new Error(
`id fields must be either String or Int Prisma scalars but the id field for the ${listKey} list is a ${field.scalar} scalar`
);
}
if (field.mode !== 'required') {
throw new Error(
`id fields must be a singular required field but the id field for the ${listKey} list is ${
field.mode === 'many' ? 'a many' : 'an optional'
} field`
);
}
if (field.index !== undefined) {
throw new Error(
`id fields must not specify indexes themselves but the id field for the ${listKey} list specifies an index`
);
}
// this will likely be loosened in the future
if (field.default === undefined) {
throw new Error(
`id fields must specify a Prisma/database level default value but the id field for the ${listKey} list does not`
);
}
}
export function printPrismaSchema(
lists: ListsWithResolvedRelations,
provider: DatabaseProvider,
clientDir: string,
prismaPreviewFeatures?: string[]
) {
let prismaFlags = '';
if (prismaPreviewFeatures && prismaPreviewFeatures.length) {
prismaFlags = `\n previewFeatures = ["${prismaPreviewFeatures.join('","')}"]`;
}
let prismaSchema = `// This file is automatically generated by Keystone, do not modify it manually.
// Modify your Keystone config when you want to change this.
datasource ${provider} {
url = env("DATABASE_URL")
provider = "${provider}"
}\n\n`;
if (provider === 'mongodb') {
prismaSchema += `generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
output = "${clientDir}"
engineType = "binary"
}
\n`;
} else {
prismaSchema += `generator client {
provider = "prisma-client-js"
output = "${clientDir}"
}
\n`;
}
for (const [listKey, { resolvedDbFields }] of Object.entries(lists)) {
prismaSchema += `model ${listKey} {`;
for (const [fieldPath, field] of Object.entries(resolvedDbFields)) {
if (field.kind !== 'none') {
prismaSchema += '\n' + printField(fieldPath, field, provider, lists);
}
if (fieldPath === 'id') {
assertDbFieldIsValidForIdField(listKey, field);
prismaSchema += ' @id';
if (provider === 'mongodb') {
prismaSchema += ' @map("_id")';
}
}
}
prismaSchema += `\n}\n`;
}
prismaSchema += `\n${collectEnums(lists)}\n`;
return prismaSchema;
}