@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.
Files changed (96) hide show
  1. package/dist/EnumType.d.ts +29 -31
  2. package/dist/EnumType.d.ts.map +1 -1
  3. package/dist/EnumType.js +38 -54
  4. package/dist/EnumType.test.d.ts +2 -0
  5. package/dist/EnumType.test.d.ts.map +1 -0
  6. package/dist/FieldType.d.ts +19 -23
  7. package/dist/FieldType.d.ts.map +1 -1
  8. package/dist/FieldType.js +41 -47
  9. package/dist/FieldType.test.d.ts +2 -0
  10. package/dist/FieldType.test.d.ts.map +1 -0
  11. package/dist/GraphQLSchemaType.d.ts +8 -12
  12. package/dist/GraphQLSchemaType.d.ts.map +1 -1
  13. package/dist/GraphQLSchemaType.js +16 -18
  14. package/dist/GraphQLSchemaType.test.d.ts +2 -0
  15. package/dist/GraphQLSchemaType.test.d.ts.map +1 -0
  16. package/dist/JsonSchemaType.d.ts +67 -63
  17. package/dist/JsonSchemaType.d.ts.map +1 -1
  18. package/dist/JsonSchemaType.js +73 -123
  19. package/dist/JsonSchemaType.test.d.ts +2 -0
  20. package/dist/JsonSchemaType.test.d.ts.map +1 -0
  21. package/dist/ScalarTypeEnum.d.ts +23 -27
  22. package/dist/ScalarTypeEnum.d.ts.map +1 -1
  23. package/dist/ScalarTypeEnum.js +235 -235
  24. package/dist/ScalarTypeEnum.test.d.ts +2 -0
  25. package/dist/ScalarTypeEnum.test.d.ts.map +1 -0
  26. package/dist/SchemaModel.d.ts +38 -39
  27. package/dist/SchemaModel.d.ts.map +1 -1
  28. package/dist/SchemaModel.js +28 -55
  29. package/dist/SchemaModel.test.d.ts +2 -0
  30. package/dist/SchemaModel.test.d.ts.map +1 -0
  31. package/dist/SchemaModelType.d.ts +28 -24
  32. package/dist/SchemaModelType.d.ts.map +1 -1
  33. package/dist/SchemaModelType.js +6 -19
  34. package/dist/SchemaModelType.test.d.ts +2 -0
  35. package/dist/SchemaModelType.test.d.ts.map +1 -0
  36. package/dist/ZodSchemaType.d.ts +44 -40
  37. package/dist/ZodSchemaType.d.ts.map +1 -1
  38. package/dist/ZodSchemaType.js +30 -101
  39. package/dist/ZodSchemaType.test.d.ts +2 -0
  40. package/dist/ZodSchemaType.test.d.ts.map +1 -0
  41. package/dist/browser/EnumType.js +40 -0
  42. package/dist/browser/FieldType.js +43 -0
  43. package/dist/browser/GraphQLSchemaType.js +18 -0
  44. package/dist/browser/JsonSchemaType.js +74 -0
  45. package/dist/browser/ScalarTypeEnum.js +236 -0
  46. package/dist/browser/SchemaModel.js +30 -0
  47. package/dist/browser/SchemaModelType.js +7 -0
  48. package/dist/browser/ZodSchemaType.js +31 -0
  49. package/dist/browser/entity/defineEntity.js +128 -0
  50. package/dist/browser/entity/generator.js +283 -0
  51. package/dist/browser/entity/index.js +410 -0
  52. package/dist/browser/entity/types.js +0 -0
  53. package/dist/browser/index.js +840 -0
  54. package/dist/entity/defineEntity.d.ts +55 -34
  55. package/dist/entity/defineEntity.d.ts.map +1 -1
  56. package/dist/entity/defineEntity.js +123 -231
  57. package/dist/entity/defineEntity.test.d.ts +2 -0
  58. package/dist/entity/defineEntity.test.d.ts.map +1 -0
  59. package/dist/entity/generator.d.ts +16 -20
  60. package/dist/entity/generator.d.ts.map +1 -1
  61. package/dist/entity/generator.js +262 -196
  62. package/dist/entity/generator.test.d.ts +2 -0
  63. package/dist/entity/generator.test.d.ts.map +1 -0
  64. package/dist/entity/index.d.ts +4 -4
  65. package/dist/entity/index.d.ts.map +1 -0
  66. package/dist/entity/index.js +410 -4
  67. package/dist/entity/types.d.ts +99 -103
  68. package/dist/entity/types.d.ts.map +1 -1
  69. package/dist/entity/types.js +1 -1
  70. package/dist/index.d.ts +10 -13
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +841 -13
  73. package/dist/node/EnumType.js +40 -0
  74. package/dist/node/FieldType.js +43 -0
  75. package/dist/node/GraphQLSchemaType.js +18 -0
  76. package/dist/node/JsonSchemaType.js +74 -0
  77. package/dist/node/ScalarTypeEnum.js +236 -0
  78. package/dist/node/SchemaModel.js +30 -0
  79. package/dist/node/SchemaModelType.js +7 -0
  80. package/dist/node/ZodSchemaType.js +31 -0
  81. package/dist/node/entity/defineEntity.js +128 -0
  82. package/dist/node/entity/generator.js +283 -0
  83. package/dist/node/entity/index.js +410 -0
  84. package/dist/node/entity/types.js +0 -0
  85. package/dist/node/index.js +840 -0
  86. package/package.json +123 -25
  87. package/dist/EnumType.js.map +0 -1
  88. package/dist/FieldType.js.map +0 -1
  89. package/dist/GraphQLSchemaType.js.map +0 -1
  90. package/dist/JsonSchemaType.js.map +0 -1
  91. package/dist/ScalarTypeEnum.js.map +0 -1
  92. package/dist/SchemaModel.js.map +0 -1
  93. package/dist/SchemaModelType.js.map +0 -1
  94. package/dist/ZodSchemaType.js.map +0 -1
  95. package/dist/entity/defineEntity.js.map +0 -1
  96. package/dist/entity/generator.js.map +0 -1
