hekireki 0.7.4 → 0.7.5
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/README.md +55 -19
- package/dist/generator/ajv/index.d.ts +4 -0
- package/dist/generator/ajv/index.js +34 -3
- package/dist/generator/arktype/index.d.ts +4 -0
- package/dist/generator/arktype/index.js +44 -3
- package/dist/generator/dbml/index.js +1 -1
- package/dist/generator/drizzle/index.js +1 -1
- package/dist/generator/ecto/index.js +1 -1
- package/dist/generator/effect/index.js +3 -3
- package/dist/generator/gorm/index.js +1 -1
- package/dist/generator/mermaid-er/index.js +1 -1
- package/dist/generator/sea-orm/index.js +1 -1
- package/dist/generator/sqlalchemy/index.js +1 -1
- package/dist/generator/typebox/index.d.ts +4 -0
- package/dist/generator/typebox/index.js +45 -4
- package/dist/generator/valibot/index.js +2 -2
- package/dist/generator/zod/index.js +2 -2
- package/dist/{prisma-DHllEbMR.js → prisma-CfcdmYJs.js} +1 -1
- package/dist/{utils-0tE5jzYR.js → utils-BGe939v0.js} +12 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -301,22 +301,34 @@ export type PostRelations = v.InferOutput<typeof PostRelationsSchema>
|
|
|
301
301
|
import { type } from 'arktype'
|
|
302
302
|
|
|
303
303
|
export const UserSchema = type({
|
|
304
|
-
/**
|
|
304
|
+
/**
|
|
305
|
+
* Primary key
|
|
306
|
+
*/
|
|
305
307
|
id: 'string.uuid',
|
|
306
|
-
/**
|
|
308
|
+
/**
|
|
309
|
+
* Display name
|
|
310
|
+
*/
|
|
307
311
|
name: '1 <= string <= 50',
|
|
308
312
|
})
|
|
309
313
|
|
|
310
314
|
export type User = typeof UserSchema.infer
|
|
311
315
|
|
|
312
316
|
export const PostSchema = type({
|
|
313
|
-
/**
|
|
317
|
+
/**
|
|
318
|
+
* Primary key
|
|
319
|
+
*/
|
|
314
320
|
id: 'string.uuid',
|
|
315
|
-
/**
|
|
321
|
+
/**
|
|
322
|
+
* Article title
|
|
323
|
+
*/
|
|
316
324
|
title: '1 <= string <= 100',
|
|
317
|
-
/**
|
|
325
|
+
/**
|
|
326
|
+
* Body content (no length limit)
|
|
327
|
+
*/
|
|
318
328
|
content: 'string',
|
|
319
|
-
/**
|
|
329
|
+
/**
|
|
330
|
+
* Foreign key referencing User.id
|
|
331
|
+
*/
|
|
320
332
|
userId: 'string.uuid',
|
|
321
333
|
})
|
|
322
334
|
|
|
@@ -369,22 +381,34 @@ export type PostEncoded = typeof PostSchema.Encoded
|
|
|
369
381
|
import { type Static, Type } from '@sinclair/typebox'
|
|
370
382
|
|
|
371
383
|
export const UserSchema = Type.Object({
|
|
372
|
-
/**
|
|
384
|
+
/**
|
|
385
|
+
* Primary key
|
|
386
|
+
*/
|
|
373
387
|
id: Type.String({ format: 'uuid' }),
|
|
374
|
-
/**
|
|
388
|
+
/**
|
|
389
|
+
* Display name
|
|
390
|
+
*/
|
|
375
391
|
name: Type.String({ minLength: 1, maxLength: 50 }),
|
|
376
392
|
})
|
|
377
393
|
|
|
378
394
|
export type User = Static<typeof UserSchema>
|
|
379
395
|
|
|
380
396
|
export const PostSchema = Type.Object({
|
|
381
|
-
/**
|
|
397
|
+
/**
|
|
398
|
+
* Primary key
|
|
399
|
+
*/
|
|
382
400
|
id: Type.String({ format: 'uuid' }),
|
|
383
|
-
/**
|
|
401
|
+
/**
|
|
402
|
+
* Article title
|
|
403
|
+
*/
|
|
384
404
|
title: Type.String({ minLength: 1, maxLength: 100 }),
|
|
385
|
-
/**
|
|
405
|
+
/**
|
|
406
|
+
* Body content (no length limit)
|
|
407
|
+
*/
|
|
386
408
|
content: Type.String(),
|
|
387
|
-
/**
|
|
409
|
+
/**
|
|
410
|
+
* Foreign key referencing User.id
|
|
411
|
+
*/
|
|
388
412
|
userId: Type.String({ format: 'uuid' }),
|
|
389
413
|
})
|
|
390
414
|
|
|
@@ -405,7 +429,7 @@ export const PostRelationsSchema = Type.Object({
|
|
|
405
429
|
export type PostRelations = Static<typeof PostRelationsSchema>
|
|
406
430
|
```
|
|
407
431
|
|
|
408
|
-
### AJV
|
|
432
|
+
### AJV
|
|
409
433
|
|
|
410
434
|
```ts
|
|
411
435
|
import type { FromSchema } from 'json-schema-to-ts'
|
|
@@ -413,9 +437,13 @@ import type { FromSchema } from 'json-schema-to-ts'
|
|
|
413
437
|
export const UserSchema = {
|
|
414
438
|
type: 'object' as const,
|
|
415
439
|
properties: {
|
|
416
|
-
/**
|
|
440
|
+
/**
|
|
441
|
+
* Primary key
|
|
442
|
+
*/
|
|
417
443
|
id: { type: 'string' as const, format: 'uuid' as const },
|
|
418
|
-
/**
|
|
444
|
+
/**
|
|
445
|
+
* Display name
|
|
446
|
+
*/
|
|
419
447
|
name: { type: 'string' as const, minLength: 1, maxLength: 50 },
|
|
420
448
|
},
|
|
421
449
|
required: ['id', 'name'] as const,
|
|
@@ -427,13 +455,21 @@ export type User = FromSchema<typeof UserSchema>
|
|
|
427
455
|
export const PostSchema = {
|
|
428
456
|
type: 'object' as const,
|
|
429
457
|
properties: {
|
|
430
|
-
/**
|
|
458
|
+
/**
|
|
459
|
+
* Primary key
|
|
460
|
+
*/
|
|
431
461
|
id: { type: 'string' as const, format: 'uuid' as const },
|
|
432
|
-
/**
|
|
462
|
+
/**
|
|
463
|
+
* Article title
|
|
464
|
+
*/
|
|
433
465
|
title: { type: 'string' as const, minLength: 1, maxLength: 100 },
|
|
434
|
-
/**
|
|
466
|
+
/**
|
|
467
|
+
* Body content (no length limit)
|
|
468
|
+
*/
|
|
435
469
|
content: { type: 'string' as const },
|
|
436
|
-
/**
|
|
470
|
+
/**
|
|
471
|
+
* Foreign key referencing User.id
|
|
472
|
+
*/
|
|
437
473
|
userId: { type: 'string' as const, format: 'uuid' as const },
|
|
438
474
|
},
|
|
439
475
|
required: ['id', 'title', 'content', 'userId'] as const,
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
2
|
|
|
3
3
|
//#region src/generator/ajv/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Prisma generator entry point for AJV JSON Schema generation
|
|
6
|
+
* @param options - The Prisma generator options
|
|
7
|
+
*/
|
|
4
8
|
declare function main(options: GeneratorOptions): Promise<void>;
|
|
5
9
|
//#endregion
|
|
6
10
|
export { main };
|
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import { c as
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { a as makeCommentBlock, c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, p as writeFile, t as getBool } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
8
8
|
//#region src/helper/ajv.ts
|
|
9
|
+
/**
|
|
10
|
+
* Generate AJV type inference using FromSchema
|
|
11
|
+
* @param modelName - The model name to generate type inference for
|
|
12
|
+
*/
|
|
9
13
|
function makeAjvInfer(modelName) {
|
|
10
14
|
return `export type ${modelName} = FromSchema<typeof ${modelName}Schema>`;
|
|
11
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate JSON Schema enum expression for AJV
|
|
18
|
+
* @param values - The enum values to generate expression for
|
|
19
|
+
*/
|
|
12
20
|
function makeAjvEnumExpression(values) {
|
|
13
21
|
return `{ enum: [${values.map((v) => `'${v}'`).join(", ")}] as const }`;
|
|
14
22
|
}
|
|
23
|
+
/** Mapping from Prisma scalar types to JSON Schema type expressions */
|
|
15
24
|
const PRISMA_TO_AJV = {
|
|
16
25
|
String: "{ type: 'string' as const }",
|
|
17
26
|
Int: "{ type: 'integer' as const }",
|
|
@@ -23,14 +32,25 @@ const PRISMA_TO_AJV = {
|
|
|
23
32
|
Json: "{}",
|
|
24
33
|
Bytes: "{ type: 'string' as const }"
|
|
25
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Generate JSON Schema object definition for a model
|
|
37
|
+
* @param modelFields - The fields of the model
|
|
38
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
39
|
+
*/
|
|
26
40
|
function makeAjvSchemas(modelFields, comment) {
|
|
27
41
|
const modelName = modelFields[0].modelName;
|
|
28
42
|
const properties = modelFields.map((field) => {
|
|
29
|
-
return `${comment
|
|
43
|
+
return `${comment ? makeCommentBlock(field.comment, 4) : ""} ${field.fieldName}: ${field.validation ?? "{ type: 'unknown' as const }"},`;
|
|
30
44
|
}).join("\n");
|
|
31
45
|
const requiredFields = modelFields.filter((f) => f.isRequired).map((f) => f.fieldName);
|
|
32
46
|
return `export const ${modelName}Schema = {\n type: 'object' as const,\n properties: {\n${properties}\n },${requiredFields.length > 0 ? `\n required: [${requiredFields.map((f) => `'${f}'`).join(", ")}] as const,` : ""}\n additionalProperties: false,\n} as const`;
|
|
33
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Generate JSON Schema relation object definition
|
|
50
|
+
* @param model - The model to generate relations for
|
|
51
|
+
* @param relProps - The relation properties
|
|
52
|
+
* @param options - Options for type export generation
|
|
53
|
+
*/
|
|
34
54
|
function makeAjvRelations(model, relProps, options) {
|
|
35
55
|
if (relProps.length === 0) return null;
|
|
36
56
|
const base = ` ...${model.name}Schema.properties,`;
|
|
@@ -38,6 +58,13 @@ function makeAjvRelations(model, relProps, options) {
|
|
|
38
58
|
const typeLine = options?.includeType ? `\n\nexport type ${model.name}Relations = FromSchema<typeof ${model.name}RelationsSchema>` : "";
|
|
39
59
|
return `export const ${model.name}RelationsSchema = {\n type: 'object' as const,\n properties: {\n${base}\n${rels}\n },\n additionalProperties: false,\n} as const${typeLine}`;
|
|
40
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Generate AJV-compatible JSON Schema validation code from Prisma models
|
|
63
|
+
* @param models - The Prisma data models
|
|
64
|
+
* @param type - Whether to include type inference using FromSchema
|
|
65
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
66
|
+
* @param enums - The Prisma enum definitions
|
|
67
|
+
*/
|
|
41
68
|
function ajv(models, type, comment, enums) {
|
|
42
69
|
return validationSchemas(models, type, comment, {
|
|
43
70
|
importStatement: type ? `import type { FromSchema } from 'json-schema-to-ts'` : "",
|
|
@@ -55,6 +82,10 @@ function ajv(models, type, comment, enums) {
|
|
|
55
82
|
//#endregion
|
|
56
83
|
//#region src/generator/ajv/index.ts
|
|
57
84
|
const { generatorHandler } = pkg;
|
|
85
|
+
/**
|
|
86
|
+
* Prisma generator entry point for AJV JSON Schema generation
|
|
87
|
+
* @param options - The Prisma generator options
|
|
88
|
+
*/
|
|
58
89
|
async function main(options) {
|
|
59
90
|
if (!(options.generator.isCustomOutput && options.generator.output?.value)) throw new Error("output is required for Hekireki-AJV. Please specify output in your generator config.");
|
|
60
91
|
const output = options.generator.output.value;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
2
|
|
|
3
3
|
//#region src/generator/arktype/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Prisma generator entry point for ArkType schema generation
|
|
6
|
+
* @param options - The Prisma generator options
|
|
7
|
+
*/
|
|
4
8
|
declare function main(options: GeneratorOptions): Promise<void>;
|
|
5
9
|
//#endregion
|
|
6
10
|
export { main };
|
|
@@ -1,25 +1,44 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { a as makeCommentBlock, c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, p as writeFile, t as getBool, u as schemaFromFields } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
8
8
|
//#region src/helper/arktype.ts
|
|
9
|
+
/**
|
|
10
|
+
* Generate ArkType type inference
|
|
11
|
+
* @param modelName - The model name to generate type inference for
|
|
12
|
+
*/
|
|
9
13
|
function makeArktypeInfer(modelName) {
|
|
10
14
|
return `export type ${modelName} = typeof ${modelName}Schema.infer`;
|
|
11
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate ArkType type() schema definition
|
|
18
|
+
* @param modelName - The model name for the schema
|
|
19
|
+
* @param fields - The formatted field definitions string
|
|
20
|
+
*/
|
|
12
21
|
function makeArktypeSchema(modelName, fields) {
|
|
13
22
|
return `export const ${modelName}Schema = type({\n${fields}\n})`;
|
|
14
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Generate ArkType property definitions
|
|
26
|
+
* @param fields - The fields to generate properties for
|
|
27
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
28
|
+
*/
|
|
15
29
|
function makeArktypeProperties(fields, comment) {
|
|
16
30
|
return fields.map((field) => {
|
|
17
|
-
return `${comment
|
|
31
|
+
return `${comment ? makeCommentBlock(field.comment, 2) : ""} ${field.fieldName}: ${field.validation ?? "\"unknown\""},`;
|
|
18
32
|
}).join("\n");
|
|
19
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Generate ArkType enum expression using union string syntax
|
|
36
|
+
* @param values - The enum values to generate expression for
|
|
37
|
+
*/
|
|
20
38
|
function makeArktypeEnumExpression(values) {
|
|
21
39
|
return `"${values.map((v) => `'${v}'`).join(" | ")}"`;
|
|
22
40
|
}
|
|
41
|
+
/** Mapping from Prisma scalar types to ArkType type expressions */
|
|
23
42
|
const PRISMA_TO_ARKTYPE = {
|
|
24
43
|
String: "\"string\"",
|
|
25
44
|
Int: "\"number\"",
|
|
@@ -31,15 +50,33 @@ const PRISMA_TO_ARKTYPE = {
|
|
|
31
50
|
Json: "\"unknown\"",
|
|
32
51
|
Bytes: "\"unknown\""
|
|
33
52
|
};
|
|
53
|
+
/**
|
|
54
|
+
* Generate ArkType type() schema from model fields
|
|
55
|
+
* @param modelFields - The fields of the model
|
|
56
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
57
|
+
*/
|
|
34
58
|
function makeArktypeSchemas(modelFields, comment) {
|
|
35
59
|
return schemaFromFields(modelFields, comment, makeArktypeSchema, makeArktypeProperties);
|
|
36
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Generate ArkType relation schema definition
|
|
63
|
+
* @param model - The model to generate relations for
|
|
64
|
+
* @param relProps - The relation properties
|
|
65
|
+
* @param options - Options for type export generation
|
|
66
|
+
*/
|
|
37
67
|
function makeArktypeRelations(model, relProps, options) {
|
|
38
68
|
if (relProps.length === 0) return null;
|
|
39
69
|
const fields = `${`...${model.name}Schema.t,`}${relProps.map((r) => `${r.key}:${r.isMany ? `${r.targetModel}Schema.array()` : `${r.targetModel}Schema`},`).join("")}`;
|
|
40
70
|
const typeLine = options?.includeType ? `\n\nexport type ${model.name}Relations = typeof ${model.name}RelationsSchema.infer` : "";
|
|
41
71
|
return `export const ${model.name}RelationsSchema = type({${fields}})${typeLine}`;
|
|
42
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Generate ArkType validation code from Prisma models
|
|
75
|
+
* @param models - The Prisma data models
|
|
76
|
+
* @param type - Whether to include type inference
|
|
77
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
78
|
+
* @param enums - The Prisma enum definitions
|
|
79
|
+
*/
|
|
43
80
|
function arktype(models, type, comment, enums) {
|
|
44
81
|
return validationSchemas(models, type, comment, {
|
|
45
82
|
importStatement: `import { type } from 'arktype'`,
|
|
@@ -57,6 +94,10 @@ function arktype(models, type, comment, enums) {
|
|
|
57
94
|
//#endregion
|
|
58
95
|
//#region src/generator/arktype/index.ts
|
|
59
96
|
const { generatorHandler } = pkg;
|
|
97
|
+
/**
|
|
98
|
+
* Prisma generator entry point for ArkType schema generation
|
|
99
|
+
* @param options - The Prisma generator options
|
|
100
|
+
*/
|
|
60
101
|
async function main(options) {
|
|
61
102
|
if (!(options.generator.isCustomOutput && options.generator.output?.value)) throw new Error("output is required for Hekireki-ArkType. Please specify output in your generator config.");
|
|
62
103
|
const output = options.generator.output.value;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { d as
|
|
2
|
+
import { d as stripAnnotations, f as mkdir, m as writeFileBinary, n as getString, p as writeFile } from "../../utils-BGe939v0.js";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
import { Resvg } from "@resvg/resvg-js";
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
3
|
+
import { f as mkdir, p as writeFile, s as makeSnakeCase } from "../../utils-BGe939v0.js";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import pkg from "@prisma/generator-helper";
|
|
6
6
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { f as mkdir, p as writeFile, s as makeSnakeCase } from "../../utils-BGe939v0.js";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { a as makeCommentBlock, c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, p as writeFile, t as getBool, u as schemaFromFields } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
@@ -14,7 +14,7 @@ function makeEffectSchema(modelName, fields) {
|
|
|
14
14
|
}
|
|
15
15
|
function makeEffectProperties(fields, comment) {
|
|
16
16
|
return fields.map((field) => {
|
|
17
|
-
return `${comment
|
|
17
|
+
return `${comment ? makeCommentBlock(field.comment, 2) : ""} ${field.fieldName}: ${field.validation ?? "Schema.Unknown"},`;
|
|
18
18
|
}).join("\n");
|
|
19
19
|
}
|
|
20
20
|
function makeEffectEnumExpression(values) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { f as mkdir, n as getString, p as writeFile, s as makeSnakeCase } from "../../utils-BGe939v0.js";
|
|
3
3
|
import path, { dirname } from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { d as
|
|
2
|
+
import { d as stripAnnotations, f as mkdir, p as writeFile } from "../../utils-BGe939v0.js";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { f as mkdir, n as getString, p as writeFile, s as makeSnakeCase } from "../../utils-BGe939v0.js";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { f as mkdir, p as writeFile, s as makeSnakeCase } from "../../utils-BGe939v0.js";
|
|
3
3
|
import path, { dirname } from "node:path";
|
|
4
4
|
import pkg from "@prisma/generator-helper";
|
|
5
5
|
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { GeneratorOptions } from "@prisma/generator-helper";
|
|
2
2
|
|
|
3
3
|
//#region src/generator/typebox/index.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Prisma generator entry point for TypeBox schema generation
|
|
6
|
+
* @param options - The Prisma generator options
|
|
7
|
+
*/
|
|
4
8
|
declare function main(options: GeneratorOptions): Promise<void>;
|
|
5
9
|
//#endregion
|
|
6
10
|
export { main };
|
|
@@ -1,28 +1,47 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { a as makeCommentBlock, c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, p as writeFile, t as getBool, u as schemaFromFields } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
8
8
|
//#region src/helper/typebox.ts
|
|
9
|
+
/**
|
|
10
|
+
* Generate TypeBox type inference using Static
|
|
11
|
+
* @param modelName - The model name to generate type inference for
|
|
12
|
+
*/
|
|
9
13
|
function makeTypeBoxInfer(modelName) {
|
|
10
14
|
return `export type ${modelName} = Static<typeof ${modelName}Schema>`;
|
|
11
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate TypeBox Type.Object schema definition
|
|
18
|
+
* @param modelName - The model name for the schema
|
|
19
|
+
* @param fields - The formatted field definitions string
|
|
20
|
+
*/
|
|
12
21
|
function makeTypeBoxSchema(modelName, fields) {
|
|
13
22
|
return `export const ${modelName}Schema = Type.Object({\n${fields}\n})`;
|
|
14
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Generate TypeBox property definitions with optional wrapping
|
|
26
|
+
* @param fields - The fields to generate properties for
|
|
27
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
28
|
+
*/
|
|
15
29
|
function makeTypeBoxProperties(fields, comment) {
|
|
16
30
|
return fields.map((field) => {
|
|
17
|
-
const
|
|
31
|
+
const commentBlock = comment ? makeCommentBlock(field.comment, 2) : "";
|
|
18
32
|
const expr = field.validation ?? "Type.Unknown()";
|
|
19
33
|
const wrapped = field.isRequired ? expr : `Type.Optional(${expr})`;
|
|
20
|
-
return `${
|
|
34
|
+
return `${commentBlock} ${field.fieldName}: ${wrapped},`;
|
|
21
35
|
}).join("\n");
|
|
22
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Generate TypeBox enum expression using Type.Union with Type.Literal
|
|
39
|
+
* @param values - The enum values to generate expression for
|
|
40
|
+
*/
|
|
23
41
|
function makeTypeBoxEnumExpression(values) {
|
|
24
42
|
return `Type.Union([${values.map((v) => `Type.Literal('${v}')`).join(", ")}])`;
|
|
25
43
|
}
|
|
44
|
+
/** Mapping from Prisma scalar types to TypeBox type expressions */
|
|
26
45
|
const PRISMA_TO_TYPEBOX = {
|
|
27
46
|
String: "Type.String()",
|
|
28
47
|
Int: "Type.Integer()",
|
|
@@ -34,9 +53,20 @@ const PRISMA_TO_TYPEBOX = {
|
|
|
34
53
|
Json: "Type.Unknown()",
|
|
35
54
|
Bytes: "Type.Any()"
|
|
36
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Generate TypeBox Type.Object schema from model fields
|
|
58
|
+
* @param modelFields - The fields of the model
|
|
59
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
60
|
+
*/
|
|
37
61
|
function makeTypeBoxSchemas(modelFields, comment) {
|
|
38
62
|
return schemaFromFields(modelFields, comment, makeTypeBoxSchema, makeTypeBoxProperties);
|
|
39
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* Generate TypeBox relation schema definition
|
|
66
|
+
* @param model - The model to generate relations for
|
|
67
|
+
* @param relProps - The relation properties
|
|
68
|
+
* @param options - Options for type export generation
|
|
69
|
+
*/
|
|
40
70
|
function makeTypeBoxRelations(model, relProps, options) {
|
|
41
71
|
if (relProps.length === 0) return null;
|
|
42
72
|
const base = ` ...${model.name}Schema.properties,`;
|
|
@@ -44,6 +74,13 @@ function makeTypeBoxRelations(model, relProps, options) {
|
|
|
44
74
|
const typeLine = options?.includeType ? `\n\nexport type ${model.name}Relations = Static<typeof ${model.name}RelationsSchema>` : "";
|
|
45
75
|
return `export const ${model.name}RelationsSchema = Type.Object({\n${base}\n${rels}\n})${typeLine}`;
|
|
46
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Generate TypeBox validation code from Prisma models
|
|
79
|
+
* @param models - The Prisma data models
|
|
80
|
+
* @param type - Whether to include type inference using Static
|
|
81
|
+
* @param comment - Whether to include JSDoc comments in the generated code
|
|
82
|
+
* @param enums - The Prisma enum definitions
|
|
83
|
+
*/
|
|
47
84
|
function typebox(models, type, comment, enums) {
|
|
48
85
|
return validationSchemas(models, type, comment, {
|
|
49
86
|
importStatement: type ? `import { type Static, Type } from '@sinclair/typebox'` : `import { Type } from '@sinclair/typebox'`,
|
|
@@ -61,6 +98,10 @@ function typebox(models, type, comment, enums) {
|
|
|
61
98
|
//#endregion
|
|
62
99
|
//#region src/generator/typebox/index.ts
|
|
63
100
|
const { generatorHandler } = pkg;
|
|
101
|
+
/**
|
|
102
|
+
* Prisma generator entry point for TypeBox schema generation
|
|
103
|
+
* @param options - The Prisma generator options
|
|
104
|
+
*/
|
|
64
105
|
async function main(options) {
|
|
65
106
|
if (!(options.generator.isCustomOutput && options.generator.output?.value)) throw new Error("output is required for Hekireki-TypeBox. Please specify output in your generator config.");
|
|
66
107
|
const output = options.generator.output.value;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, o as makePropertiesGenerator, p as writeFile, t as getBool, u as schemaFromFields } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { t as fmt } from "../../format-DwiYldCm.js";
|
|
3
|
-
import {
|
|
4
|
-
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-
|
|
3
|
+
import { c as makeValidationExtractor, f as mkdir, l as parseDocumentWithoutAnnotations, n as getString, o as makePropertiesGenerator, p as writeFile, t as getBool, u as schemaFromFields } from "../../utils-BGe939v0.js";
|
|
4
|
+
import { n as validationSchemas, t as makeRelationsOnly } from "../../prisma-CfcdmYJs.js";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import pkg from "@prisma/generator-helper";
|
|
7
7
|
|
|
@@ -56,11 +56,21 @@ function stripAnnotations(doc) {
|
|
|
56
56
|
}).join("\n").trim();
|
|
57
57
|
return result.length > 0 ? result : void 0;
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Generate a JSDoc comment block from comment lines
|
|
61
|
+
* @param lines - The comment lines to include
|
|
62
|
+
* @param indent - The indentation level in spaces
|
|
63
|
+
*/
|
|
64
|
+
function makeCommentBlock(lines, indent) {
|
|
65
|
+
if (lines.length === 0) return "";
|
|
66
|
+
const prefix = " ".repeat(indent);
|
|
67
|
+
return `${prefix}/**\n${lines.map((c) => `${prefix} * ${c}`).join("\n")}\n${prefix} */\n`;
|
|
68
|
+
}
|
|
59
69
|
function makePropertiesGenerator(libraryPrefix, wrapCardinality) {
|
|
60
70
|
return function makeProperties(modelFields, includeComments) {
|
|
61
71
|
return modelFields.filter((field) => field.validation).map((field) => {
|
|
62
72
|
const cleanLines = field.comment.filter((line) => !(line.includes("@relation") || line.includes("@z") || line.includes("@v") || line.includes("@a") || line.includes("@e") || line.includes("@t") || line.includes("@j")));
|
|
63
|
-
const docComment = includeComments
|
|
73
|
+
const docComment = includeComments ? makeCommentBlock(cleanLines, 2) : "";
|
|
64
74
|
const base = `${libraryPrefix}.${field.validation}`;
|
|
65
75
|
const wrapped = wrapCardinality ? wrapCardinality(base, field.isRequired) : base;
|
|
66
76
|
return `${docComment} ${field.fieldName}: ${wrapped}`;
|
|
@@ -80,4 +90,4 @@ function schemaFromFields(modelFields, comment, schemaBuilder, propertiesGenerat
|
|
|
80
90
|
}
|
|
81
91
|
|
|
82
92
|
//#endregion
|
|
83
|
-
export {
|
|
93
|
+
export { makeCommentBlock as a, makeValidationExtractor as c, stripAnnotations as d, mkdir as f, isFields as i, parseDocumentWithoutAnnotations as l, writeFileBinary as m, getString as n, makePropertiesGenerator as o, writeFile as p, groupByModel as r, makeSnakeCase as s, getBool as t, schemaFromFields as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hekireki",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.5",
|
|
4
4
|
"description": "Hekireki is a tool that generates validation schemas for Zod, Valibot, ArkType, and Effect Schema, as well as ER diagrams and DBML, from Prisma schemas annotated with comments.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ajv",
|