@lssm/lib.schema 0.0.0-canary-20251217062943 → 0.0.0-canary-20251217072406
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.js +56 -1
- package/dist/FieldType.js +49 -1
- package/dist/ScalarTypeEnum.js +236 -1
- package/dist/SchemaModel.js +39 -1
- package/dist/entity/defineEntity.js +236 -1
- package/dist/entity/generator.js +217 -4
- package/dist/entity/index.js +5 -1
- package/dist/entity/types.js +1 -1
- package/dist/index.js +9 -1
- package/package.json +3 -3
package/dist/EnumType.js
CHANGED
|
@@ -1 +1,56 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { GraphQLEnumType } from "graphql";
|
|
3
|
+
|
|
4
|
+
//#region src/EnumType.ts
|
|
5
|
+
/**
|
|
6
|
+
* Strongly-typed string enum wrapper with one source of truth for zod, GraphQL, and JSON Schema.
|
|
7
|
+
*/
|
|
8
|
+
var EnumType = class {
|
|
9
|
+
name;
|
|
10
|
+
values;
|
|
11
|
+
gqlEnum;
|
|
12
|
+
constructor(name, values) {
|
|
13
|
+
this.name = name;
|
|
14
|
+
this.values = values;
|
|
15
|
+
this.gqlEnum = new GraphQLEnumType({
|
|
16
|
+
name: this.name,
|
|
17
|
+
values: Object.fromEntries(values.map((v) => [v, { value: v }]))
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** Enum type name (used by GraphQL and JSON Schema). */
|
|
21
|
+
getName() {
|
|
22
|
+
return this.name;
|
|
23
|
+
}
|
|
24
|
+
/** Returns the literal tuple of allowed values. */
|
|
25
|
+
getEnumValues() {
|
|
26
|
+
return this.values;
|
|
27
|
+
}
|
|
28
|
+
/** GraphQL enum instance suitable for Pothos or vanilla GraphQL schemas. */
|
|
29
|
+
getPothos() {
|
|
30
|
+
return this.gqlEnum;
|
|
31
|
+
}
|
|
32
|
+
/** zod schema representing this enum. */
|
|
33
|
+
getZod() {
|
|
34
|
+
return z.enum(this.values);
|
|
35
|
+
}
|
|
36
|
+
/** Minimal JSON representation (alias of getJsonSchema). */
|
|
37
|
+
getJson() {
|
|
38
|
+
return {
|
|
39
|
+
type: "string",
|
|
40
|
+
enum: this.values
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** JSON Schema for this enum. */
|
|
44
|
+
getJsonSchema() {
|
|
45
|
+
return this.getJson();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Helper to define an EnumType.
|
|
50
|
+
* @param name Display/type name used across GraphQL and JSON Schema
|
|
51
|
+
* @param values Literal tuple of allowed string values (at least one)
|
|
52
|
+
*/
|
|
53
|
+
const defineEnum = (name, values) => new EnumType(name, values);
|
|
54
|
+
|
|
55
|
+
//#endregion
|
|
56
|
+
export { EnumType, defineEnum };
|
package/dist/FieldType.js
CHANGED
|
@@ -1 +1,49 @@
|
|
|
1
|
-
import"zod";
|
|
1
|
+
import "zod";
|
|
2
|
+
import { GraphQLScalarType } from "graphql";
|
|
3
|
+
|
|
4
|
+
//#region src/FieldType.ts
|
|
5
|
+
/**
|
|
6
|
+
* GraphQL scalar wrapper that carries zod and JSON Schema metadata.
|
|
7
|
+
*
|
|
8
|
+
* TInternal is the runtime representation; TExternal is the GraphQL output.
|
|
9
|
+
*/
|
|
10
|
+
var FieldType = class extends GraphQLScalarType {
|
|
11
|
+
zodSchema;
|
|
12
|
+
jsonSchemaDef;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
super(config);
|
|
15
|
+
this.zodSchema = config.zod;
|
|
16
|
+
this.jsonSchemaDef = config.jsonSchema;
|
|
17
|
+
}
|
|
18
|
+
/** Return the attached zod schema for validation. */
|
|
19
|
+
getZod() {
|
|
20
|
+
return this.zodSchema;
|
|
21
|
+
}
|
|
22
|
+
/** GraphQL scalar instance usable by Pothos or vanilla GraphQL. */
|
|
23
|
+
getPothos() {
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/** Return the JSON Schema (evaluates factory if provided). */
|
|
27
|
+
getJson() {
|
|
28
|
+
return typeof this.jsonSchemaDef === "function" ? this.jsonSchemaDef() : this.jsonSchemaDef;
|
|
29
|
+
}
|
|
30
|
+
getJsonSchemaDef() {
|
|
31
|
+
return this.jsonSchemaDef;
|
|
32
|
+
}
|
|
33
|
+
getJsonSchema() {
|
|
34
|
+
const deepResolve = (v) => {
|
|
35
|
+
const value = typeof v === "function" ? v() : v;
|
|
36
|
+
if (Array.isArray(value)) return value.map((item) => deepResolve(item));
|
|
37
|
+
if (value && typeof value === "object") {
|
|
38
|
+
const obj = {};
|
|
39
|
+
for (const [k, val] of Object.entries(value)) obj[k] = deepResolve(val);
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
return value;
|
|
43
|
+
};
|
|
44
|
+
return deepResolve(this.getJson());
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
//#endregion
|
|
49
|
+
export { FieldType };
|
package/dist/ScalarTypeEnum.js
CHANGED
|
@@ -1 +1,236 @@
|
|
|
1
|
-
import{FieldType
|
|
1
|
+
import { FieldType } from "./FieldType.js";
|
|
2
|
+
import * as z from "zod";
|
|
3
|
+
import { Kind } from "graphql";
|
|
4
|
+
|
|
5
|
+
//#region src/ScalarTypeEnum.ts
|
|
6
|
+
const localeRegex = /^[A-Za-z]{2}(?:-[A-Za-z0-9]{2,8})*$/;
|
|
7
|
+
const timezoneRegex = /^(?:UTC|[A-Za-z_]+\/[A-Za-z_]+)$/;
|
|
8
|
+
const phoneRegex = /^[+]?\d[\d\s().-]{3,}$/;
|
|
9
|
+
const currencyRegex = /^[A-Z]{3}$/;
|
|
10
|
+
const countryRegex = /^[A-Z]{2}$/;
|
|
11
|
+
const latMin = -90;
|
|
12
|
+
const latMax = 90;
|
|
13
|
+
const lonMin = -180;
|
|
14
|
+
const lonMax = 180;
|
|
15
|
+
/**
|
|
16
|
+
* Factory functions for common scalar FieldTypes with zod/GraphQL/JSON Schema.
|
|
17
|
+
*/
|
|
18
|
+
const ScalarTypeEnum = {
|
|
19
|
+
String_unsecure: () => new FieldType({
|
|
20
|
+
name: "String_unsecure",
|
|
21
|
+
description: "Unvalidated string scalar",
|
|
22
|
+
zod: z.string(),
|
|
23
|
+
parseValue: (v) => z.string().parse(v),
|
|
24
|
+
serialize: (v) => String(v),
|
|
25
|
+
parseLiteral: (ast) => {
|
|
26
|
+
if (ast.kind !== Kind.STRING) throw new TypeError("Invalid literal");
|
|
27
|
+
return ast.value;
|
|
28
|
+
},
|
|
29
|
+
jsonSchema: { type: "string" }
|
|
30
|
+
}),
|
|
31
|
+
Int_unsecure: () => new FieldType({
|
|
32
|
+
name: "Int_unsecure",
|
|
33
|
+
description: "Unvalidated integer scalar",
|
|
34
|
+
zod: z.number().int(),
|
|
35
|
+
parseValue: (v) => {
|
|
36
|
+
const num = typeof v === "number" ? v : Number(v);
|
|
37
|
+
return z.number().int().parse(num);
|
|
38
|
+
},
|
|
39
|
+
serialize: (v) => Math.trunc(typeof v === "number" ? v : Number(v)),
|
|
40
|
+
parseLiteral: (ast) => {
|
|
41
|
+
if (ast.kind !== Kind.INT) throw new TypeError("Invalid literal");
|
|
42
|
+
return Number(ast.value);
|
|
43
|
+
},
|
|
44
|
+
jsonSchema: { type: "integer" }
|
|
45
|
+
}),
|
|
46
|
+
Float_unsecure: () => new FieldType({
|
|
47
|
+
name: "Float_unsecure",
|
|
48
|
+
description: "Unvalidated float scalar",
|
|
49
|
+
zod: z.number(),
|
|
50
|
+
parseValue: (v) => {
|
|
51
|
+
const num = typeof v === "number" ? v : Number(v);
|
|
52
|
+
return z.number().parse(num);
|
|
53
|
+
},
|
|
54
|
+
serialize: (v) => Number(v),
|
|
55
|
+
parseLiteral: (ast) => {
|
|
56
|
+
if (ast.kind !== Kind.FLOAT && ast.kind !== Kind.INT) throw new TypeError("Invalid literal");
|
|
57
|
+
return Number(ast.value);
|
|
58
|
+
},
|
|
59
|
+
jsonSchema: { type: "number" }
|
|
60
|
+
}),
|
|
61
|
+
Boolean: () => new FieldType({
|
|
62
|
+
name: "Boolean",
|
|
63
|
+
description: "Unvalidated boolean scalar",
|
|
64
|
+
zod: z.boolean(),
|
|
65
|
+
parseValue: (v) => z.coerce.boolean().parse(v),
|
|
66
|
+
serialize: (v) => Boolean(v),
|
|
67
|
+
parseLiteral: (ast) => {
|
|
68
|
+
if (ast.kind !== Kind.BOOLEAN) throw new TypeError("Invalid literal");
|
|
69
|
+
return ast.value;
|
|
70
|
+
},
|
|
71
|
+
jsonSchema: { type: "boolean" }
|
|
72
|
+
}),
|
|
73
|
+
ID: () => new FieldType({
|
|
74
|
+
name: "ID",
|
|
75
|
+
description: "Unvalidated id scalar",
|
|
76
|
+
zod: z.string(),
|
|
77
|
+
parseValue: (v) => z.string().parse(v),
|
|
78
|
+
serialize: (v) => String(v),
|
|
79
|
+
parseLiteral: (ast) => {
|
|
80
|
+
if (ast.kind !== Kind.STRING) throw new TypeError("Invalid literal");
|
|
81
|
+
return ast.value;
|
|
82
|
+
},
|
|
83
|
+
jsonSchema: { type: "string" }
|
|
84
|
+
}),
|
|
85
|
+
JSON: () => new FieldType({
|
|
86
|
+
name: "JSON",
|
|
87
|
+
zod: z.any(),
|
|
88
|
+
parseValue: (v) => v,
|
|
89
|
+
serialize: (v) => v,
|
|
90
|
+
jsonSchema: {}
|
|
91
|
+
}),
|
|
92
|
+
JSONObject: () => new FieldType({
|
|
93
|
+
name: "JSONObject",
|
|
94
|
+
zod: z.record(z.string(), z.any()),
|
|
95
|
+
parseValue: (v) => z.record(z.string(), z.any()).parse(v),
|
|
96
|
+
serialize: (v) => v ?? {},
|
|
97
|
+
jsonSchema: { type: "object" }
|
|
98
|
+
}),
|
|
99
|
+
Date: () => new FieldType({
|
|
100
|
+
name: "Date",
|
|
101
|
+
zod: z.date(),
|
|
102
|
+
parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
|
|
103
|
+
serialize: (v) => v instanceof Date ? v.toISOString().split("T")[0] : String(v),
|
|
104
|
+
jsonSchema: {
|
|
105
|
+
type: "string",
|
|
106
|
+
format: "date"
|
|
107
|
+
}
|
|
108
|
+
}),
|
|
109
|
+
DateTime: () => new FieldType({
|
|
110
|
+
name: "DateTime",
|
|
111
|
+
zod: z.date(),
|
|
112
|
+
parseValue: (v) => v instanceof Date ? v : new Date(String(v)),
|
|
113
|
+
serialize: (v) => {
|
|
114
|
+
return v instanceof Date ? v.toISOString() : String(v);
|
|
115
|
+
},
|
|
116
|
+
jsonSchema: {
|
|
117
|
+
type: "string",
|
|
118
|
+
format: "date-time"
|
|
119
|
+
}
|
|
120
|
+
}),
|
|
121
|
+
Time: () => new FieldType({
|
|
122
|
+
name: "Time",
|
|
123
|
+
zod: z.string().regex(/^\d{2}:\d{2}(:\d{2})?$/),
|
|
124
|
+
parseValue: (v) => z.string().regex(/^\d{2}:\d{2}(:\d{2})?$/).parse(v),
|
|
125
|
+
serialize: (v) => String(v),
|
|
126
|
+
jsonSchema: {
|
|
127
|
+
type: "string",
|
|
128
|
+
pattern: "^\\d{2}:\\d{2}(:\\d{2})?$"
|
|
129
|
+
}
|
|
130
|
+
}),
|
|
131
|
+
EmailAddress: () => new FieldType({
|
|
132
|
+
name: "EmailAddress",
|
|
133
|
+
zod: z.string().email(),
|
|
134
|
+
parseValue: (v) => z.string().email().parse(v),
|
|
135
|
+
serialize: (v) => String(v),
|
|
136
|
+
jsonSchema: {
|
|
137
|
+
type: "string",
|
|
138
|
+
format: "email"
|
|
139
|
+
}
|
|
140
|
+
}),
|
|
141
|
+
URL: () => new FieldType({
|
|
142
|
+
name: "URL",
|
|
143
|
+
zod: z.string().url(),
|
|
144
|
+
parseValue: (v) => z.string().url().parse(v),
|
|
145
|
+
serialize: (v) => String(v),
|
|
146
|
+
jsonSchema: {
|
|
147
|
+
type: "string",
|
|
148
|
+
format: "uri"
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
PhoneNumber: () => new FieldType({
|
|
152
|
+
name: "PhoneNumber",
|
|
153
|
+
zod: z.string().regex(phoneRegex),
|
|
154
|
+
parseValue: (v) => z.string().regex(phoneRegex).parse(v),
|
|
155
|
+
serialize: (v) => String(v),
|
|
156
|
+
jsonSchema: {
|
|
157
|
+
type: "string",
|
|
158
|
+
pattern: phoneRegex.source
|
|
159
|
+
}
|
|
160
|
+
}),
|
|
161
|
+
NonEmptyString: () => new FieldType({
|
|
162
|
+
name: "NonEmptyString",
|
|
163
|
+
zod: z.string().min(1),
|
|
164
|
+
parseValue: (v) => z.string().min(1).parse(v),
|
|
165
|
+
serialize: (v) => String(v),
|
|
166
|
+
jsonSchema: {
|
|
167
|
+
type: "string",
|
|
168
|
+
minLength: 1
|
|
169
|
+
}
|
|
170
|
+
}),
|
|
171
|
+
Locale: () => new FieldType({
|
|
172
|
+
name: "Locale",
|
|
173
|
+
zod: z.string().regex(localeRegex),
|
|
174
|
+
parseValue: (v) => z.string().regex(localeRegex).parse(v),
|
|
175
|
+
serialize: (v) => String(v),
|
|
176
|
+
jsonSchema: {
|
|
177
|
+
type: "string",
|
|
178
|
+
pattern: localeRegex.source
|
|
179
|
+
}
|
|
180
|
+
}),
|
|
181
|
+
TimeZone: () => new FieldType({
|
|
182
|
+
name: "TimeZone",
|
|
183
|
+
zod: z.string().regex(timezoneRegex),
|
|
184
|
+
parseValue: (v) => z.string().regex(timezoneRegex).parse(v),
|
|
185
|
+
serialize: (v) => String(v),
|
|
186
|
+
jsonSchema: {
|
|
187
|
+
type: "string",
|
|
188
|
+
pattern: timezoneRegex.source
|
|
189
|
+
}
|
|
190
|
+
}),
|
|
191
|
+
Latitude: () => new FieldType({
|
|
192
|
+
name: "Latitude",
|
|
193
|
+
zod: z.number().min(latMin).max(latMax),
|
|
194
|
+
parseValue: (v) => z.coerce.number().min(latMin).max(latMax).parse(v),
|
|
195
|
+
serialize: (v) => Number(v),
|
|
196
|
+
jsonSchema: {
|
|
197
|
+
type: "number",
|
|
198
|
+
minimum: latMin,
|
|
199
|
+
maximum: latMax
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
Longitude: () => new FieldType({
|
|
203
|
+
name: "Longitude",
|
|
204
|
+
zod: z.number().min(lonMin).max(lonMax),
|
|
205
|
+
parseValue: (v) => z.coerce.number().min(lonMin).max(lonMax).parse(v),
|
|
206
|
+
serialize: (v) => Number(v),
|
|
207
|
+
jsonSchema: {
|
|
208
|
+
type: "number",
|
|
209
|
+
minimum: lonMin,
|
|
210
|
+
maximum: lonMax
|
|
211
|
+
}
|
|
212
|
+
}),
|
|
213
|
+
Currency: () => new FieldType({
|
|
214
|
+
name: "Currency",
|
|
215
|
+
zod: z.string().regex(currencyRegex),
|
|
216
|
+
parseValue: (v) => z.string().regex(currencyRegex).parse(v),
|
|
217
|
+
serialize: (v) => String(v),
|
|
218
|
+
jsonSchema: {
|
|
219
|
+
type: "string",
|
|
220
|
+
pattern: currencyRegex.source
|
|
221
|
+
}
|
|
222
|
+
}),
|
|
223
|
+
CountryCode: () => new FieldType({
|
|
224
|
+
name: "CountryCode",
|
|
225
|
+
zod: z.string().regex(countryRegex),
|
|
226
|
+
parseValue: (v) => z.string().regex(countryRegex).parse(v),
|
|
227
|
+
serialize: (v) => String(v),
|
|
228
|
+
jsonSchema: {
|
|
229
|
+
type: "string",
|
|
230
|
+
pattern: countryRegex.source
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
export { ScalarTypeEnum };
|
package/dist/SchemaModel.js
CHANGED
|
@@ -1 +1,39 @@
|
|
|
1
|
-
import"./EnumType.js";
|
|
1
|
+
import "./EnumType.js";
|
|
2
|
+
import "./FieldType.js";
|
|
3
|
+
import * as z from "zod";
|
|
4
|
+
|
|
5
|
+
//#region src/SchemaModel.ts
|
|
6
|
+
/**
|
|
7
|
+
* Named object model built from FieldType/EnumType/SchemaModel fields.
|
|
8
|
+
* Provides zod and GraphQL input helpers, and supports arrays/optional fields.
|
|
9
|
+
*/
|
|
10
|
+
var SchemaModel = class {
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Build a typed ZodObject from the model fields, preserving each field's
|
|
16
|
+
* Zod schema and optionality at the type level when possible.
|
|
17
|
+
*/
|
|
18
|
+
getZod() {
|
|
19
|
+
const shape = Object.entries(this.config.fields).reduce((acc, [key, def]) => {
|
|
20
|
+
const base = def.type.getZod();
|
|
21
|
+
const withArray = def.isArray ? z.array(base) : base;
|
|
22
|
+
acc[key] = def.isOptional ? withArray.optional() : withArray;
|
|
23
|
+
return acc;
|
|
24
|
+
}, {});
|
|
25
|
+
return z.object(shape);
|
|
26
|
+
}
|
|
27
|
+
/** Input object name for GraphQL builder adapters. */
|
|
28
|
+
getPothosInput() {
|
|
29
|
+
return this.config.name;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Helper to define a SchemaModel with type inference.
|
|
34
|
+
* Equivalent to `new SchemaModel(config)` but with better ergonomics.
|
|
35
|
+
*/
|
|
36
|
+
const defineSchemaModel = (config) => new SchemaModel(config);
|
|
37
|
+
|
|
38
|
+
//#endregion
|
|
39
|
+
export { SchemaModel, defineSchemaModel };
|
|
@@ -1 +1,236 @@
|
|
|
1
|
-
import*as
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/entity/defineEntity.ts
|
|
4
|
+
/**
|
|
5
|
+
* Helper to define a database entity with full type safety.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* const UserEntity = defineEntity({
|
|
10
|
+
* name: 'User',
|
|
11
|
+
* schema: 'lssm_sigil',
|
|
12
|
+
* description: 'A user of the platform.',
|
|
13
|
+
* fields: {
|
|
14
|
+
* id: field.id(),
|
|
15
|
+
* email: field.string({ isUnique: true, zod: z.string().email() }),
|
|
16
|
+
* name: field.string({ isOptional: true }),
|
|
17
|
+
* createdAt: field.createdAt(),
|
|
18
|
+
* updatedAt: field.updatedAt(),
|
|
19
|
+
* memberships: field.hasMany('Member'),
|
|
20
|
+
* },
|
|
21
|
+
* indexes: [{ fields: ['email'], unique: true }],
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function defineEntity(spec) {
|
|
26
|
+
return spec;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Helper to define an enum that can be shared across entities.
|
|
30
|
+
*/
|
|
31
|
+
function defineEntityEnum(def) {
|
|
32
|
+
return def;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Field builder helpers for common field patterns.
|
|
36
|
+
*/
|
|
37
|
+
const field = {
|
|
38
|
+
string(opts) {
|
|
39
|
+
return {
|
|
40
|
+
kind: "scalar",
|
|
41
|
+
type: "String",
|
|
42
|
+
...opts
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
int(opts) {
|
|
46
|
+
return {
|
|
47
|
+
kind: "scalar",
|
|
48
|
+
type: "Int",
|
|
49
|
+
...opts
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
float(opts) {
|
|
53
|
+
return {
|
|
54
|
+
kind: "scalar",
|
|
55
|
+
type: "Float",
|
|
56
|
+
...opts
|
|
57
|
+
};
|
|
58
|
+
},
|
|
59
|
+
boolean(opts) {
|
|
60
|
+
return {
|
|
61
|
+
kind: "scalar",
|
|
62
|
+
type: "Boolean",
|
|
63
|
+
...opts
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
dateTime(opts) {
|
|
67
|
+
return {
|
|
68
|
+
kind: "scalar",
|
|
69
|
+
type: "DateTime",
|
|
70
|
+
...opts
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
json(opts) {
|
|
74
|
+
return {
|
|
75
|
+
kind: "scalar",
|
|
76
|
+
type: "Json",
|
|
77
|
+
...opts
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
bigInt(opts) {
|
|
81
|
+
return {
|
|
82
|
+
kind: "scalar",
|
|
83
|
+
type: "BigInt",
|
|
84
|
+
...opts
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
decimal(opts) {
|
|
88
|
+
return {
|
|
89
|
+
kind: "scalar",
|
|
90
|
+
type: "Decimal",
|
|
91
|
+
...opts
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
bytes(opts) {
|
|
95
|
+
return {
|
|
96
|
+
kind: "scalar",
|
|
97
|
+
type: "Bytes",
|
|
98
|
+
...opts
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
id(opts) {
|
|
102
|
+
return {
|
|
103
|
+
kind: "scalar",
|
|
104
|
+
type: "String",
|
|
105
|
+
isId: true,
|
|
106
|
+
default: "cuid()",
|
|
107
|
+
...opts
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
uuid(opts) {
|
|
111
|
+
return {
|
|
112
|
+
kind: "scalar",
|
|
113
|
+
type: "String",
|
|
114
|
+
isId: true,
|
|
115
|
+
default: "uuid()",
|
|
116
|
+
...opts
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
autoIncrement(opts) {
|
|
120
|
+
return {
|
|
121
|
+
kind: "scalar",
|
|
122
|
+
type: "Int",
|
|
123
|
+
isId: true,
|
|
124
|
+
default: "autoincrement()",
|
|
125
|
+
...opts
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
createdAt(opts) {
|
|
129
|
+
return {
|
|
130
|
+
kind: "scalar",
|
|
131
|
+
type: "DateTime",
|
|
132
|
+
default: "now()",
|
|
133
|
+
...opts
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
updatedAt(opts) {
|
|
137
|
+
return {
|
|
138
|
+
kind: "scalar",
|
|
139
|
+
type: "DateTime",
|
|
140
|
+
updatedAt: true,
|
|
141
|
+
...opts
|
|
142
|
+
};
|
|
143
|
+
},
|
|
144
|
+
email(opts) {
|
|
145
|
+
return {
|
|
146
|
+
kind: "scalar",
|
|
147
|
+
type: "String",
|
|
148
|
+
zod: z.email(),
|
|
149
|
+
...opts
|
|
150
|
+
};
|
|
151
|
+
},
|
|
152
|
+
url(opts) {
|
|
153
|
+
return {
|
|
154
|
+
kind: "scalar",
|
|
155
|
+
type: "String",
|
|
156
|
+
zod: z.url(),
|
|
157
|
+
...opts
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
enum(enumName, opts) {
|
|
161
|
+
return {
|
|
162
|
+
kind: "enum",
|
|
163
|
+
enumName,
|
|
164
|
+
...opts
|
|
165
|
+
};
|
|
166
|
+
},
|
|
167
|
+
inlineEnum(enumName, values, opts) {
|
|
168
|
+
return {
|
|
169
|
+
kind: "enum",
|
|
170
|
+
enumName,
|
|
171
|
+
values,
|
|
172
|
+
...opts
|
|
173
|
+
};
|
|
174
|
+
},
|
|
175
|
+
hasOne(target, opts) {
|
|
176
|
+
return {
|
|
177
|
+
kind: "relation",
|
|
178
|
+
type: "hasOne",
|
|
179
|
+
target,
|
|
180
|
+
...opts
|
|
181
|
+
};
|
|
182
|
+
},
|
|
183
|
+
hasMany(target, opts) {
|
|
184
|
+
return {
|
|
185
|
+
kind: "relation",
|
|
186
|
+
type: "hasMany",
|
|
187
|
+
target,
|
|
188
|
+
...opts
|
|
189
|
+
};
|
|
190
|
+
},
|
|
191
|
+
belongsTo(target, fields, references, opts) {
|
|
192
|
+
return {
|
|
193
|
+
kind: "relation",
|
|
194
|
+
type: "belongsTo",
|
|
195
|
+
target,
|
|
196
|
+
fields,
|
|
197
|
+
references,
|
|
198
|
+
...opts
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
foreignKey(opts) {
|
|
202
|
+
return {
|
|
203
|
+
kind: "scalar",
|
|
204
|
+
type: "String",
|
|
205
|
+
...opts
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Index builder helpers.
|
|
211
|
+
*/
|
|
212
|
+
const index = {
|
|
213
|
+
on(fields, opts) {
|
|
214
|
+
return {
|
|
215
|
+
fields,
|
|
216
|
+
...opts
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
unique(fields, opts) {
|
|
220
|
+
return {
|
|
221
|
+
fields,
|
|
222
|
+
unique: true,
|
|
223
|
+
...opts
|
|
224
|
+
};
|
|
225
|
+
},
|
|
226
|
+
compound(fields, sort, opts) {
|
|
227
|
+
return {
|
|
228
|
+
fields,
|
|
229
|
+
sort,
|
|
230
|
+
...opts
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
//#endregion
|
|
236
|
+
export { defineEntity, defineEntityEnum, field, index };
|
package/dist/entity/generator.js
CHANGED
|
@@ -1,4 +1,217 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region src/entity/generator.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generate Prisma schema content from entity specifications.
|
|
4
|
+
*/
|
|
5
|
+
function generatePrismaSchema(entities, options = {}) {
|
|
6
|
+
const { provider = "postgresql", clientOutput = "../../src/generated/prisma", includePothos = true, pothosOutput = "../../src/generated/pothos-types.ts" } = options;
|
|
7
|
+
const lines = [];
|
|
8
|
+
const schemas = /* @__PURE__ */ new Set();
|
|
9
|
+
entities.forEach((entity) => {
|
|
10
|
+
if (entity.schema) schemas.add(entity.schema);
|
|
11
|
+
});
|
|
12
|
+
const schemaList = schemas.size > 0 ? Array.from(schemas) : ["public"];
|
|
13
|
+
lines.push("datasource db {");
|
|
14
|
+
lines.push(` provider = "${provider}"`);
|
|
15
|
+
if (schemas.size > 0) lines.push(` schemas = [${schemaList.map((s) => `"${s}"`).join(", ")}]`);
|
|
16
|
+
lines.push("}");
|
|
17
|
+
lines.push("");
|
|
18
|
+
lines.push("generator client {");
|
|
19
|
+
lines.push(" provider = \"prisma-client\"");
|
|
20
|
+
lines.push(` output = "${clientOutput}"`);
|
|
21
|
+
lines.push("");
|
|
22
|
+
lines.push(" engineType = \"client\"");
|
|
23
|
+
lines.push(" runtime = \"bun\"");
|
|
24
|
+
lines.push(" moduleFormat = \"esm\"");
|
|
25
|
+
lines.push(" generatedFileExtension = \"ts\"");
|
|
26
|
+
lines.push(" importFileExtension = \"ts\"");
|
|
27
|
+
lines.push("}");
|
|
28
|
+
lines.push("");
|
|
29
|
+
if (includePothos) {
|
|
30
|
+
lines.push("generator pothos {");
|
|
31
|
+
lines.push(" provider = \"prisma-pothos-types\"");
|
|
32
|
+
lines.push(" clientOutput = \"./prisma\"");
|
|
33
|
+
lines.push(` output = "${pothosOutput}"`);
|
|
34
|
+
lines.push(" generateDatamodel = true");
|
|
35
|
+
lines.push(" documentation = false");
|
|
36
|
+
lines.push("}");
|
|
37
|
+
lines.push("");
|
|
38
|
+
}
|
|
39
|
+
const enumMap = /* @__PURE__ */ new Map();
|
|
40
|
+
entities.forEach((entity) => {
|
|
41
|
+
entity.enums?.forEach((enumDef) => {
|
|
42
|
+
if (!enumMap.has(enumDef.name)) enumMap.set(enumDef.name, enumDef);
|
|
43
|
+
});
|
|
44
|
+
Object.values(entity.fields).forEach((field) => {
|
|
45
|
+
if (field.kind === "enum" && field.values && !enumMap.has(field.enumName)) enumMap.set(field.enumName, {
|
|
46
|
+
name: field.enumName,
|
|
47
|
+
values: field.values,
|
|
48
|
+
schema: entity.schema
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
enumMap.forEach((enumDef) => {
|
|
53
|
+
lines.push(...generateEnumBlock(enumDef));
|
|
54
|
+
lines.push("");
|
|
55
|
+
});
|
|
56
|
+
entities.forEach((entity) => {
|
|
57
|
+
lines.push(...generateModelBlock(entity));
|
|
58
|
+
lines.push("");
|
|
59
|
+
});
|
|
60
|
+
return lines.join("\n");
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Generate Prisma enum block.
|
|
64
|
+
*/
|
|
65
|
+
function generateEnumBlock(enumDef) {
|
|
66
|
+
const lines = [];
|
|
67
|
+
if (enumDef.description) lines.push(`/// ${enumDef.description}`);
|
|
68
|
+
lines.push(`enum ${enumDef.name} {`);
|
|
69
|
+
enumDef.values.forEach((value) => {
|
|
70
|
+
lines.push(` ${value}`);
|
|
71
|
+
});
|
|
72
|
+
lines.push("");
|
|
73
|
+
if (enumDef.schema) lines.push(` @@schema("${enumDef.schema}")`);
|
|
74
|
+
lines.push("}");
|
|
75
|
+
return lines;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Generate Prisma model block from entity spec.
|
|
79
|
+
*/
|
|
80
|
+
function generateModelBlock(entity) {
|
|
81
|
+
const lines = [];
|
|
82
|
+
if (entity.description) lines.push(`/// ${entity.description}`);
|
|
83
|
+
lines.push(`model ${entity.name} {`);
|
|
84
|
+
Object.entries(entity.fields).forEach(([fieldName, field]) => {
|
|
85
|
+
const fieldLine = generateFieldLine(fieldName, field);
|
|
86
|
+
if (field.description) lines.push(` /// ${field.description}`);
|
|
87
|
+
lines.push(` ${fieldLine}`);
|
|
88
|
+
});
|
|
89
|
+
if (entity.indexes && entity.indexes.length > 0) {
|
|
90
|
+
lines.push("");
|
|
91
|
+
entity.indexes.forEach((idx) => {
|
|
92
|
+
lines.push(` ${generateIndexLine(idx)}`);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (entity.map) lines.push(` @@map("${entity.map}")`);
|
|
96
|
+
if (entity.schema) lines.push(` @@schema("${entity.schema}")`);
|
|
97
|
+
lines.push("}");
|
|
98
|
+
return lines;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Generate a single field line.
|
|
102
|
+
*/
|
|
103
|
+
function generateFieldLine(fieldName, field) {
|
|
104
|
+
if (field.kind === "scalar") return generateScalarFieldLine(fieldName, field);
|
|
105
|
+
else if (field.kind === "enum") return generateEnumFieldLine(fieldName, field);
|
|
106
|
+
else if (field.kind === "relation") return generateRelationFieldLine(fieldName, field);
|
|
107
|
+
throw new Error(`Unknown field kind: ${field.kind}`);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Generate scalar field line.
|
|
111
|
+
*/
|
|
112
|
+
function generateScalarFieldLine(fieldName, field) {
|
|
113
|
+
const parts = [fieldName];
|
|
114
|
+
let typeStr = field.type;
|
|
115
|
+
if (field.isArray) typeStr += "[]";
|
|
116
|
+
if (field.isOptional) typeStr += "?";
|
|
117
|
+
parts.push(typeStr);
|
|
118
|
+
const attrs = [];
|
|
119
|
+
if (field.isId) attrs.push("@id");
|
|
120
|
+
if (field.default !== void 0) if (typeof field.default === "string" && field.default.includes("(")) attrs.push(`@default(${field.default})`);
|
|
121
|
+
else if (typeof field.default === "boolean") attrs.push(`@default(${field.default})`);
|
|
122
|
+
else if (typeof field.default === "number") attrs.push(`@default(${field.default})`);
|
|
123
|
+
else attrs.push(`@default("${field.default}")`);
|
|
124
|
+
if (field.updatedAt) attrs.push("@updatedAt");
|
|
125
|
+
if (field.isUnique) attrs.push("@unique");
|
|
126
|
+
if (field.map) attrs.push(`@map("${field.map}")`);
|
|
127
|
+
if (field.dbType) attrs.push(`@db.${field.dbType}`);
|
|
128
|
+
if (attrs.length > 0) parts.push(attrs.join(" "));
|
|
129
|
+
return parts.join(" ");
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Generate enum field line.
|
|
133
|
+
*/
|
|
134
|
+
function generateEnumFieldLine(fieldName, field) {
|
|
135
|
+
const parts = [fieldName];
|
|
136
|
+
let typeStr = field.enumName;
|
|
137
|
+
if (field.isArray) typeStr += "[]";
|
|
138
|
+
if (field.isOptional) typeStr += "?";
|
|
139
|
+
parts.push(typeStr);
|
|
140
|
+
const attrs = [];
|
|
141
|
+
if (field.default !== void 0) attrs.push(`@default(${field.default})`);
|
|
142
|
+
if (field.isUnique) attrs.push("@unique");
|
|
143
|
+
if (field.map) attrs.push(`@map("${field.map}")`);
|
|
144
|
+
if (attrs.length > 0) parts.push(attrs.join(" "));
|
|
145
|
+
return parts.join(" ");
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Generate relation field line.
|
|
149
|
+
*/
|
|
150
|
+
function generateRelationFieldLine(fieldName, field) {
|
|
151
|
+
const parts = [fieldName];
|
|
152
|
+
let typeStr = field.target;
|
|
153
|
+
if (field.type === "hasMany") typeStr += "[]";
|
|
154
|
+
if (field.type === "hasOne") typeStr += "?";
|
|
155
|
+
parts.push(typeStr);
|
|
156
|
+
const relationParts = [];
|
|
157
|
+
if (field.name) relationParts.push(`name: "${field.name}"`);
|
|
158
|
+
if (field.fields && field.fields.length > 0) relationParts.push(`fields: [${field.fields.join(", ")}]`);
|
|
159
|
+
if (field.references && field.references.length > 0) relationParts.push(`references: [${field.references.join(", ")}]`);
|
|
160
|
+
if (field.onDelete) relationParts.push(`onDelete: ${field.onDelete}`);
|
|
161
|
+
if (field.onUpdate) relationParts.push(`onUpdate: ${field.onUpdate}`);
|
|
162
|
+
if (relationParts.length > 0) parts.push(`@relation(${relationParts.join(", ")})`);
|
|
163
|
+
return parts.join(" ");
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Generate index line.
|
|
167
|
+
*/
|
|
168
|
+
function generateIndexLine(idx) {
|
|
169
|
+
const fieldList = idx.fields.join(", ");
|
|
170
|
+
const parts = [];
|
|
171
|
+
if (idx.unique) parts.push(`@@unique([${fieldList}]`);
|
|
172
|
+
else parts.push(`@@index([${fieldList}]`);
|
|
173
|
+
const options = [];
|
|
174
|
+
if (idx.name) options.push(`name: "${idx.name}"`);
|
|
175
|
+
if (idx.type) options.push(`type: ${idx.type}`);
|
|
176
|
+
if (options.length > 0) parts[0] += `, ${options.join(", ")}`;
|
|
177
|
+
parts[0] += ")";
|
|
178
|
+
return parts.join("");
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Compose multiple module schema contributions into a single schema.
|
|
182
|
+
*/
|
|
183
|
+
function composeModuleSchemas(contributions, options = {}) {
|
|
184
|
+
const allEntities = [];
|
|
185
|
+
const allEnums = /* @__PURE__ */ new Map();
|
|
186
|
+
contributions.forEach((contrib) => {
|
|
187
|
+
contrib.entities.forEach((entity) => {
|
|
188
|
+
allEntities.push({
|
|
189
|
+
...entity,
|
|
190
|
+
module: contrib.moduleId
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
contrib.enums?.forEach((enumDef) => {
|
|
194
|
+
if (!allEnums.has(enumDef.name)) allEnums.set(enumDef.name, enumDef);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
if (allEntities.length > 0 && allEnums.size > 0) allEntities[0] = {
|
|
198
|
+
...allEntities[0],
|
|
199
|
+
enums: [...allEntities[0].enums ?? [], ...allEnums.values()]
|
|
200
|
+
};
|
|
201
|
+
return generatePrismaSchema(allEntities, options);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Generate a single entity's Prisma schema fragment (for modular output).
|
|
205
|
+
*/
|
|
206
|
+
function generateEntityFragment(entity) {
|
|
207
|
+
return generateModelBlock(entity).join("\n");
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Generate a single enum's Prisma schema fragment (for modular output).
|
|
211
|
+
*/
|
|
212
|
+
function generateEnumFragment(enumDef) {
|
|
213
|
+
return generateEnumBlock(enumDef).join("\n");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
//#endregion
|
|
217
|
+
export { composeModuleSchemas, generateEntityFragment, generateEnumFragment, generatePrismaSchema };
|
package/dist/entity/index.js
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
import"./types.js";
|
|
1
|
+
import "./types.js";
|
|
2
|
+
import { defineEntity, defineEntityEnum, field, index } from "./defineEntity.js";
|
|
3
|
+
import { composeModuleSchemas, generateEntityFragment, generateEnumFragment, generatePrismaSchema } from "./generator.js";
|
|
4
|
+
|
|
5
|
+
export { composeModuleSchemas, defineEntity, defineEntityEnum, field, generateEntityFragment, generateEnumFragment, generatePrismaSchema, index };
|
package/dist/entity/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"zod";
|
|
1
|
+
import "zod";
|
package/dist/index.js
CHANGED
|
@@ -1 +1,9 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { EnumType, defineEnum } from "./EnumType.js";
|
|
2
|
+
import { FieldType } from "./FieldType.js";
|
|
3
|
+
import { ScalarTypeEnum } from "./ScalarTypeEnum.js";
|
|
4
|
+
import { SchemaModel, defineSchemaModel } from "./SchemaModel.js";
|
|
5
|
+
import { defineEntity, defineEntityEnum, field, index } from "./entity/defineEntity.js";
|
|
6
|
+
import { composeModuleSchemas, generateEntityFragment, generateEnumFragment, generatePrismaSchema } from "./entity/generator.js";
|
|
7
|
+
import "./entity/index.js";
|
|
8
|
+
|
|
9
|
+
export { EnumType, FieldType, ScalarTypeEnum, SchemaModel, composeModuleSchemas, defineEntity, defineEntityEnum, defineEnum, defineSchemaModel, field, generateEntityFragment, generateEnumFragment, generatePrismaSchema, index };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lssm/lib.schema",
|
|
3
|
-
"version": "0.0.0-canary-
|
|
3
|
+
"version": "0.0.0-canary-20251217072406",
|
|
4
4
|
"scripts": {
|
|
5
5
|
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
6
6
|
"publish:pkg:canary": "bun publish:pkg --tag canary",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"lint:check": "eslint src"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@lssm/tool.tsdown": "0.0.0-canary-
|
|
18
|
-
"@lssm/tool.typescript": "0.0.0-canary-
|
|
17
|
+
"@lssm/tool.tsdown": "0.0.0-canary-20251217072406",
|
|
18
|
+
"@lssm/tool.typescript": "0.0.0-canary-20251217072406",
|
|
19
19
|
"tsdown": "^0.17.4",
|
|
20
20
|
"typescript": "^5.9.3"
|
|
21
21
|
},
|