@contractspec/lib.schema 1.57.0 → 1.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/EnumType.d.ts +29 -31
- package/dist/EnumType.d.ts.map +1 -1
- package/dist/EnumType.js +38 -54
- package/dist/EnumType.test.d.ts +2 -0
- package/dist/EnumType.test.d.ts.map +1 -0
- package/dist/FieldType.d.ts +19 -23
- package/dist/FieldType.d.ts.map +1 -1
- package/dist/FieldType.js +41 -47
- package/dist/FieldType.test.d.ts +2 -0
- package/dist/FieldType.test.d.ts.map +1 -0
- package/dist/GraphQLSchemaType.d.ts +8 -12
- package/dist/GraphQLSchemaType.d.ts.map +1 -1
- package/dist/GraphQLSchemaType.js +16 -18
- package/dist/GraphQLSchemaType.test.d.ts +2 -0
- package/dist/GraphQLSchemaType.test.d.ts.map +1 -0
- package/dist/JsonSchemaType.d.ts +67 -63
- package/dist/JsonSchemaType.d.ts.map +1 -1
- package/dist/JsonSchemaType.js +73 -123
- package/dist/JsonSchemaType.test.d.ts +2 -0
- package/dist/JsonSchemaType.test.d.ts.map +1 -0
- package/dist/ScalarTypeEnum.d.ts +23 -27
- package/dist/ScalarTypeEnum.d.ts.map +1 -1
- package/dist/ScalarTypeEnum.js +235 -235
- package/dist/ScalarTypeEnum.test.d.ts +2 -0
- package/dist/ScalarTypeEnum.test.d.ts.map +1 -0
- package/dist/SchemaModel.d.ts +38 -39
- package/dist/SchemaModel.d.ts.map +1 -1
- package/dist/SchemaModel.js +28 -55
- package/dist/SchemaModel.test.d.ts +2 -0
- package/dist/SchemaModel.test.d.ts.map +1 -0
- package/dist/SchemaModelType.d.ts +28 -24
- package/dist/SchemaModelType.d.ts.map +1 -1
- package/dist/SchemaModelType.js +6 -19
- package/dist/SchemaModelType.test.d.ts +2 -0
- package/dist/SchemaModelType.test.d.ts.map +1 -0
- package/dist/ZodSchemaType.d.ts +44 -40
- package/dist/ZodSchemaType.d.ts.map +1 -1
- package/dist/ZodSchemaType.js +30 -101
- package/dist/ZodSchemaType.test.d.ts +2 -0
- package/dist/ZodSchemaType.test.d.ts.map +1 -0
- package/dist/browser/EnumType.js +40 -0
- package/dist/browser/FieldType.js +43 -0
- package/dist/browser/GraphQLSchemaType.js +18 -0
- package/dist/browser/JsonSchemaType.js +74 -0
- package/dist/browser/ScalarTypeEnum.js +236 -0
- package/dist/browser/SchemaModel.js +30 -0
- package/dist/browser/SchemaModelType.js +7 -0
- package/dist/browser/ZodSchemaType.js +31 -0
- package/dist/browser/entity/defineEntity.js +128 -0
- package/dist/browser/entity/generator.js +283 -0
- package/dist/browser/entity/index.js +410 -0
- package/dist/browser/entity/types.js +0 -0
- package/dist/browser/index.js +840 -0
- package/dist/entity/defineEntity.d.ts +55 -34
- package/dist/entity/defineEntity.d.ts.map +1 -1
- package/dist/entity/defineEntity.js +123 -231
- package/dist/entity/defineEntity.test.d.ts +2 -0
- package/dist/entity/defineEntity.test.d.ts.map +1 -0
- package/dist/entity/generator.d.ts +16 -20
- package/dist/entity/generator.d.ts.map +1 -1
- package/dist/entity/generator.js +262 -196
- package/dist/entity/generator.test.d.ts +2 -0
- package/dist/entity/generator.test.d.ts.map +1 -0
- package/dist/entity/index.d.ts +4 -4
- package/dist/entity/index.d.ts.map +1 -0
- package/dist/entity/index.js +410 -4
- package/dist/entity/types.d.ts +99 -103
- package/dist/entity/types.d.ts.map +1 -1
- package/dist/entity/types.js +1 -1
- package/dist/index.d.ts +10 -13
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +841 -13
- package/dist/node/EnumType.js +40 -0
- package/dist/node/FieldType.js +43 -0
- package/dist/node/GraphQLSchemaType.js +18 -0
- package/dist/node/JsonSchemaType.js +74 -0
- package/dist/node/ScalarTypeEnum.js +236 -0
- package/dist/node/SchemaModel.js +30 -0
- package/dist/node/SchemaModelType.js +7 -0
- package/dist/node/ZodSchemaType.js +31 -0
- package/dist/node/entity/defineEntity.js +128 -0
- package/dist/node/entity/generator.js +283 -0
- package/dist/node/entity/index.js +410 -0
- package/dist/node/entity/types.js +0 -0
- package/dist/node/index.js +840 -0
- package/package.json +123 -25
- package/dist/EnumType.js.map +0 -1
- package/dist/FieldType.js.map +0 -1
- package/dist/GraphQLSchemaType.js.map +0 -1
- package/dist/JsonSchemaType.js.map +0 -1
- package/dist/ScalarTypeEnum.js.map +0 -1
- package/dist/SchemaModel.js.map +0 -1
- package/dist/SchemaModelType.js.map +0 -1
- package/dist/ZodSchemaType.js.map +0 -1
- package/dist/entity/defineEntity.js.map +0 -1
- package/dist/entity/generator.js.map +0 -1
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// src/entity/generator.ts
|
|
2
|
+
function generatePrismaSchema(entities, options = {}) {
|
|
3
|
+
const {
|
|
4
|
+
provider = "postgresql",
|
|
5
|
+
clientOutput = "../../src/generated/prisma",
|
|
6
|
+
includePothos = true,
|
|
7
|
+
pothosOutput = "../../src/generated/pothos-types.ts"
|
|
8
|
+
} = options;
|
|
9
|
+
const lines = [];
|
|
10
|
+
const schemas = new Set;
|
|
11
|
+
entities.forEach((entity) => {
|
|
12
|
+
if (entity.schema) {
|
|
13
|
+
schemas.add(entity.schema);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const schemaList = schemas.size > 0 ? Array.from(schemas) : ["public"];
|
|
17
|
+
lines.push("datasource db {");
|
|
18
|
+
lines.push(` provider = "${provider}"`);
|
|
19
|
+
if (schemas.size > 0) {
|
|
20
|
+
lines.push(` schemas = [${schemaList.map((s) => `"${s}"`).join(", ")}]`);
|
|
21
|
+
}
|
|
22
|
+
lines.push("}");
|
|
23
|
+
lines.push("");
|
|
24
|
+
lines.push("generator client {");
|
|
25
|
+
lines.push(' provider = "prisma-client"');
|
|
26
|
+
lines.push(` output = "${clientOutput}"`);
|
|
27
|
+
lines.push("");
|
|
28
|
+
lines.push(' engineType = "client"');
|
|
29
|
+
lines.push(' runtime = "bun"');
|
|
30
|
+
lines.push(' moduleFormat = "esm"');
|
|
31
|
+
lines.push(' generatedFileExtension = "ts"');
|
|
32
|
+
lines.push(' importFileExtension = "ts"');
|
|
33
|
+
lines.push("}");
|
|
34
|
+
lines.push("");
|
|
35
|
+
if (includePothos) {
|
|
36
|
+
lines.push("generator pothos {");
|
|
37
|
+
lines.push(' provider = "prisma-pothos-types"');
|
|
38
|
+
lines.push(' clientOutput = "./prisma"');
|
|
39
|
+
lines.push(` output = "${pothosOutput}"`);
|
|
40
|
+
lines.push(" generateDatamodel = true");
|
|
41
|
+
lines.push(" documentation = false");
|
|
42
|
+
lines.push("}");
|
|
43
|
+
lines.push("");
|
|
44
|
+
}
|
|
45
|
+
const enumMap = new Map;
|
|
46
|
+
entities.forEach((entity) => {
|
|
47
|
+
entity.enums?.forEach((enumDef) => {
|
|
48
|
+
if (!enumMap.has(enumDef.name)) {
|
|
49
|
+
enumMap.set(enumDef.name, enumDef);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
Object.values(entity.fields).forEach((field) => {
|
|
53
|
+
if (field.kind === "enum" && field.values && !enumMap.has(field.enumName)) {
|
|
54
|
+
enumMap.set(field.enumName, {
|
|
55
|
+
name: field.enumName,
|
|
56
|
+
values: field.values,
|
|
57
|
+
schema: entity.schema
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
enumMap.forEach((enumDef) => {
|
|
63
|
+
lines.push(...generateEnumBlock(enumDef));
|
|
64
|
+
lines.push("");
|
|
65
|
+
});
|
|
66
|
+
entities.forEach((entity) => {
|
|
67
|
+
lines.push(...generateModelBlock(entity));
|
|
68
|
+
lines.push("");
|
|
69
|
+
});
|
|
70
|
+
return lines.join(`
|
|
71
|
+
`);
|
|
72
|
+
}
|
|
73
|
+
function generateEnumBlock(enumDef) {
|
|
74
|
+
const lines = [];
|
|
75
|
+
if (enumDef.description) {
|
|
76
|
+
lines.push(`/// ${enumDef.description}`);
|
|
77
|
+
}
|
|
78
|
+
lines.push(`enum ${enumDef.name} {`);
|
|
79
|
+
enumDef.values.forEach((value) => {
|
|
80
|
+
lines.push(` ${value}`);
|
|
81
|
+
});
|
|
82
|
+
lines.push("");
|
|
83
|
+
if (enumDef.schema) {
|
|
84
|
+
lines.push(` @@schema("${enumDef.schema}")`);
|
|
85
|
+
}
|
|
86
|
+
lines.push("}");
|
|
87
|
+
return lines;
|
|
88
|
+
}
|
|
89
|
+
function generateModelBlock(entity) {
|
|
90
|
+
const lines = [];
|
|
91
|
+
if (entity.description) {
|
|
92
|
+
lines.push(`/// ${entity.description}`);
|
|
93
|
+
}
|
|
94
|
+
lines.push(`model ${entity.name} {`);
|
|
95
|
+
Object.entries(entity.fields).forEach(([fieldName, field]) => {
|
|
96
|
+
const fieldLine = generateFieldLine(fieldName, field);
|
|
97
|
+
if (field.description) {
|
|
98
|
+
lines.push(` /// ${field.description}`);
|
|
99
|
+
}
|
|
100
|
+
lines.push(` ${fieldLine}`);
|
|
101
|
+
});
|
|
102
|
+
if (entity.indexes && entity.indexes.length > 0) {
|
|
103
|
+
lines.push("");
|
|
104
|
+
entity.indexes.forEach((idx) => {
|
|
105
|
+
lines.push(` ${generateIndexLine(idx)}`);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (entity.map) {
|
|
109
|
+
lines.push(` @@map("${entity.map}")`);
|
|
110
|
+
}
|
|
111
|
+
if (entity.schema) {
|
|
112
|
+
lines.push(` @@schema("${entity.schema}")`);
|
|
113
|
+
}
|
|
114
|
+
lines.push("}");
|
|
115
|
+
return lines;
|
|
116
|
+
}
|
|
117
|
+
function generateFieldLine(fieldName, field) {
|
|
118
|
+
if (field.kind === "scalar") {
|
|
119
|
+
return generateScalarFieldLine(fieldName, field);
|
|
120
|
+
} else if (field.kind === "enum") {
|
|
121
|
+
return generateEnumFieldLine(fieldName, field);
|
|
122
|
+
} else if (field.kind === "relation") {
|
|
123
|
+
return generateRelationFieldLine(fieldName, field);
|
|
124
|
+
}
|
|
125
|
+
throw new Error(`Unknown field kind: ${field.kind}`);
|
|
126
|
+
}
|
|
127
|
+
function generateScalarFieldLine(fieldName, field) {
|
|
128
|
+
const parts = [fieldName];
|
|
129
|
+
let typeStr = field.type;
|
|
130
|
+
if (field.isArray) {
|
|
131
|
+
typeStr += "[]";
|
|
132
|
+
}
|
|
133
|
+
if (field.isOptional) {
|
|
134
|
+
typeStr += "?";
|
|
135
|
+
}
|
|
136
|
+
parts.push(typeStr);
|
|
137
|
+
const attrs = [];
|
|
138
|
+
if (field.isId) {
|
|
139
|
+
attrs.push("@id");
|
|
140
|
+
}
|
|
141
|
+
if (field.default !== undefined) {
|
|
142
|
+
if (typeof field.default === "string" && field.default.includes("(")) {
|
|
143
|
+
attrs.push(`@default(${field.default})`);
|
|
144
|
+
} else if (typeof field.default === "boolean") {
|
|
145
|
+
attrs.push(`@default(${field.default})`);
|
|
146
|
+
} else if (typeof field.default === "number") {
|
|
147
|
+
attrs.push(`@default(${field.default})`);
|
|
148
|
+
} else {
|
|
149
|
+
attrs.push(`@default("${field.default}")`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (field.updatedAt) {
|
|
153
|
+
attrs.push("@updatedAt");
|
|
154
|
+
}
|
|
155
|
+
if (field.isUnique) {
|
|
156
|
+
attrs.push("@unique");
|
|
157
|
+
}
|
|
158
|
+
if (field.map) {
|
|
159
|
+
attrs.push(`@map("${field.map}")`);
|
|
160
|
+
}
|
|
161
|
+
if (field.dbType) {
|
|
162
|
+
attrs.push(`@db.${field.dbType}`);
|
|
163
|
+
}
|
|
164
|
+
if (attrs.length > 0) {
|
|
165
|
+
parts.push(attrs.join(" "));
|
|
166
|
+
}
|
|
167
|
+
return parts.join(" ");
|
|
168
|
+
}
|
|
169
|
+
function generateEnumFieldLine(fieldName, field) {
|
|
170
|
+
const parts = [fieldName];
|
|
171
|
+
let typeStr = field.enumName;
|
|
172
|
+
if (field.isArray) {
|
|
173
|
+
typeStr += "[]";
|
|
174
|
+
}
|
|
175
|
+
if (field.isOptional) {
|
|
176
|
+
typeStr += "?";
|
|
177
|
+
}
|
|
178
|
+
parts.push(typeStr);
|
|
179
|
+
const attrs = [];
|
|
180
|
+
if (field.default !== undefined) {
|
|
181
|
+
attrs.push(`@default(${field.default})`);
|
|
182
|
+
}
|
|
183
|
+
if (field.isUnique) {
|
|
184
|
+
attrs.push("@unique");
|
|
185
|
+
}
|
|
186
|
+
if (field.map) {
|
|
187
|
+
attrs.push(`@map("${field.map}")`);
|
|
188
|
+
}
|
|
189
|
+
if (attrs.length > 0) {
|
|
190
|
+
parts.push(attrs.join(" "));
|
|
191
|
+
}
|
|
192
|
+
return parts.join(" ");
|
|
193
|
+
}
|
|
194
|
+
function generateRelationFieldLine(fieldName, field) {
|
|
195
|
+
const parts = [fieldName];
|
|
196
|
+
let typeStr = field.target;
|
|
197
|
+
if (field.type === "hasMany") {
|
|
198
|
+
typeStr += "[]";
|
|
199
|
+
}
|
|
200
|
+
if (field.type === "hasOne") {
|
|
201
|
+
typeStr += "?";
|
|
202
|
+
}
|
|
203
|
+
parts.push(typeStr);
|
|
204
|
+
const relationParts = [];
|
|
205
|
+
if (field.name) {
|
|
206
|
+
relationParts.push(`name: "${field.name}"`);
|
|
207
|
+
}
|
|
208
|
+
if (field.fields && field.fields.length > 0) {
|
|
209
|
+
relationParts.push(`fields: [${field.fields.join(", ")}]`);
|
|
210
|
+
}
|
|
211
|
+
if (field.references && field.references.length > 0) {
|
|
212
|
+
relationParts.push(`references: [${field.references.join(", ")}]`);
|
|
213
|
+
}
|
|
214
|
+
if (field.onDelete) {
|
|
215
|
+
relationParts.push(`onDelete: ${field.onDelete}`);
|
|
216
|
+
}
|
|
217
|
+
if (field.onUpdate) {
|
|
218
|
+
relationParts.push(`onUpdate: ${field.onUpdate}`);
|
|
219
|
+
}
|
|
220
|
+
if (relationParts.length > 0) {
|
|
221
|
+
parts.push(`@relation(${relationParts.join(", ")})`);
|
|
222
|
+
}
|
|
223
|
+
return parts.join(" ");
|
|
224
|
+
}
|
|
225
|
+
function generateIndexLine(idx) {
|
|
226
|
+
const fieldList = idx.fields.join(", ");
|
|
227
|
+
const parts = [];
|
|
228
|
+
if (idx.unique) {
|
|
229
|
+
parts.push(`@@unique([${fieldList}]`);
|
|
230
|
+
} else {
|
|
231
|
+
parts.push(`@@index([${fieldList}]`);
|
|
232
|
+
}
|
|
233
|
+
const options = [];
|
|
234
|
+
if (idx.name) {
|
|
235
|
+
options.push(`name: "${idx.name}"`);
|
|
236
|
+
}
|
|
237
|
+
if (idx.type) {
|
|
238
|
+
options.push(`type: ${idx.type}`);
|
|
239
|
+
}
|
|
240
|
+
if (options.length > 0) {
|
|
241
|
+
parts[0] += `, ${options.join(", ")}`;
|
|
242
|
+
}
|
|
243
|
+
parts[0] += ")";
|
|
244
|
+
return parts.join("");
|
|
245
|
+
}
|
|
246
|
+
function composeModuleSchemas(contributions, options = {}) {
|
|
247
|
+
const allEntities = [];
|
|
248
|
+
const allEnums = new Map;
|
|
249
|
+
contributions.forEach((contrib) => {
|
|
250
|
+
contrib.entities.forEach((entity) => {
|
|
251
|
+
allEntities.push({
|
|
252
|
+
...entity,
|
|
253
|
+
module: contrib.moduleId
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
contrib.enums?.forEach((enumDef) => {
|
|
257
|
+
if (!allEnums.has(enumDef.name)) {
|
|
258
|
+
allEnums.set(enumDef.name, enumDef);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
if (allEntities[0] && allEnums.size > 0) {
|
|
263
|
+
allEntities[0] = {
|
|
264
|
+
...allEntities[0],
|
|
265
|
+
enums: [...allEntities[0].enums ?? [], ...allEnums.values()]
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
return generatePrismaSchema(allEntities, options);
|
|
269
|
+
}
|
|
270
|
+
function generateEntityFragment(entity) {
|
|
271
|
+
return generateModelBlock(entity).join(`
|
|
272
|
+
`);
|
|
273
|
+
}
|
|
274
|
+
function generateEnumFragment(enumDef) {
|
|
275
|
+
return generateEnumBlock(enumDef).join(`
|
|
276
|
+
`);
|
|
277
|
+
}
|
|
278
|
+
export {
|
|
279
|
+
generatePrismaSchema,
|
|
280
|
+
generateEnumFragment,
|
|
281
|
+
generateEntityFragment,
|
|
282
|
+
composeModuleSchemas
|
|
283
|
+
};
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
// src/entity/defineEntity.ts
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
function defineEntity(spec) {
|
|
4
|
+
return spec;
|
|
5
|
+
}
|
|
6
|
+
function defineEntityEnum(def) {
|
|
7
|
+
return def;
|
|
8
|
+
}
|
|
9
|
+
var field = {
|
|
10
|
+
string(opts) {
|
|
11
|
+
return { kind: "scalar", type: "String", ...opts };
|
|
12
|
+
},
|
|
13
|
+
int(opts) {
|
|
14
|
+
return { kind: "scalar", type: "Int", ...opts };
|
|
15
|
+
},
|
|
16
|
+
float(opts) {
|
|
17
|
+
return { kind: "scalar", type: "Float", ...opts };
|
|
18
|
+
},
|
|
19
|
+
boolean(opts) {
|
|
20
|
+
return { kind: "scalar", type: "Boolean", ...opts };
|
|
21
|
+
},
|
|
22
|
+
dateTime(opts) {
|
|
23
|
+
return { kind: "scalar", type: "DateTime", ...opts };
|
|
24
|
+
},
|
|
25
|
+
json(opts) {
|
|
26
|
+
return { kind: "scalar", type: "Json", ...opts };
|
|
27
|
+
},
|
|
28
|
+
bigInt(opts) {
|
|
29
|
+
return { kind: "scalar", type: "BigInt", ...opts };
|
|
30
|
+
},
|
|
31
|
+
decimal(opts) {
|
|
32
|
+
return { kind: "scalar", type: "Decimal", ...opts };
|
|
33
|
+
},
|
|
34
|
+
bytes(opts) {
|
|
35
|
+
return { kind: "scalar", type: "Bytes", ...opts };
|
|
36
|
+
},
|
|
37
|
+
id(opts) {
|
|
38
|
+
return {
|
|
39
|
+
kind: "scalar",
|
|
40
|
+
type: "String",
|
|
41
|
+
isId: true,
|
|
42
|
+
default: "cuid()",
|
|
43
|
+
...opts
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
uuid(opts) {
|
|
47
|
+
return {
|
|
48
|
+
kind: "scalar",
|
|
49
|
+
type: "String",
|
|
50
|
+
isId: true,
|
|
51
|
+
default: "uuid()",
|
|
52
|
+
...opts
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
autoIncrement(opts) {
|
|
56
|
+
return {
|
|
57
|
+
kind: "scalar",
|
|
58
|
+
type: "Int",
|
|
59
|
+
isId: true,
|
|
60
|
+
default: "autoincrement()",
|
|
61
|
+
...opts
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
createdAt(opts) {
|
|
65
|
+
return { kind: "scalar", type: "DateTime", default: "now()", ...opts };
|
|
66
|
+
},
|
|
67
|
+
updatedAt(opts) {
|
|
68
|
+
return { kind: "scalar", type: "DateTime", updatedAt: true, ...opts };
|
|
69
|
+
},
|
|
70
|
+
email(opts) {
|
|
71
|
+
return {
|
|
72
|
+
kind: "scalar",
|
|
73
|
+
type: "String",
|
|
74
|
+
zod: z.email(),
|
|
75
|
+
...opts
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
url(opts) {
|
|
79
|
+
return {
|
|
80
|
+
kind: "scalar",
|
|
81
|
+
type: "String",
|
|
82
|
+
zod: z.url(),
|
|
83
|
+
...opts
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
enum(enumName, opts) {
|
|
87
|
+
return { kind: "enum", enumName, ...opts };
|
|
88
|
+
},
|
|
89
|
+
inlineEnum(enumName, values, opts) {
|
|
90
|
+
return { kind: "enum", enumName, values, ...opts };
|
|
91
|
+
},
|
|
92
|
+
hasOne(target, opts) {
|
|
93
|
+
return { kind: "relation", type: "hasOne", target, ...opts };
|
|
94
|
+
},
|
|
95
|
+
hasMany(target, opts) {
|
|
96
|
+
return { kind: "relation", type: "hasMany", target, ...opts };
|
|
97
|
+
},
|
|
98
|
+
belongsTo(target, fields, references, opts) {
|
|
99
|
+
return {
|
|
100
|
+
kind: "relation",
|
|
101
|
+
type: "belongsTo",
|
|
102
|
+
target,
|
|
103
|
+
fields,
|
|
104
|
+
references,
|
|
105
|
+
...opts
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
foreignKey(opts) {
|
|
109
|
+
return { kind: "scalar", type: "String", ...opts };
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var index = {
|
|
113
|
+
on(fields, opts) {
|
|
114
|
+
return { fields, ...opts };
|
|
115
|
+
},
|
|
116
|
+
unique(fields, opts) {
|
|
117
|
+
return { fields, unique: true, ...opts };
|
|
118
|
+
},
|
|
119
|
+
compound(fields, sort, opts) {
|
|
120
|
+
return { fields, sort, ...opts };
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/entity/generator.ts
|
|
125
|
+
function generatePrismaSchema(entities, options = {}) {
|
|
126
|
+
const {
|
|
127
|
+
provider = "postgresql",
|
|
128
|
+
clientOutput = "../../src/generated/prisma",
|
|
129
|
+
includePothos = true,
|
|
130
|
+
pothosOutput = "../../src/generated/pothos-types.ts"
|
|
131
|
+
} = options;
|
|
132
|
+
const lines = [];
|
|
133
|
+
const schemas = new Set;
|
|
134
|
+
entities.forEach((entity) => {
|
|
135
|
+
if (entity.schema) {
|
|
136
|
+
schemas.add(entity.schema);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
const schemaList = schemas.size > 0 ? Array.from(schemas) : ["public"];
|
|
140
|
+
lines.push("datasource db {");
|
|
141
|
+
lines.push(` provider = "${provider}"`);
|
|
142
|
+
if (schemas.size > 0) {
|
|
143
|
+
lines.push(` schemas = [${schemaList.map((s) => `"${s}"`).join(", ")}]`);
|
|
144
|
+
}
|
|
145
|
+
lines.push("}");
|
|
146
|
+
lines.push("");
|
|
147
|
+
lines.push("generator client {");
|
|
148
|
+
lines.push(' provider = "prisma-client"');
|
|
149
|
+
lines.push(` output = "${clientOutput}"`);
|
|
150
|
+
lines.push("");
|
|
151
|
+
lines.push(' engineType = "client"');
|
|
152
|
+
lines.push(' runtime = "bun"');
|
|
153
|
+
lines.push(' moduleFormat = "esm"');
|
|
154
|
+
lines.push(' generatedFileExtension = "ts"');
|
|
155
|
+
lines.push(' importFileExtension = "ts"');
|
|
156
|
+
lines.push("}");
|
|
157
|
+
lines.push("");
|
|
158
|
+
if (includePothos) {
|
|
159
|
+
lines.push("generator pothos {");
|
|
160
|
+
lines.push(' provider = "prisma-pothos-types"');
|
|
161
|
+
lines.push(' clientOutput = "./prisma"');
|
|
162
|
+
lines.push(` output = "${pothosOutput}"`);
|
|
163
|
+
lines.push(" generateDatamodel = true");
|
|
164
|
+
lines.push(" documentation = false");
|
|
165
|
+
lines.push("}");
|
|
166
|
+
lines.push("");
|
|
167
|
+
}
|
|
168
|
+
const enumMap = new Map;
|
|
169
|
+
entities.forEach((entity) => {
|
|
170
|
+
entity.enums?.forEach((enumDef) => {
|
|
171
|
+
if (!enumMap.has(enumDef.name)) {
|
|
172
|
+
enumMap.set(enumDef.name, enumDef);
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
Object.values(entity.fields).forEach((field2) => {
|
|
176
|
+
if (field2.kind === "enum" && field2.values && !enumMap.has(field2.enumName)) {
|
|
177
|
+
enumMap.set(field2.enumName, {
|
|
178
|
+
name: field2.enumName,
|
|
179
|
+
values: field2.values,
|
|
180
|
+
schema: entity.schema
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
enumMap.forEach((enumDef) => {
|
|
186
|
+
lines.push(...generateEnumBlock(enumDef));
|
|
187
|
+
lines.push("");
|
|
188
|
+
});
|
|
189
|
+
entities.forEach((entity) => {
|
|
190
|
+
lines.push(...generateModelBlock(entity));
|
|
191
|
+
lines.push("");
|
|
192
|
+
});
|
|
193
|
+
return lines.join(`
|
|
194
|
+
`);
|
|
195
|
+
}
|
|
196
|
+
function generateEnumBlock(enumDef) {
|
|
197
|
+
const lines = [];
|
|
198
|
+
if (enumDef.description) {
|
|
199
|
+
lines.push(`/// ${enumDef.description}`);
|
|
200
|
+
}
|
|
201
|
+
lines.push(`enum ${enumDef.name} {`);
|
|
202
|
+
enumDef.values.forEach((value) => {
|
|
203
|
+
lines.push(` ${value}`);
|
|
204
|
+
});
|
|
205
|
+
lines.push("");
|
|
206
|
+
if (enumDef.schema) {
|
|
207
|
+
lines.push(` @@schema("${enumDef.schema}")`);
|
|
208
|
+
}
|
|
209
|
+
lines.push("}");
|
|
210
|
+
return lines;
|
|
211
|
+
}
|
|
212
|
+
function generateModelBlock(entity) {
|
|
213
|
+
const lines = [];
|
|
214
|
+
if (entity.description) {
|
|
215
|
+
lines.push(`/// ${entity.description}`);
|
|
216
|
+
}
|
|
217
|
+
lines.push(`model ${entity.name} {`);
|
|
218
|
+
Object.entries(entity.fields).forEach(([fieldName, field2]) => {
|
|
219
|
+
const fieldLine = generateFieldLine(fieldName, field2);
|
|
220
|
+
if (field2.description) {
|
|
221
|
+
lines.push(` /// ${field2.description}`);
|
|
222
|
+
}
|
|
223
|
+
lines.push(` ${fieldLine}`);
|
|
224
|
+
});
|
|
225
|
+
if (entity.indexes && entity.indexes.length > 0) {
|
|
226
|
+
lines.push("");
|
|
227
|
+
entity.indexes.forEach((idx) => {
|
|
228
|
+
lines.push(` ${generateIndexLine(idx)}`);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
if (entity.map) {
|
|
232
|
+
lines.push(` @@map("${entity.map}")`);
|
|
233
|
+
}
|
|
234
|
+
if (entity.schema) {
|
|
235
|
+
lines.push(` @@schema("${entity.schema}")`);
|
|
236
|
+
}
|
|
237
|
+
lines.push("}");
|
|
238
|
+
return lines;
|
|
239
|
+
}
|
|
240
|
+
function generateFieldLine(fieldName, field2) {
|
|
241
|
+
if (field2.kind === "scalar") {
|
|
242
|
+
return generateScalarFieldLine(fieldName, field2);
|
|
243
|
+
} else if (field2.kind === "enum") {
|
|
244
|
+
return generateEnumFieldLine(fieldName, field2);
|
|
245
|
+
} else if (field2.kind === "relation") {
|
|
246
|
+
return generateRelationFieldLine(fieldName, field2);
|
|
247
|
+
}
|
|
248
|
+
throw new Error(`Unknown field kind: ${field2.kind}`);
|
|
249
|
+
}
|
|
250
|
+
function generateScalarFieldLine(fieldName, field2) {
|
|
251
|
+
const parts = [fieldName];
|
|
252
|
+
let typeStr = field2.type;
|
|
253
|
+
if (field2.isArray) {
|
|
254
|
+
typeStr += "[]";
|
|
255
|
+
}
|
|
256
|
+
if (field2.isOptional) {
|
|
257
|
+
typeStr += "?";
|
|
258
|
+
}
|
|
259
|
+
parts.push(typeStr);
|
|
260
|
+
const attrs = [];
|
|
261
|
+
if (field2.isId) {
|
|
262
|
+
attrs.push("@id");
|
|
263
|
+
}
|
|
264
|
+
if (field2.default !== undefined) {
|
|
265
|
+
if (typeof field2.default === "string" && field2.default.includes("(")) {
|
|
266
|
+
attrs.push(`@default(${field2.default})`);
|
|
267
|
+
} else if (typeof field2.default === "boolean") {
|
|
268
|
+
attrs.push(`@default(${field2.default})`);
|
|
269
|
+
} else if (typeof field2.default === "number") {
|
|
270
|
+
attrs.push(`@default(${field2.default})`);
|
|
271
|
+
} else {
|
|
272
|
+
attrs.push(`@default("${field2.default}")`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (field2.updatedAt) {
|
|
276
|
+
attrs.push("@updatedAt");
|
|
277
|
+
}
|
|
278
|
+
if (field2.isUnique) {
|
|
279
|
+
attrs.push("@unique");
|
|
280
|
+
}
|
|
281
|
+
if (field2.map) {
|
|
282
|
+
attrs.push(`@map("${field2.map}")`);
|
|
283
|
+
}
|
|
284
|
+
if (field2.dbType) {
|
|
285
|
+
attrs.push(`@db.${field2.dbType}`);
|
|
286
|
+
}
|
|
287
|
+
if (attrs.length > 0) {
|
|
288
|
+
parts.push(attrs.join(" "));
|
|
289
|
+
}
|
|
290
|
+
return parts.join(" ");
|
|
291
|
+
}
|
|
292
|
+
function generateEnumFieldLine(fieldName, field2) {
|
|
293
|
+
const parts = [fieldName];
|
|
294
|
+
let typeStr = field2.enumName;
|
|
295
|
+
if (field2.isArray) {
|
|
296
|
+
typeStr += "[]";
|
|
297
|
+
}
|
|
298
|
+
if (field2.isOptional) {
|
|
299
|
+
typeStr += "?";
|
|
300
|
+
}
|
|
301
|
+
parts.push(typeStr);
|
|
302
|
+
const attrs = [];
|
|
303
|
+
if (field2.default !== undefined) {
|
|
304
|
+
attrs.push(`@default(${field2.default})`);
|
|
305
|
+
}
|
|
306
|
+
if (field2.isUnique) {
|
|
307
|
+
attrs.push("@unique");
|
|
308
|
+
}
|
|
309
|
+
if (field2.map) {
|
|
310
|
+
attrs.push(`@map("${field2.map}")`);
|
|
311
|
+
}
|
|
312
|
+
if (attrs.length > 0) {
|
|
313
|
+
parts.push(attrs.join(" "));
|
|
314
|
+
}
|
|
315
|
+
return parts.join(" ");
|
|
316
|
+
}
|
|
317
|
+
function generateRelationFieldLine(fieldName, field2) {
|
|
318
|
+
const parts = [fieldName];
|
|
319
|
+
let typeStr = field2.target;
|
|
320
|
+
if (field2.type === "hasMany") {
|
|
321
|
+
typeStr += "[]";
|
|
322
|
+
}
|
|
323
|
+
if (field2.type === "hasOne") {
|
|
324
|
+
typeStr += "?";
|
|
325
|
+
}
|
|
326
|
+
parts.push(typeStr);
|
|
327
|
+
const relationParts = [];
|
|
328
|
+
if (field2.name) {
|
|
329
|
+
relationParts.push(`name: "${field2.name}"`);
|
|
330
|
+
}
|
|
331
|
+
if (field2.fields && field2.fields.length > 0) {
|
|
332
|
+
relationParts.push(`fields: [${field2.fields.join(", ")}]`);
|
|
333
|
+
}
|
|
334
|
+
if (field2.references && field2.references.length > 0) {
|
|
335
|
+
relationParts.push(`references: [${field2.references.join(", ")}]`);
|
|
336
|
+
}
|
|
337
|
+
if (field2.onDelete) {
|
|
338
|
+
relationParts.push(`onDelete: ${field2.onDelete}`);
|
|
339
|
+
}
|
|
340
|
+
if (field2.onUpdate) {
|
|
341
|
+
relationParts.push(`onUpdate: ${field2.onUpdate}`);
|
|
342
|
+
}
|
|
343
|
+
if (relationParts.length > 0) {
|
|
344
|
+
parts.push(`@relation(${relationParts.join(", ")})`);
|
|
345
|
+
}
|
|
346
|
+
return parts.join(" ");
|
|
347
|
+
}
|
|
348
|
+
function generateIndexLine(idx) {
|
|
349
|
+
const fieldList = idx.fields.join(", ");
|
|
350
|
+
const parts = [];
|
|
351
|
+
if (idx.unique) {
|
|
352
|
+
parts.push(`@@unique([${fieldList}]`);
|
|
353
|
+
} else {
|
|
354
|
+
parts.push(`@@index([${fieldList}]`);
|
|
355
|
+
}
|
|
356
|
+
const options = [];
|
|
357
|
+
if (idx.name) {
|
|
358
|
+
options.push(`name: "${idx.name}"`);
|
|
359
|
+
}
|
|
360
|
+
if (idx.type) {
|
|
361
|
+
options.push(`type: ${idx.type}`);
|
|
362
|
+
}
|
|
363
|
+
if (options.length > 0) {
|
|
364
|
+
parts[0] += `, ${options.join(", ")}`;
|
|
365
|
+
}
|
|
366
|
+
parts[0] += ")";
|
|
367
|
+
return parts.join("");
|
|
368
|
+
}
|
|
369
|
+
function composeModuleSchemas(contributions, options = {}) {
|
|
370
|
+
const allEntities = [];
|
|
371
|
+
const allEnums = new Map;
|
|
372
|
+
contributions.forEach((contrib) => {
|
|
373
|
+
contrib.entities.forEach((entity) => {
|
|
374
|
+
allEntities.push({
|
|
375
|
+
...entity,
|
|
376
|
+
module: contrib.moduleId
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
contrib.enums?.forEach((enumDef) => {
|
|
380
|
+
if (!allEnums.has(enumDef.name)) {
|
|
381
|
+
allEnums.set(enumDef.name, enumDef);
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
if (allEntities[0] && allEnums.size > 0) {
|
|
386
|
+
allEntities[0] = {
|
|
387
|
+
...allEntities[0],
|
|
388
|
+
enums: [...allEntities[0].enums ?? [], ...allEnums.values()]
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
return generatePrismaSchema(allEntities, options);
|
|
392
|
+
}
|
|
393
|
+
function generateEntityFragment(entity) {
|
|
394
|
+
return generateModelBlock(entity).join(`
|
|
395
|
+
`);
|
|
396
|
+
}
|
|
397
|
+
function generateEnumFragment(enumDef) {
|
|
398
|
+
return generateEnumBlock(enumDef).join(`
|
|
399
|
+
`);
|
|
400
|
+
}
|
|
401
|
+
export {
|
|
402
|
+
index,
|
|
403
|
+
generatePrismaSchema,
|
|
404
|
+
generateEnumFragment,
|
|
405
|
+
generateEntityFragment,
|
|
406
|
+
field,
|
|
407
|
+
defineEntityEnum,
|
|
408
|
+
defineEntity,
|
|
409
|
+
composeModuleSchemas
|
|
410
|
+
};
|
|
File without changes
|