@lssm/lib.schema 0.0.0-canary-20251217083314 → 0.0.0-canary-20251220002821

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.
@@ -1,217 +0,0 @@
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 };
@@ -1,5 +0,0 @@
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 };
@@ -1 +0,0 @@
1
- import "zod";