@famgia/omnify-laravel 0.0.8 → 0.0.10
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/{chunk-2XKSPWNJ.js → chunk-UEJWSSDU.js} +157 -22
- package/dist/chunk-UEJWSSDU.js.map +1 -0
- package/dist/index.cjs +156 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/plugin.cjs +136 -19
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +1 -1
- package/package.json +4 -4
- package/dist/chunk-2XKSPWNJ.js.map +0 -1
package/dist/plugin.cjs
CHANGED
|
@@ -31,6 +31,7 @@ var TYPE_METHOD_MAP = {
|
|
|
31
31
|
Int: "integer",
|
|
32
32
|
BigInt: "bigInteger",
|
|
33
33
|
Float: "double",
|
|
34
|
+
Decimal: "decimal",
|
|
34
35
|
Boolean: "boolean",
|
|
35
36
|
Text: "text",
|
|
36
37
|
LongText: "longText",
|
|
@@ -52,6 +53,12 @@ var PK_METHOD_MAP = {
|
|
|
52
53
|
Uuid: "uuid",
|
|
53
54
|
String: "string"
|
|
54
55
|
};
|
|
56
|
+
function getIdType(schema) {
|
|
57
|
+
return schema.options?.idType ?? "BigInt";
|
|
58
|
+
}
|
|
59
|
+
function hasAutoId(schema) {
|
|
60
|
+
return schema.options?.id !== false;
|
|
61
|
+
}
|
|
55
62
|
function toColumnName(propertyName) {
|
|
56
63
|
return propertyName.replace(/([A-Z])/g, "_$1").toLowerCase().replace(/^_/, "");
|
|
57
64
|
}
|
|
@@ -77,6 +84,12 @@ function propertyToColumnMethod(propertyName, property) {
|
|
|
77
84
|
if (method === "string" && propWithLength.length) {
|
|
78
85
|
args.push(propWithLength.length);
|
|
79
86
|
}
|
|
87
|
+
if (property.type === "Decimal") {
|
|
88
|
+
const decimalProp = property;
|
|
89
|
+
const precision = decimalProp.precision ?? 8;
|
|
90
|
+
const scale = decimalProp.scale ?? 2;
|
|
91
|
+
args.push(precision, scale);
|
|
92
|
+
}
|
|
80
93
|
if (property.type === "Enum") {
|
|
81
94
|
const enumProp = property;
|
|
82
95
|
if (enumProp.enum && enumProp.enum.length > 0) {
|
|
@@ -153,6 +166,54 @@ function generateSoftDeleteColumn() {
|
|
|
153
166
|
modifiers: [{ method: "nullable" }]
|
|
154
167
|
};
|
|
155
168
|
}
|
|
169
|
+
function generatePolymorphicColumns(propertyName, property, allSchemas) {
|
|
170
|
+
if (property.type !== "Association") {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
const assocProp = property;
|
|
174
|
+
if (assocProp.relation !== "MorphTo") {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
const targets = assocProp.targets;
|
|
178
|
+
if (!targets || targets.length === 0) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
const columnBaseName = toColumnName(propertyName);
|
|
182
|
+
const typeColumnName = `${columnBaseName}_type`;
|
|
183
|
+
const idColumnName = `${columnBaseName}_id`;
|
|
184
|
+
const typeColumn = {
|
|
185
|
+
name: typeColumnName,
|
|
186
|
+
method: "enum",
|
|
187
|
+
args: [typeColumnName, targets],
|
|
188
|
+
modifiers: [{ method: "nullable" }]
|
|
189
|
+
};
|
|
190
|
+
let idMethod = "unsignedBigInteger";
|
|
191
|
+
for (const targetName of targets) {
|
|
192
|
+
const targetSchema = allSchemas[targetName];
|
|
193
|
+
if (targetSchema) {
|
|
194
|
+
const targetIdType = targetSchema.options?.idType ?? "BigInt";
|
|
195
|
+
if (targetIdType === "Uuid") {
|
|
196
|
+
idMethod = "uuid";
|
|
197
|
+
break;
|
|
198
|
+
} else if (targetIdType === "String") {
|
|
199
|
+
idMethod = "string";
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const idColumn = {
|
|
204
|
+
name: idColumnName,
|
|
205
|
+
method: idMethod,
|
|
206
|
+
args: idMethod === "string" ? [idColumnName, 255] : [idColumnName],
|
|
207
|
+
modifiers: [{ method: "nullable" }]
|
|
208
|
+
};
|
|
209
|
+
const indexes = [
|
|
210
|
+
{
|
|
211
|
+
columns: [typeColumnName, idColumnName],
|
|
212
|
+
unique: false
|
|
213
|
+
}
|
|
214
|
+
];
|
|
215
|
+
return { typeColumn, idColumn, indexes };
|
|
216
|
+
}
|
|
156
217
|
function generateForeignKey(propertyName, property, allSchemas) {
|
|
157
218
|
if (property.type !== "Association") {
|
|
158
219
|
return null;
|
|
@@ -167,7 +228,7 @@ function generateForeignKey(propertyName, property, allSchemas) {
|
|
|
167
228
|
const columnName = toColumnName(propertyName) + "_id";
|
|
168
229
|
const targetSchema = assocProp.target ? allSchemas[assocProp.target] : void 0;
|
|
169
230
|
const targetTable = assocProp.target ? toTableName(assocProp.target) : "unknown";
|
|
170
|
-
const targetPkType = targetSchema
|
|
231
|
+
const targetPkType = targetSchema ? getIdType(targetSchema) : "BigInt";
|
|
171
232
|
let method = "unsignedBigInteger";
|
|
172
233
|
if (targetPkType === "Int") {
|
|
173
234
|
method = "unsignedInteger";
|
|
@@ -200,8 +261,10 @@ function schemaToBlueprint(schema, allSchemas) {
|
|
|
200
261
|
const columns = [];
|
|
201
262
|
const foreignKeys = [];
|
|
202
263
|
const indexes = [];
|
|
203
|
-
|
|
204
|
-
|
|
264
|
+
if (hasAutoId(schema)) {
|
|
265
|
+
const pkType = getIdType(schema);
|
|
266
|
+
columns.push(generatePrimaryKeyColumn(pkType));
|
|
267
|
+
}
|
|
205
268
|
if (schema.properties) {
|
|
206
269
|
for (const [propName, property] of Object.entries(schema.properties)) {
|
|
207
270
|
const columnMethod = propertyToColumnMethod(propName, property);
|
|
@@ -214,6 +277,12 @@ function schemaToBlueprint(schema, allSchemas) {
|
|
|
214
277
|
foreignKeys.push(fkResult.foreignKey);
|
|
215
278
|
indexes.push(fkResult.index);
|
|
216
279
|
}
|
|
280
|
+
const polyResult = generatePolymorphicColumns(propName, property, allSchemas);
|
|
281
|
+
if (polyResult) {
|
|
282
|
+
columns.push(polyResult.typeColumn);
|
|
283
|
+
columns.push(polyResult.idColumn);
|
|
284
|
+
indexes.push(...polyResult.indexes);
|
|
285
|
+
}
|
|
217
286
|
}
|
|
218
287
|
}
|
|
219
288
|
if (schema.options?.timestamps !== false) {
|
|
@@ -307,7 +376,7 @@ function extractManyToManyRelations(schema, allSchemas) {
|
|
|
307
376
|
return pivotTables;
|
|
308
377
|
}
|
|
309
378
|
const sourceTable = toTableName(schema.name);
|
|
310
|
-
const sourcePkType = schema
|
|
379
|
+
const sourcePkType = getIdType(schema);
|
|
311
380
|
for (const [, property] of Object.entries(schema.properties)) {
|
|
312
381
|
if (property.type !== "Association") {
|
|
313
382
|
continue;
|
|
@@ -322,7 +391,7 @@ function extractManyToManyRelations(schema, allSchemas) {
|
|
|
322
391
|
}
|
|
323
392
|
const targetSchema = allSchemas[targetName];
|
|
324
393
|
const targetTable = toTableName(targetName);
|
|
325
|
-
const targetPkType = targetSchema
|
|
394
|
+
const targetPkType = targetSchema ? getIdType(targetSchema) : "BigInt";
|
|
326
395
|
const isOwningSide = assocProp.owning ?? schema.name < targetName;
|
|
327
396
|
if (!isOwningSide) {
|
|
328
397
|
continue;
|
|
@@ -653,12 +722,25 @@ function getPropertyType(property, _allSchemas) {
|
|
|
653
722
|
const assocProp = property;
|
|
654
723
|
const targetName = assocProp.target ?? "unknown";
|
|
655
724
|
switch (assocProp.relation) {
|
|
725
|
+
// Standard relations
|
|
656
726
|
case "OneToOne":
|
|
657
727
|
case "ManyToOne":
|
|
658
728
|
return targetName;
|
|
659
729
|
case "OneToMany":
|
|
660
730
|
case "ManyToMany":
|
|
661
731
|
return `${targetName}[]`;
|
|
732
|
+
// Polymorphic relations
|
|
733
|
+
case "MorphTo":
|
|
734
|
+
if (assocProp.targets && assocProp.targets.length > 0) {
|
|
735
|
+
return assocProp.targets.join(" | ");
|
|
736
|
+
}
|
|
737
|
+
return "unknown";
|
|
738
|
+
case "MorphOne":
|
|
739
|
+
return targetName;
|
|
740
|
+
case "MorphMany":
|
|
741
|
+
case "MorphToMany":
|
|
742
|
+
case "MorphedByMany":
|
|
743
|
+
return `${targetName}[]`;
|
|
662
744
|
default:
|
|
663
745
|
return "unknown";
|
|
664
746
|
}
|
|
@@ -680,30 +762,65 @@ function getPropertyType(property, _allSchemas) {
|
|
|
680
762
|
}
|
|
681
763
|
return TYPE_MAP[property.type] ?? "unknown";
|
|
682
764
|
}
|
|
683
|
-
function
|
|
684
|
-
const type = getPropertyType(property, allSchemas);
|
|
765
|
+
function propertyToTSProperties(propertyName, property, allSchemas, options = {}) {
|
|
685
766
|
const baseProp = property;
|
|
686
|
-
|
|
767
|
+
const isReadonly = options.readonly ?? true;
|
|
768
|
+
if (property.type === "Association") {
|
|
769
|
+
const assocProp = property;
|
|
770
|
+
if (assocProp.relation === "MorphTo" && assocProp.targets && assocProp.targets.length > 0) {
|
|
771
|
+
const propBaseName = toPropertyName(propertyName);
|
|
772
|
+
const targetUnion = assocProp.targets.map((t) => `'${t}'`).join(" | ");
|
|
773
|
+
const relationUnion = assocProp.targets.join(" | ");
|
|
774
|
+
return [
|
|
775
|
+
{
|
|
776
|
+
name: `${propBaseName}Type`,
|
|
777
|
+
type: targetUnion,
|
|
778
|
+
optional: true,
|
|
779
|
+
// Polymorphic columns are nullable
|
|
780
|
+
readonly: isReadonly,
|
|
781
|
+
comment: `Polymorphic type for ${propertyName}`
|
|
782
|
+
},
|
|
783
|
+
{
|
|
784
|
+
name: `${propBaseName}Id`,
|
|
785
|
+
type: "number",
|
|
786
|
+
optional: true,
|
|
787
|
+
readonly: isReadonly,
|
|
788
|
+
comment: `Polymorphic ID for ${propertyName}`
|
|
789
|
+
},
|
|
790
|
+
{
|
|
791
|
+
name: propBaseName,
|
|
792
|
+
type: `${relationUnion} | null`,
|
|
793
|
+
optional: true,
|
|
794
|
+
readonly: isReadonly,
|
|
795
|
+
comment: baseProp.displayName ?? `Polymorphic relation to ${assocProp.targets.join(", ")}`
|
|
796
|
+
}
|
|
797
|
+
];
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
const type = getPropertyType(property, allSchemas);
|
|
801
|
+
return [{
|
|
687
802
|
name: toPropertyName(propertyName),
|
|
688
803
|
type,
|
|
689
804
|
optional: baseProp.nullable ?? false,
|
|
690
|
-
readonly:
|
|
805
|
+
readonly: isReadonly,
|
|
691
806
|
comment: baseProp.displayName
|
|
692
|
-
};
|
|
807
|
+
}];
|
|
693
808
|
}
|
|
694
809
|
function schemaToInterface(schema, allSchemas, options = {}) {
|
|
695
810
|
const properties = [];
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
811
|
+
if (schema.options?.id !== false) {
|
|
812
|
+
const pkType = schema.options?.idType ?? "BigInt";
|
|
813
|
+
properties.push({
|
|
814
|
+
name: "id",
|
|
815
|
+
type: PK_TYPE_MAP[pkType] ?? "number",
|
|
816
|
+
optional: false,
|
|
817
|
+
readonly: options.readonly ?? true,
|
|
818
|
+
comment: "Primary key"
|
|
819
|
+
});
|
|
820
|
+
}
|
|
704
821
|
if (schema.properties) {
|
|
705
822
|
for (const [propName, property] of Object.entries(schema.properties)) {
|
|
706
|
-
properties.push(
|
|
823
|
+
properties.push(...propertyToTSProperties(propName, property, allSchemas, options));
|
|
707
824
|
}
|
|
708
825
|
}
|
|
709
826
|
if (schema.options?.timestamps !== false) {
|