@@ -0,0 +1,840 @@
1
+ // src/EnumType.ts
2
+ import * as z from "zod";
3
+ import { GraphQLEnumType } from "graphql";
4
+
5
+ class EnumType {
6
+ name;
7
+ values;
8
+ gqlEnum;
9
+ constructor(name, values) {
10
+ this.name = name;
11
+ this.values = values;
12
+ this.gqlEnum = new GraphQLEnumType({
13
+ name: this.name,
14
+ values: Object.fromEntries(values.map((v) => [v, { value: v }]))
15
+ });
16
+ }
17
+ getName() {
18
+ return this.name;
19
+ }
20
+ getEnumValues() {
21
+ return this.values;
22
+ }
23
+ getPothos() {
24
+ return this.gqlEnum;
25
+ }
26
+ getZod() {
27
+ return z.enum(this.values);
28
+ }
29
+ getJson() {
30
+ return { type: "string", enum: this.values };
31
+ }
32
+ getJsonSchema() {
33
+ return this.getJson();
34
+ }
35
+ }
36
+ var defineEnum = (name, values) => new EnumType(name, values);
37
+
38
+ // src/FieldType.ts
39
+ import { GraphQLScalarType } from "graphql";
40
+
41
+ class FieldType extends GraphQLScalarType {
42
+ zodSchema;
43
+ jsonSchemaDef;
44
+ constructor(config) {
45
+ super(config);
46
+ this.zodSchema = config.zod;
47
+ this.jsonSchemaDef = config.jsonSchema;
48
+ }
49
+ getZod() {
50
+ return this.zodSchema;
51
+ }
52
+ getPothos() {
53
+ return this;
54
+ }
55
+ getJson() {
56
+ return typeof this.jsonSchemaDef === "function" ? this.jsonSchemaDef() : this.jsonSchemaDef;
57
+ }
58
+ getJsonSchemaDef() {
59
+ return this.jsonSchemaDef;
60
+ }
61
+ getJsonSchema() {
62
+ const deepResolve = (v) => {
63
+ const value = typeof v === "function" ? v() : v;
64
+ if (Array.isArray(value))
65
+ return value.map((item) => deepResolve(item));
66
+ if (value && typeof value === "object") {
67
+ const obj = {};
68
+ for (const [k, val] of Object.entries(value)) {
69
+ obj[k] = deepResolve(val);
70
+ }
71
+ return obj;
72
+ }
73
+ return value;
74
+ };
75
+ return deepResolve(this.getJson());
76
+ }
77
+ }
78
+
79
+ // src/GraphQLSchemaType.ts
80
+ import { z as z2 } from "zod";
81
+
82
+ class GraphQLSchemaType {
83
+ typeDef;
84
+ name;
85
+ _isSchemaType = true;
86
+ constructor(typeDef, name) {
87
+ this.typeDef = typeDef;
88
+ this.name = name;
89
+ }
90
+ getZod() {
91
+ return z2.unknown();
92
+ }
93
+ }
94
+
95
+ // src/JsonSchemaType.ts
96
+ import * as z3 from "zod";
97
+
98
+ class JsonSchemaType {
99
+ jsonSchema;
100
+ options;
101
+ constructor(jsonSchema, options = {}) {
102
+ this.jsonSchema = jsonSchema;
103
+ this.options = options;
104
+ }
105
+ getZod() {
106
+ if (this.jsonSchema.additionalProperties !== undefined) {
107
+ if (this.jsonSchema.additionalProperties === true) {
108
+ return z3.record(z3.string(), z3.unknown());
109
+ }
110
+ if (typeof this.jsonSchema.additionalProperties === "object") {
111
+ return z3.record(z3.string(), z3.unknown());
112
+ }
113
+ if (this.jsonSchema.additionalProperties === false) {
114
+ return z3.object({}).strict();
115
+ }
116
+ }
117
+ if (this.jsonSchema.properties) {
118
+ const shape = {};
119
+ const required = new Set(this.jsonSchema.required ?? []);
120
+ for (const [key, propSchema] of Object.entries(this.jsonSchema.properties)) {
121
+ const fieldType = this.convertPropertyToZod(propSchema);
122
+ shape[key] = required.has(key) ? fieldType : fieldType.optional();
123
+ }
124
+ return z3.object(shape).passthrough();
125
+ }
126
+ return z3.object({}).passthrough();
127
+ }
128
+ getJsonSchema() {
129
+ return this.jsonSchema;
130
+ }
131
+ getPothos() {
132
+ return { type: "JSON", name: this.options.name };
133
+ }
134
+ getName() {
135
+ return this.options.name;
136
+ }
137
+ convertPropertyToZod(schema) {
138
+ const type = Array.isArray(schema.type) ? schema.type[0] : schema.type;
139
+ switch (type) {
140
+ case "string":
141
+ return z3.string();
142
+ case "number":
143
+ case "integer":
144
+ return z3.number();
145
+ case "boolean":
146
+ return z3.boolean();
147
+ case "array":
148
+ if (schema.items && !Array.isArray(schema.items)) {
149
+ return z3.array(this.convertPropertyToZod(schema.items));
150
+ }
151
+ return z3.array(z3.unknown());
152
+ case "object":
153
+ if (schema.properties) {
154
+ return new JsonSchemaType(schema).getZod();
155
+ }
156
+ return z3.record(z3.string(), z3.unknown());
157
+ case "null":
158
+ return z3.null();
159
+ default:
160
+ return z3.unknown();
161
+ }
162
+ }
163
+ }
164
+ var fromJsonSchema = (schema, options) => new JsonSchemaType(schema, options);
165
+
166
+ // src/ScalarTypeEnum.ts
167
+ import * as z4 from "zod";
168
+ import { Kind } from "graphql";
169
+ var localeRegex = /^[A-Za-z]{2}(?:-[A-Za-z0-9]{2,8})*$/;
170
+ var timezoneRegex = /^(?:UTC|[A-Za-z_]+\/[A-Za-z_]+)$/;
171
+ var phoneRegex = /^[+]?\d[\d\s().-]{3,}$/;
172
+ var currencyRegex = /^[A-Z]{3}$/;
173
+ var countryRegex = /^[A-Z]{2}$/;
174
+ var latMin = -90;
175
+ var latMax = 90;
176
+ var lonMin = -180;
177
+ var lonMax = 180;
178
+ var ScalarTypeEnum = {
179
+ String_unsecure: () => new FieldType({
180
+ name: "String_unsecure",
181
+ description: "Unvalidated string scalar",
182
+ zod: z4.string(),
183
+ parseValue: (v) => z4.string().parse(v),
184
+ serialize: (v) => String(v),
185
+ parseLiteral: (ast) => {
186
+ if (ast.kind !== Kind.STRING)
187
+ throw new TypeError("Invalid literal");
188
+ return ast.value;
189
+ },
190
+ jsonSchema: { type: "string" }
191
+ }),
192
+ Int_unsecure: () => new FieldType({
193
+ name: "Int_unsecure",
194
+ description: "Unvalidated integer scalar",
195
+ zod: z4.number().int(),
196
+ parseValue: (v) => {
197
+ const num = typeof v === "number" ? v : Number(v);
198
+ return z4.number().int().parse(num);
199
+ },
200
+ serialize: (v) => Math.trunc(typeof v === "number" ? v : Number(v)),
201
+ parseLiteral: (ast) => {
202
+ if (ast.kind !== Kind.INT)
203
+ throw new TypeError("Invalid literal");
204
+ return Number(ast.value);
205
+ },
206
+ jsonSchema: { type: "integer" }
207
+ }),
208
+ Float_unsecure: () => new FieldType({
209
+ name: "Float_unsecure",
210
+ description: "Unvalidated float scalar",
211
+ zod: z4.number(),
212
+ parseValue: (v) => {
213
+ const num = typeof v === "number" ? v : Number(v);
214
+ return z4.number().parse(num);
215
+ },
216
+ serialize: (v) => Number(v),
217
+ parseLiteral: (ast) => {
218
+ if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT)
219
+ throw new TypeError("Invalid literal");
220
+ return Number(ast.value);
221
+ },
222
+ jsonSchema: { type: "number" }
223
+ }),
224
+ Boolean: () => new FieldType({
225
+ name: "Boolean",
226
+ description: "Unvalidated boolean scalar",
227
+ zod: z4.boolean(),
228
+ parseValue: (v) => z4.coerce.boolean().parse(v),
229
+ serialize: (v) => Boolean(v),
230
+ parseLiteral: (ast) => {
231
+ if (ast.kind !== Kind.BOOLEAN)
232
+ throw new TypeError("Invalid literal");
233
+ return ast.value;
234
+ },
235
+ jsonSchema: { type: "boolean" }
236
+ }),
237
+ ID: () => new FieldType({
238
+ name: "ID",
239
+ description: "Unvalidated id scalar",
240
+ zod: z4.string(),
241
+ parseValue: (v) => z4.string().parse(v),
242
+ serialize: (v) => String(v),
243
+ parseLiteral: (ast) => {
244
+ if (ast.kind !== Kind.STRING)
245
+ throw new TypeError("Invalid literal");
246
+ return ast.value;
247
+ },
248
+ jsonSchema: { type: "string" }
249
+ }),
250
+ JSON: () => new FieldType({
251
+ name: "JSON",
252
+ zod: z4.any(),
253
+ parseValue: (v) => v,
254
+ serialize: (v) => v,
255
+ jsonSchema: {}
256
+ }),
257
+ JSONObject: () => new FieldType({
258
+ name: "JSONObject",
259
+ zod: z4.record(z4.string(), z4.any()),
260
+ parseValue: (v) => z4.record(z4.string(), z4.any()).parse(v),
261
+ serialize: (v) => v ?? {},
262
+ jsonSchema: { type: "object" }
263
+ }),
264
+ Date: () => new FieldType({
265
+ name: "Date",
266
+ zod: z4.date(),
267
+ parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
268
+ serialize: (v) => v instanceof Date ? v.toISOString().split("T")[0] : String(v),
269
+ jsonSchema: { type: "string", format: "date" }
270
+ }),
271
+ DateTime: () => new FieldType({
272
+ name: "DateTime",
273
+ zod: z4.date(),
274
+ parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
275
+ serialize: (v) => {
276
+ return v instanceof Date ? v.toISOString() : String(v);
277
+ },
278
+ jsonSchema: { type: "string", format: "date-time" }
279
+ }),
280
+ Time: () => new FieldType({
281
+ name: "Time",
282
+ zod: z4.string().regex(/^\d{2}:\d{2}(:\d{2})?$/),
283
+ parseValue: (v) => z4.string().regex(/^\d{2}:\d{2}(:\d{2})?$/).parse(v),
284
+ serialize: (v) => String(v),
285
+ jsonSchema: { type: "string", pattern: "^\\d{2}:\\d{2}(:\\d{2})?$" }
286
+ }),
287
+ EmailAddress: () => new FieldType({
288
+ name: "EmailAddress",
289
+ zod: z4.string().email(),
290
+ parseValue: (v) => z4.string().email().parse(v),
291
+ serialize: (v) => String(v),
292
+ jsonSchema: { type: "string", format: "email" }
293
+ }),
294
+ URL: () => new FieldType({
295
+ name: "URL",
296
+ zod: z4.string().url(),
297
+ parseValue: (v) => z4.string().url().parse(v),
298
+ serialize: (v) => String(v),
299
+ jsonSchema: { type: "string", format: "uri" }
300
+ }),
301
+ PhoneNumber: () => new FieldType({
302
+ name: "PhoneNumber",
303
+ zod: z4.string().regex(phoneRegex),
304
+ parseValue: (v) => z4.string().regex(phoneRegex).parse(v),
305
+ serialize: (v) => String(v),
306
+ jsonSchema: { type: "string", pattern: phoneRegex.source }
307
+ }),
308
+ NonEmptyString: () => new FieldType({
309
+ name: "NonEmptyString",
310
+ zod: z4.string().min(1),
311
+ parseValue: (v) => z4.string().min(1).parse(v),
312
+ serialize: (v) => String(v),
313
+ jsonSchema: { type: "string", minLength: 1 }
314
+ }),
315
+ Locale: () => new FieldType({
316
+ name: "Locale",
317
+ zod: z4.string().regex(localeRegex),
318
+ parseValue: (v) => z4.string().regex(localeRegex).parse(v),
319
+ serialize: (v) => String(v),
320
+ jsonSchema: { type: "string", pattern: localeRegex.source }
321
+ }),
322
+ TimeZone: () => new FieldType({
323
+ name: "TimeZone",
324
+ zod: z4.string().regex(timezoneRegex),
325
+ parseValue: (v) => z4.string().regex(timezoneRegex).parse(v),
326
+ serialize: (v) => String(v),
327
+ jsonSchema: { type: "string", pattern: timezoneRegex.source }
328
+ }),
329
+ Latitude: () => new FieldType({
330
+ name: "Latitude",
331
+ zod: z4.number().min(latMin).max(latMax),
332
+ parseValue: (v) => z4.coerce.number().min(latMin).max(latMax).parse(v),
333
+ serialize: (v) => Number(v),
334
+ jsonSchema: { type: "number", minimum: latMin, maximum: latMax }
335
+ }),
336
+ Longitude: () => new FieldType({
337
+ name: "Longitude",
338
+ zod: z4.number().min(lonMin).max(lonMax),
339
+ parseValue: (v) => z4.coerce.number().min(lonMin).max(lonMax).parse(v),
340
+ serialize: (v) => Number(v),
341
+ jsonSchema: { type: "number", minimum: lonMin, maximum: lonMax }
342
+ }),
343
+ Currency: () => new FieldType({
344
+ name: "Currency",
345
+ zod: z4.string().regex(currencyRegex),
346
+ parseValue: (v) => z4.string().regex(currencyRegex).parse(v),
347
+ serialize: (v) => String(v),
348
+ jsonSchema: { type: "string", pattern: currencyRegex.source }
349
+ }),
350
+ CountryCode: () => new FieldType({
351
+ name: "CountryCode",
352
+ zod: z4.string().regex(countryRegex),
353
+ parseValue: (v) => z4.string().regex(countryRegex).parse(v),
354
+ serialize: (v) => String(v),
355
+ jsonSchema: { type: "string", pattern: countryRegex.source }
356
+ })
357
+ };
358
+
359
+ // src/SchemaModel.ts
360
+ import * as z5 from "zod";
361
+
362
+ class SchemaModel {
363
+ config;
364
+ constructor(config) {
365
+ this.config = config;
366
+ }
367
+ getZod() {
368
+ const shape = Object.entries(this.config.fields).reduce((acc, [key, def]) => {
369
+ const base = def.type.getZod();
370
+ const withArray = def.isArray ? z5.array(base) : base;
371
+ acc[key] = def.isOptional ? withArray.optional() : withArray;
372
+ return acc;
373
+ }, {});
374
+ return z5.object(shape);
375
+ }
376
+ getPothosInput() {
377
+ return this.config.name;
378
+ }
379
+ }
380
+ function isSchemaModel(model) {
381
+ return model !== null && model !== undefined && "config" in model && typeof model.config === "object" && "name" in model.config;
382
+ }
383
+ var defineSchemaModel = (config) => new SchemaModel(config);
384
+
385
+ // src/SchemaModelType.ts
386
+ function isSchemaType(value) {
387
+ return value !== null && typeof value === "object" && "getZod" in value && typeof value.getZod === "function";
388
+ }
389
+
390
+ // src/ZodSchemaType.ts
391
+ import * as z6 from "zod";
392
+
393
+ class ZodSchemaType {
394
+ schema;
395
+ options;
396
+ constructor(schema, options = {}) {
397
+ this.schema = schema;
398
+ this.options = options;
399
+ }
400
+ getZod() {
401
+ return this.schema;
402
+ }
403
+ getJsonSchema() {
404
+ return z6.toJSONSchema(this.schema);
405
+ }
406
+ getPothos() {
407
+ return { type: "JSON", name: this.options.name };
408
+ }
409
+ getName() {
410
+ return this.options.name;
411
+ }
412
+ getDescription() {
413
+ return this.options.description;
414
+ }
415
+ }
416
+ var fromZod = (schema, options) => new ZodSchemaType(schema, options);
417
+
418
+ // src/entity/defineEntity.ts
419
+ import * as z7 from "zod";
420
+ function defineEntity(spec) {
421
+ return spec;
422
+ }
423
+ function defineEntityEnum(def) {
424
+ return def;
425
+ }
426
+ var field = {
427
+ string(opts) {
428
+ return { kind: "scalar", type: "String", ...opts };
429
+ },
430
+ int(opts) {
431
+ return { kind: "scalar", type: "Int", ...opts };
432
+ },
433
+ float(opts) {
434
+ return { kind: "scalar", type: "Float", ...opts };
435
+ },
436
+ boolean(opts) {
437
+ return { kind: "scalar", type: "Boolean", ...opts };
438
+ },
439
+ dateTime(opts) {
440
+ return { kind: "scalar", type: "DateTime", ...opts };
441
+ },
442
+ json(opts) {
443
+ return { kind: "scalar", type: "Json", ...opts };
444
+ },
445
+ bigInt(opts) {
446
+ return { kind: "scalar", type: "BigInt", ...opts };
447
+ },
448
+ decimal(opts) {
449
+ return { kind: "scalar", type: "Decimal", ...opts };
450
+ },
451
+ bytes(opts) {
452
+ return { kind: "scalar", type: "Bytes", ...opts };
453
+ },
454
+ id(opts) {
455
+ return {
456
+ kind: "scalar",
457
+ type: "String",
458
+ isId: true,
459
+ default: "cuid()",
460
+ ...opts
461
+ };
462
+ },
463
+ uuid(opts) {
464
+ return {
465
+ kind: "scalar",
466
+ type: "String",
467
+ isId: true,
468
+ default: "uuid()",
469
+ ...opts
470
+ };
471
+ },
472
+ autoIncrement(opts) {
473
+ return {
474
+ kind: "scalar",
475
+ type: "Int",
476
+ isId: true,
477
+ default: "autoincrement()",
478
+ ...opts
479
+ };
480
+ },
481
+ createdAt(opts) {
482
+ return { kind: "scalar", type: "DateTime", default: "now()", ...opts };
483
+ },
484
+ updatedAt(opts) {
485
+ return { kind: "scalar", type: "DateTime", updatedAt: true, ...opts };
486
+ },
487
+ email(opts) {
488
+ return {
489
+ kind: "scalar",
490
+ type: "String",
491
+ zod: z7.email(),
492
+ ...opts
493
+ };
494
+ },
495
+ url(opts) {
496
+ return {
497
+ kind: "scalar",
498
+ type: "String",
499
+ zod: z7.url(),
500
+ ...opts
501
+ };
502
+ },
503
+ enum(enumName, opts) {
504
+ return { kind: "enum", enumName, ...opts };
505
+ },
506
+ inlineEnum(enumName, values, opts) {
507
+ return { kind: "enum", enumName, values, ...opts };
508
+ },
509
+ hasOne(target, opts) {
510
+ return { kind: "relation", type: "hasOne", target, ...opts };
511
+ },
512
+ hasMany(target, opts) {
513
+ return { kind: "relation", type: "hasMany", target, ...opts };
514
+ },
515
+ belongsTo(target, fields, references, opts) {
516
+ return {
517
+ kind: "relation",
518
+ type: "belongsTo",
519
+ target,
520
+ fields,
521
+ references,
522
+ ...opts
523
+ };
524
+ },
525
+ foreignKey(opts) {
526
+ return { kind: "scalar", type: "String", ...opts };
527
+ }
528
+ };
529
+ var index = {
530
+ on(fields, opts) {
531
+ return { fields, ...opts };
532
+ },
533
+ unique(fields, opts) {
534
+ return { fields, unique: true, ...opts };
535
+ },
536
+ compound(fields, sort, opts) {
537
+ return { fields, sort, ...opts };
538
+ }
539
+ };
540
+
541
+ // src/entity/generator.ts
542
+ function generatePrismaSchema(entities, options = {}) {
543
+ const {
544
+ provider = "postgresql",
545
+ clientOutput = "../../src/generated/prisma",
546
+ includePothos = true,
547
+ pothosOutput = "../../src/generated/pothos-types.ts"
548
+ } = options;
549
+ const lines = [];
550
+ const schemas = new Set;
551
+ entities.forEach((entity) => {
552
+ if (entity.schema) {
553
+ schemas.add(entity.schema);
554
+ }
555
+ });
556
+ const schemaList = schemas.size > 0 ? Array.from(schemas) : ["public"];
557
+ lines.push("datasource db {");
558
+ lines.push(` provider = "${provider}"`);
559
+ if (schemas.size > 0) {
560
+ lines.push(` schemas = [${schemaList.map((s) => `"${s}"`).join(", ")}]`);
561
+ }
562
+ lines.push("}");
563
+ lines.push("");
564
+ lines.push("generator client {");
565
+ lines.push(' provider = "prisma-client"');
566
+ lines.push(` output = "${clientOutput}"`);
567
+ lines.push("");
568
+ lines.push(' engineType = "client"');
569
+ lines.push(' runtime = "bun"');
570
+ lines.push(' moduleFormat = "esm"');
571
+ lines.push(' generatedFileExtension = "ts"');
572
+ lines.push(' importFileExtension = "ts"');
573
+ lines.push("}");
574
+ lines.push("");
575
+ if (includePothos) {
576
+ lines.push("generator pothos {");
577
+ lines.push(' provider = "prisma-pothos-types"');
578
+ lines.push(' clientOutput = "./prisma"');
579
+ lines.push(` output = "${pothosOutput}"`);
580
+ lines.push(" generateDatamodel = true");
581
+ lines.push(" documentation = false");
582
+ lines.push("}");
583
+ lines.push("");
584
+ }
585
+ const enumMap = new Map;
586
+ entities.forEach((entity) => {
587
+ entity.enums?.forEach((enumDef) => {
588
+ if (!enumMap.has(enumDef.name)) {
589
+ enumMap.set(enumDef.name, enumDef);
590
+ }
591
+ });
592
+ Object.values(entity.fields).forEach((field2) => {
593
+ if (field2.kind === "enum" && field2.values && !enumMap.has(field2.enumName)) {
594
+ enumMap.set(field2.enumName, {
595
+ name: field2.enumName,
596
+ values: field2.values,
597
+ schema: entity.schema
598
+ });
599
+ }
600
+ });
601
+ });
602
+ enumMap.forEach((enumDef) => {
603
+ lines.push(...generateEnumBlock(enumDef));
604
+ lines.push("");
605
+ });
606
+ entities.forEach((entity) => {
607
+ lines.push(...generateModelBlock(entity));
608
+ lines.push("");
609
+ });
610
+ return lines.join(`
611
+ `);
612
+ }
613
+ function generateEnumBlock(enumDef) {
614
+ const lines = [];
615
+ if (enumDef.description) {
616
+ lines.push(`/// ${enumDef.description}`);
617
+ }
618
+ lines.push(`enum ${enumDef.name} {`);
619
+ enumDef.values.forEach((value) => {
620
+ lines.push(` ${value}`);
621
+ });
622
+ lines.push("");
623
+ if (enumDef.schema) {
624
+ lines.push(` @@schema("${enumDef.schema}")`);
625
+ }
626
+ lines.push("}");
627
+ return lines;
628
+ }
629
+ function generateModelBlock(entity) {
630
+ const lines = [];
631
+ if (entity.description) {
632
+ lines.push(`/// ${entity.description}`);
633
+ }
634
+ lines.push(`model ${entity.name} {`);
635
+ Object.entries(entity.fields).forEach(([fieldName, field2]) => {
636
+ const fieldLine = generateFieldLine(fieldName, field2);
637
+ if (field2.description) {
638
+ lines.push(` /// ${field2.description}`);
639
+ }
640
+ lines.push(` ${fieldLine}`);
641
+ });
642
+ if (entity.indexes && entity.indexes.length > 0) {
643
+ lines.push("");
644
+ entity.indexes.forEach((idx) => {
645
+ lines.push(` ${generateIndexLine(idx)}`);
646
+ });
647
+ }
648
+ if (entity.map) {
649
+ lines.push(` @@map("${entity.map}")`);
650
+ }
651
+ if (entity.schema) {
652
+ lines.push(` @@schema("${entity.schema}")`);
653
+ }
654
+ lines.push("}");
655
+ return lines;
656
+ }
657
+ function generateFieldLine(fieldName, field2) {
658
+ if (field2.kind === "scalar") {
659
+ return generateScalarFieldLine(fieldName, field2);
660
+ } else if (field2.kind === "enum") {
661
+ return generateEnumFieldLine(fieldName, field2);
662
+ } else if (field2.kind === "relation") {
663
+ return generateRelationFieldLine(fieldName, field2);
664
+ }
665
+ throw new Error(`Unknown field kind: ${field2.kind}`);
666
+ }
667
+ function generateScalarFieldLine(fieldName, field2) {
668
+ const parts = [fieldName];
669
+ let typeStr = field2.type;
670
+ if (field2.isArray) {
671
+ typeStr += "[]";
672
+ }
673
+ if (field2.isOptional) {
674
+ typeStr += "?";
675
+ }
676
+ parts.push(typeStr);
677
+ const attrs = [];
678
+ if (field2.isId) {
679
+ attrs.push("@id");
680
+ }
681
+ if (field2.default !== undefined) {
682
+ if (typeof field2.default === "string" && field2.default.includes("(")) {
683
+ attrs.push(`@default(${field2.default})`);
684
+ } else if (typeof field2.default === "boolean") {
685
+ attrs.push(`@default(${field2.default})`);
686
+ } else if (typeof field2.default === "number") {
687
+ attrs.push(`@default(${field2.default})`);
688
+ } else {
689
+ attrs.push(`@default("${field2.default}")`);
690
+ }
691
+ }
692
+ if (field2.updatedAt) {
693
+ attrs.push("@updatedAt");
694
+ }
695
+ if (field2.isUnique) {
696
+ attrs.push("@unique");
697
+ }
698
+ if (field2.map) {
699
+ attrs.push(`@map("${field2.map}")`);
700
+ }
701
+ if (field2.dbType) {
702
+ attrs.push(`@db.${field2.dbType}`);
703
+ }
704
+ if (attrs.length > 0) {
705
+ parts.push(attrs.join(" "));
706
+ }
707
+ return parts.join(" ");
708
+ }
709
+ function generateEnumFieldLine(fieldName, field2) {
710
+ const parts = [fieldName];
711
+ let typeStr = field2.enumName;
712
+ if (field2.isArray) {
713
+ typeStr += "[]";
714
+ }
715
+ if (field2.isOptional) {
716
+ typeStr += "?";
717
+ }
718
+ parts.push(typeStr);
719
+ const attrs = [];
720
+ if (field2.default !== undefined) {
721
+ attrs.push(`@default(${field2.default})`);
722
+ }
723
+ if (field2.isUnique) {
724
+ attrs.push("@unique");
725
+ }
726
+ if (field2.map) {
727
+ attrs.push(`@map("${field2.map}")`);
728
+ }
729
+ if (attrs.length > 0) {
730
+ parts.push(attrs.join(" "));
731
+ }
732
+ return parts.join(" ");
733
+ }
734
+ function generateRelationFieldLine(fieldName, field2) {
735
+ const parts = [fieldName];
736
+ let typeStr = field2.target;
737
+ if (field2.type === "hasMany") {
738
+ typeStr += "[]";
739
+ }
740
+ if (field2.type === "hasOne") {
741
+ typeStr += "?";
742
+ }
743
+ parts.push(typeStr);
744
+ const relationParts = [];
745
+ if (field2.name) {
746
+ relationParts.push(`name: "${field2.name}"`);
747
+ }
748
+ if (field2.fields && field2.fields.length > 0) {
749
+ relationParts.push(`fields: [${field2.fields.join(", ")}]`);
750
+ }
751
+ if (field2.references && field2.references.length > 0) {
752
+ relationParts.push(`references: [${field2.references.join(", ")}]`);
753
+ }
754
+ if (field2.onDelete) {
755
+ relationParts.push(`onDelete: ${field2.onDelete}`);
756
+ }
757
+ if (field2.onUpdate) {
758
+ relationParts.push(`onUpdate: ${field2.onUpdate}`);
759
+ }
760
+ if (relationParts.length > 0) {
761
+ parts.push(`@relation(${relationParts.join(", ")})`);
762
+ }
763
+ return parts.join(" ");
764
+ }
765
+ function generateIndexLine(idx) {
766
+ const fieldList = idx.fields.join(", ");
767
+ const parts = [];
768
+ if (idx.unique) {
769
+ parts.push(`@@unique([${fieldList}]`);
770
+ } else {
771
+ parts.push(`@@index([${fieldList}]`);
772
+ }
773
+ const options = [];
774
+ if (idx.name) {
775
+ options.push(`name: "${idx.name}"`);
776
+ }
777
+ if (idx.type) {
778
+ options.push(`type: ${idx.type}`);
779
+ }
780
+ if (options.length > 0) {
781
+ parts[0] += `, ${options.join(", ")}`;
782
+ }
783
+ parts[0] += ")";
784
+ return parts.join("");
785
+ }
786
+ function composeModuleSchemas(contributions, options = {}) {
787
+ const allEntities = [];
788
+ const allEnums = new Map;
789
+ contributions.forEach((contrib) => {
790
+ contrib.entities.forEach((entity) => {
791
+ allEntities.push({
792
+ ...entity,
793
+ module: contrib.moduleId
794
+ });
795
+ });
796
+ contrib.enums?.forEach((enumDef) => {
797
+ if (!allEnums.has(enumDef.name)) {
798
+ allEnums.set(enumDef.name, enumDef);
799
+ }
800
+ });
801
+ });
802
+ if (allEntities[0] && allEnums.size > 0) {
803
+ allEntities[0] = {
804
+ ...allEntities[0],
805
+ enums: [...allEntities[0].enums ?? [], ...allEnums.values()]
806
+ };
807
+ }
808
+ return generatePrismaSchema(allEntities, options);
809
+ }
810
+ function generateEntityFragment(entity) {
811
+ return generateModelBlock(entity).join(`
812
+ `);
813
+ }
814
+ function generateEnumFragment(enumDef) {
815
+ return generateEnumBlock(enumDef).join(`
816
+ `);
817
+ }
818
+ export {
819
+ isSchemaType,
820
+ isSchemaModel,
821
+ index,
822
+ generatePrismaSchema,
823
+ generateEnumFragment,
824
+ generateEntityFragment,
825
+ fromZod,
826
+ fromJsonSchema,
827
+ field,
828
+ defineSchemaModel,
829
+ defineEnum,
830
+ defineEntityEnum,
831
+ defineEntity,
832
+ composeModuleSchemas,
833
+ ZodSchemaType,
834
+ SchemaModel,
835
+ ScalarTypeEnum,
836
+ JsonSchemaType,
837
+ GraphQLSchemaType,
838
+ FieldType,
839
+ EnumType
840
+ };