arkormx 0.2.1 → 0.2.3
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/cli.mjs +269 -12
- package/dist/index.cjs +276 -12
- package/dist/index.d.cts +149 -6
- package/dist/index.d.mts +149 -6
- package/dist/index.mjs +270 -13
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -117,6 +117,79 @@ var ArkormException = class extends Error {
|
|
|
117
117
|
}
|
|
118
118
|
};
|
|
119
119
|
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/database/ForeignKeyBuilder.ts
|
|
122
|
+
/**
|
|
123
|
+
* The ForeignKeyBuilder class provides a fluent interface for defining
|
|
124
|
+
* foreign key constraints in a migration. It allows you to specify
|
|
125
|
+
* the referenced table and column, as well as actions to take on
|
|
126
|
+
* delete and aliases for the relation.
|
|
127
|
+
*
|
|
128
|
+
* @author Legacy (3m1n3nc3)
|
|
129
|
+
* @since 0.2.2
|
|
130
|
+
*/
|
|
131
|
+
var ForeignKeyBuilder = class {
|
|
132
|
+
foreignKey;
|
|
133
|
+
constructor(foreignKey) {
|
|
134
|
+
this.foreignKey = foreignKey;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Defines the referenced table and column for this foreign key constraint.
|
|
138
|
+
*
|
|
139
|
+
* @param table
|
|
140
|
+
* @param column
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
references(table, column) {
|
|
144
|
+
this.foreignKey.referencesTable = table;
|
|
145
|
+
this.foreignKey.referencesColumn = column;
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Defines the action to take when a referenced record is deleted, such
|
|
150
|
+
* as "CASCADE", "SET NULL", or "RESTRICT".
|
|
151
|
+
*
|
|
152
|
+
* @param action
|
|
153
|
+
* @returns
|
|
154
|
+
*/
|
|
155
|
+
onDelete(action) {
|
|
156
|
+
this.foreignKey.onDelete = action;
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Defines an alias for the relation represented by this foreign key, which
|
|
161
|
+
* can be used in the ORM for more intuitive access to related models.
|
|
162
|
+
*
|
|
163
|
+
* @param name
|
|
164
|
+
* @returns
|
|
165
|
+
*/
|
|
166
|
+
alias(name) {
|
|
167
|
+
this.foreignKey.relationAlias = name;
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Defines an alias for the inverse relation represented by this foreign key.
|
|
172
|
+
*
|
|
173
|
+
* @param name
|
|
174
|
+
* @returns
|
|
175
|
+
*/
|
|
176
|
+
inverseAlias(name) {
|
|
177
|
+
this.foreignKey.inverseRelationAlias = name;
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Defines an alias for the foreign key field itself, which can be
|
|
182
|
+
* used in the ORM for more intuitive access to the foreign key value.
|
|
183
|
+
*
|
|
184
|
+
* @param fieldName
|
|
185
|
+
* @returns
|
|
186
|
+
*/
|
|
187
|
+
as(fieldName) {
|
|
188
|
+
this.foreignKey.fieldAlias = fieldName;
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
120
193
|
//#endregion
|
|
121
194
|
//#region src/database/TableBuilder.ts
|
|
122
195
|
/**
|
|
@@ -130,6 +203,7 @@ var TableBuilder = class {
|
|
|
130
203
|
columns = [];
|
|
131
204
|
dropColumnNames = [];
|
|
132
205
|
indexes = [];
|
|
206
|
+
foreignKeys = [];
|
|
133
207
|
latestColumnName;
|
|
134
208
|
/**
|
|
135
209
|
* Defines a primary key column in the table.
|
|
@@ -194,7 +268,7 @@ var TableBuilder = class {
|
|
|
194
268
|
*
|
|
195
269
|
* @param name The name of the integer column.
|
|
196
270
|
* @param options Additional options for the integer column.
|
|
197
|
-
* @returns
|
|
271
|
+
* @returns The current TableBuilder instance for chaining.
|
|
198
272
|
*/
|
|
199
273
|
integer(name, options = {}) {
|
|
200
274
|
return this.column(name, "integer", options);
|
|
@@ -204,7 +278,7 @@ var TableBuilder = class {
|
|
|
204
278
|
*
|
|
205
279
|
* @param name The name of the big integer column.
|
|
206
280
|
* @param options Additional options for the big integer column.
|
|
207
|
-
* @returns
|
|
281
|
+
* @returns The current TableBuilder instance for chaining.
|
|
208
282
|
*/
|
|
209
283
|
bigInteger(name, options = {}) {
|
|
210
284
|
return this.column(name, "bigInteger", options);
|
|
@@ -214,17 +288,29 @@ var TableBuilder = class {
|
|
|
214
288
|
*
|
|
215
289
|
* @param name The name of the float column.
|
|
216
290
|
* @param options Additional options for the float column.
|
|
217
|
-
* @returns
|
|
291
|
+
* @returns The current TableBuilder instance for chaining.
|
|
218
292
|
*/
|
|
219
293
|
float(name, options = {}) {
|
|
220
294
|
return this.column(name, "float", options);
|
|
221
295
|
}
|
|
222
296
|
/**
|
|
297
|
+
* Marks a column as unique in the table.
|
|
298
|
+
*
|
|
299
|
+
* @param name Optional explicit column name.
|
|
300
|
+
* When omitted, applies to the latest defined column.
|
|
301
|
+
* @returns The current TableBuilder instance for chaining.
|
|
302
|
+
*/
|
|
303
|
+
unique(name) {
|
|
304
|
+
const column = this.resolveColumn(name);
|
|
305
|
+
column.unique = true;
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
223
309
|
* Defines a boolean column in the table.
|
|
224
310
|
*
|
|
225
311
|
* @param name The name of the boolean column.
|
|
226
312
|
* @param options Additional options for the boolean column.
|
|
227
|
-
* @returns
|
|
313
|
+
* @returns The current TableBuilder instance for chaining.
|
|
228
314
|
*/
|
|
229
315
|
boolean(name, options = {}) {
|
|
230
316
|
return this.column(name, "boolean", options);
|
|
@@ -360,6 +446,32 @@ var TableBuilder = class {
|
|
|
360
446
|
return this;
|
|
361
447
|
}
|
|
362
448
|
/**
|
|
449
|
+
* Defines a foreign key relation for an existing column.
|
|
450
|
+
*
|
|
451
|
+
* @param column The local foreign key column name.
|
|
452
|
+
* @returns A fluent foreign key builder.
|
|
453
|
+
*/
|
|
454
|
+
foreignKey(column) {
|
|
455
|
+
const entry = {
|
|
456
|
+
column,
|
|
457
|
+
referencesTable: "",
|
|
458
|
+
referencesColumn: "id"
|
|
459
|
+
};
|
|
460
|
+
this.foreignKeys.push(entry);
|
|
461
|
+
return new ForeignKeyBuilder(entry);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Defines a foreign key relation for a column, using a
|
|
465
|
+
* conventional naming pattern.
|
|
466
|
+
*
|
|
467
|
+
* @param column
|
|
468
|
+
* @returns
|
|
469
|
+
*/
|
|
470
|
+
foreign(column) {
|
|
471
|
+
const columnName = this.resolveColumn(column).name;
|
|
472
|
+
return this.foreignKey(columnName + (column ? "" : "Id"));
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
363
475
|
* Returns a deep copy of the defined columns for the table.
|
|
364
476
|
*
|
|
365
477
|
* @returns
|
|
@@ -387,6 +499,14 @@ var TableBuilder = class {
|
|
|
387
499
|
}));
|
|
388
500
|
}
|
|
389
501
|
/**
|
|
502
|
+
* Returns a deep copy of the defined foreign keys for the table.
|
|
503
|
+
*
|
|
504
|
+
* @returns
|
|
505
|
+
*/
|
|
506
|
+
getForeignKeys() {
|
|
507
|
+
return this.foreignKeys.map((foreignKey) => ({ ...foreignKey }));
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
390
510
|
* Defines a column in the table with the given name.
|
|
391
511
|
*
|
|
392
512
|
* @param name The name of the column.
|
|
@@ -449,7 +569,8 @@ var SchemaBuilder = class {
|
|
|
449
569
|
type: "createTable",
|
|
450
570
|
table,
|
|
451
571
|
columns: builder.getColumns(),
|
|
452
|
-
indexes: builder.getIndexes()
|
|
572
|
+
indexes: builder.getIndexes(),
|
|
573
|
+
foreignKeys: builder.getForeignKeys()
|
|
453
574
|
});
|
|
454
575
|
return this;
|
|
455
576
|
}
|
|
@@ -468,7 +589,8 @@ var SchemaBuilder = class {
|
|
|
468
589
|
table,
|
|
469
590
|
addColumns: builder.getColumns(),
|
|
470
591
|
dropColumns: builder.getDropColumns(),
|
|
471
|
-
addIndexes: builder.getIndexes()
|
|
592
|
+
addIndexes: builder.getIndexes(),
|
|
593
|
+
addForeignKeys: builder.getForeignKeys()
|
|
472
594
|
});
|
|
473
595
|
return this;
|
|
474
596
|
}
|
|
@@ -498,7 +620,8 @@ var SchemaBuilder = class {
|
|
|
498
620
|
indexes: operation.indexes.map((index) => ({
|
|
499
621
|
...index,
|
|
500
622
|
columns: [...index.columns]
|
|
501
|
-
}))
|
|
623
|
+
})),
|
|
624
|
+
foreignKeys: operation.foreignKeys.map((foreignKey) => ({ ...foreignKey }))
|
|
502
625
|
};
|
|
503
626
|
if (operation.type === "alterTable") return {
|
|
504
627
|
...operation,
|
|
@@ -507,7 +630,8 @@ var SchemaBuilder = class {
|
|
|
507
630
|
addIndexes: operation.addIndexes.map((index) => ({
|
|
508
631
|
...index,
|
|
509
632
|
columns: [...index.columns]
|
|
510
|
-
}))
|
|
633
|
+
})),
|
|
634
|
+
addForeignKeys: operation.addForeignKeys.map((foreignKey) => ({ ...foreignKey }))
|
|
511
635
|
};
|
|
512
636
|
return { ...operation };
|
|
513
637
|
});
|
|
@@ -592,13 +716,139 @@ const buildFieldLine = (column) => {
|
|
|
592
716
|
/**
|
|
593
717
|
* Build a Prisma model-level @@index definition line.
|
|
594
718
|
*
|
|
595
|
-
* @param index
|
|
719
|
+
* @param index The schema index definition to convert to a Prisma \@\@index line.
|
|
596
720
|
* @returns
|
|
597
721
|
*/
|
|
598
722
|
const buildIndexLine = (index) => {
|
|
599
723
|
return ` @@index([${index.columns.join(", ")}]${typeof index.name === "string" && index.name.trim().length > 0 ? `, name: "${index.name.replace(/"/g, "\\\"")}"` : ""})`;
|
|
600
724
|
};
|
|
601
725
|
/**
|
|
726
|
+
* Derive a relation field name from a foreign key column name by applying
|
|
727
|
+
* common conventions, such as removing "Id" suffixes and converting to camelCase.
|
|
728
|
+
*
|
|
729
|
+
* @param columnName The name of the foreign key column.
|
|
730
|
+
* @returns The derived relation field name.
|
|
731
|
+
*/
|
|
732
|
+
const deriveRelationFieldName = (columnName) => {
|
|
733
|
+
const trimmed = columnName.trim();
|
|
734
|
+
if (!trimmed) return "relation";
|
|
735
|
+
if (trimmed.endsWith("Id") && trimmed.length > 2) {
|
|
736
|
+
const root = trimmed.slice(0, -2);
|
|
737
|
+
return `${root.charAt(0).toLowerCase()}${root.slice(1)}`;
|
|
738
|
+
}
|
|
739
|
+
if (trimmed.endsWith("_id") && trimmed.length > 3) return trimmed.slice(0, -3).replace(/_([a-zA-Z0-9])/g, (_, letter) => letter.toUpperCase());
|
|
740
|
+
return `${trimmed.charAt(0).toLowerCase()}${trimmed.slice(1)}`;
|
|
741
|
+
};
|
|
742
|
+
const pascalWords = (value) => {
|
|
743
|
+
return value.match(/[A-Z][a-z0-9]*/g) ?? [value];
|
|
744
|
+
};
|
|
745
|
+
/**
|
|
746
|
+
* Derive a relation name for the inverse side of a relation based on the
|
|
747
|
+
* source and target model names, using an explicit alias if provided or a
|
|
748
|
+
* convention of combining the target model name with the last segment of
|
|
749
|
+
* the source model name.
|
|
750
|
+
*
|
|
751
|
+
* @param sourceModelName The name of the source model in the relation.
|
|
752
|
+
* @param targetModelName The name of the target model in the relation.
|
|
753
|
+
* @param explicitAlias An optional explicit alias for the inverse relation.
|
|
754
|
+
* @returns The derived or explicit inverse relation alias.
|
|
755
|
+
*/
|
|
756
|
+
const deriveInverseRelationAlias = (sourceModelName, targetModelName, explicitAlias) => {
|
|
757
|
+
if (explicitAlias && explicitAlias.trim().length > 0) return explicitAlias.trim();
|
|
758
|
+
const sourceWords = pascalWords(sourceModelName);
|
|
759
|
+
return `${sourceWords[sourceWords.length - 1] ?? sourceModelName}${targetModelName}`;
|
|
760
|
+
};
|
|
761
|
+
const deriveCollectionFieldName = (modelName) => {
|
|
762
|
+
if (!modelName) return "items";
|
|
763
|
+
const camel = `${modelName.charAt(0).toLowerCase()}${modelName.slice(1)}`;
|
|
764
|
+
if (camel.endsWith("s")) return `${camel}es`;
|
|
765
|
+
return `${camel}s`;
|
|
766
|
+
};
|
|
767
|
+
/**
|
|
768
|
+
* Format a SchemaForeignKeyAction value as a Prisma onDelete action string.
|
|
769
|
+
*
|
|
770
|
+
* @param action The foreign key action to format.
|
|
771
|
+
* @returns The corresponding Prisma onDelete action string.
|
|
772
|
+
*/
|
|
773
|
+
const formatRelationAction = (action) => {
|
|
774
|
+
if (action === "cascade") return "Cascade";
|
|
775
|
+
if (action === "restrict") return "Restrict";
|
|
776
|
+
if (action === "setNull") return "SetNull";
|
|
777
|
+
if (action === "setDefault") return "SetDefault";
|
|
778
|
+
return "NoAction";
|
|
779
|
+
};
|
|
780
|
+
/**
|
|
781
|
+
* Build a Prisma relation field line based on a SchemaForeignKey
|
|
782
|
+
* definition, including relation name and onDelete action.
|
|
783
|
+
*
|
|
784
|
+
* @param foreignKey The foreign key definition to convert to a relation line.
|
|
785
|
+
* @returns The corresponding Prisma schema line for the relation field.
|
|
786
|
+
*/
|
|
787
|
+
const buildRelationLine = (foreignKey) => {
|
|
788
|
+
if (!foreignKey.referencesTable.trim()) throw new ArkormException(`Foreign key [${foreignKey.column}] must define a referenced table.`);
|
|
789
|
+
if (!foreignKey.referencesColumn.trim()) throw new ArkormException(`Foreign key [${foreignKey.column}] must define a referenced column.`);
|
|
790
|
+
const fieldName = foreignKey.fieldAlias?.trim() || deriveRelationFieldName(foreignKey.column);
|
|
791
|
+
const targetModel = toModelName(foreignKey.referencesTable);
|
|
792
|
+
const relationName = foreignKey.relationAlias?.trim();
|
|
793
|
+
const relationPrefix = relationName ? `@relation("${relationName.replace(/"/g, "\\\"")}", ` : "@relation(";
|
|
794
|
+
const onDelete = foreignKey.onDelete ? `, onDelete: ${formatRelationAction(foreignKey.onDelete)}` : "";
|
|
795
|
+
return ` ${fieldName} ${targetModel} ${relationPrefix}fields: [${foreignKey.column}], references: [${foreignKey.referencesColumn}]${onDelete})`;
|
|
796
|
+
};
|
|
797
|
+
/**
|
|
798
|
+
* Build a Prisma relation field line for the inverse side of a relation, based
|
|
799
|
+
* on the source and target model names and the foreign key definition, using
|
|
800
|
+
* naming conventions and any explicit inverse alias provided.
|
|
801
|
+
*
|
|
802
|
+
* @param sourceModelName The name of the source model in the relation.
|
|
803
|
+
* @param targetModelName The name of the target model in the relation.
|
|
804
|
+
* @param foreignKey The foreign key definition for the relation.
|
|
805
|
+
* @returns The Prisma schema line for the inverse relation field.
|
|
806
|
+
*/
|
|
807
|
+
const buildInverseRelationLine = (sourceModelName, targetModelName, foreignKey) => {
|
|
808
|
+
return ` ${deriveCollectionFieldName(sourceModelName)} ${sourceModelName}[] @relation("${deriveInverseRelationAlias(sourceModelName, targetModelName, foreignKey.inverseRelationAlias).replace(/"/g, "\\\"")}")`;
|
|
809
|
+
};
|
|
810
|
+
/**
|
|
811
|
+
* Inject a line into the body of a Prisma model block if it does not already
|
|
812
|
+
* exist, using a provided existence check function to determine if the line
|
|
813
|
+
* is already present.
|
|
814
|
+
*
|
|
815
|
+
* @param bodyLines The lines of the model block body to modify.
|
|
816
|
+
* @param line The line to inject if it does not already exist.
|
|
817
|
+
* @param exists A function that checks if a given line already exists in the body.
|
|
818
|
+
* @returns
|
|
819
|
+
*/
|
|
820
|
+
const injectLineIntoModelBody = (bodyLines, line, exists) => {
|
|
821
|
+
if (bodyLines.some(exists)) return bodyLines;
|
|
822
|
+
const insertIndex = Math.max(1, bodyLines.length - 1);
|
|
823
|
+
bodyLines.splice(insertIndex, 0, line);
|
|
824
|
+
return bodyLines;
|
|
825
|
+
};
|
|
826
|
+
/**
|
|
827
|
+
* Apply inverse relation definitions to a Prisma schema string based on the
|
|
828
|
+
* foreign keys defined in a create or alter table operation, ensuring that
|
|
829
|
+
* related models have corresponding relation fields for bi-directional navigation.
|
|
830
|
+
*
|
|
831
|
+
* @param schema The Prisma schema string to modify.
|
|
832
|
+
* @param sourceModelName The name of the source model in the relation.
|
|
833
|
+
* @param foreignKeys An array of foreign key definitions to process.
|
|
834
|
+
* @returns The updated Prisma schema string with inverse relations applied.
|
|
835
|
+
*/
|
|
836
|
+
const applyInverseRelations = (schema, sourceModelName, foreignKeys) => {
|
|
837
|
+
let nextSchema = schema;
|
|
838
|
+
for (const foreignKey of foreignKeys) {
|
|
839
|
+
const targetModel = findModelBlock(nextSchema, foreignKey.referencesTable);
|
|
840
|
+
if (!targetModel) continue;
|
|
841
|
+
const inverseLine = buildInverseRelationLine(sourceModelName, targetModel.modelName, foreignKey);
|
|
842
|
+
const targetBodyLines = targetModel.block.split("\n");
|
|
843
|
+
const fieldName = deriveCollectionFieldName(sourceModelName);
|
|
844
|
+
const fieldRegex = new RegExp(`^\\s*${escapeRegex(fieldName)}\\s+`);
|
|
845
|
+
injectLineIntoModelBody(targetBodyLines, inverseLine, (line) => fieldRegex.test(line));
|
|
846
|
+
const updatedTarget = targetBodyLines.join("\n");
|
|
847
|
+
nextSchema = `${nextSchema.slice(0, targetModel.start)}${updatedTarget}${nextSchema.slice(targetModel.end)}`;
|
|
848
|
+
}
|
|
849
|
+
return nextSchema;
|
|
850
|
+
};
|
|
851
|
+
/**
|
|
602
852
|
* Build a Prisma model block string based on a SchemaTableCreateOperation, including
|
|
603
853
|
* all fields and any necessary mapping.
|
|
604
854
|
*
|
|
@@ -609,12 +859,14 @@ const buildModelBlock = (operation) => {
|
|
|
609
859
|
const modelName = toModelName(operation.table);
|
|
610
860
|
const mapped = operation.table !== modelName.toLowerCase();
|
|
611
861
|
const fields = operation.columns.map(buildFieldLine);
|
|
862
|
+
const relations = (operation.foreignKeys ?? []).map(buildRelationLine);
|
|
612
863
|
const metadata = [...(operation.indexes ?? []).map(buildIndexLine), ...mapped ? [` @@map("${(0, _h3ravel_support.str)(operation.table).snake()}")`] : []];
|
|
613
864
|
return `model ${modelName} {\n${(metadata.length > 0 ? [
|
|
614
865
|
...fields,
|
|
866
|
+
...relations,
|
|
615
867
|
"",
|
|
616
868
|
...metadata
|
|
617
|
-
] : fields).join("\n")}\n}`;
|
|
869
|
+
] : [...fields, ...relations]).join("\n")}\n}`;
|
|
618
870
|
};
|
|
619
871
|
/**
|
|
620
872
|
* Find the Prisma model block in a schema string that corresponds to a given
|
|
@@ -664,7 +916,7 @@ const findModelBlock = (schema, table) => {
|
|
|
664
916
|
const applyCreateTableOperation = (schema, operation) => {
|
|
665
917
|
if (findModelBlock(schema, operation.table)) throw new ArkormException(`Prisma model for table [${operation.table}] already exists.`);
|
|
666
918
|
const block = buildModelBlock(operation);
|
|
667
|
-
return `${schema.trimEnd()}\n\n${block}\n
|
|
919
|
+
return applyInverseRelations(`${schema.trimEnd()}\n\n${block}\n`, toModelName(operation.table), operation.foreignKeys ?? []);
|
|
668
920
|
};
|
|
669
921
|
/**
|
|
670
922
|
* Apply an alter table operation to a Prisma schema string, modifying the model
|
|
@@ -701,8 +953,13 @@ const applyAlterTableOperation = (schema, operation) => {
|
|
|
701
953
|
const insertIndex = Math.max(1, bodyLines.length - 1);
|
|
702
954
|
bodyLines.splice(insertIndex, 0, indexLine);
|
|
703
955
|
});
|
|
956
|
+
for (const foreignKey of operation.addForeignKeys ?? []) {
|
|
957
|
+
const relationLine = buildRelationLine(foreignKey);
|
|
958
|
+
const relationRegex = new RegExp(`^\\s*${escapeRegex(foreignKey.fieldAlias?.trim() || deriveRelationFieldName(foreignKey.column))}\\s+`);
|
|
959
|
+
injectLineIntoModelBody(bodyLines, relationLine, (line) => relationRegex.test(line));
|
|
960
|
+
}
|
|
704
961
|
block = bodyLines.join("\n");
|
|
705
|
-
return `${schema.slice(0, model.start)}${block}${schema.slice(model.end)}
|
|
962
|
+
return applyInverseRelations(`${schema.slice(0, model.start)}${block}${schema.slice(model.end)}`, model.modelName, operation.addForeignKeys ?? []);
|
|
706
963
|
};
|
|
707
964
|
/**
|
|
708
965
|
* Apply a drop table operation to a Prisma schema string, removing the model block
|
|
@@ -4862,6 +5119,7 @@ var Model = class Model {
|
|
|
4862
5119
|
exports.ArkormCollection = ArkormCollection;
|
|
4863
5120
|
exports.ArkormException = ArkormException;
|
|
4864
5121
|
exports.CliApp = CliApp;
|
|
5122
|
+
exports.ForeignKeyBuilder = ForeignKeyBuilder;
|
|
4865
5123
|
exports.InitCommand = InitCommand;
|
|
4866
5124
|
exports.InlineFactory = InlineFactory;
|
|
4867
5125
|
exports.LengthAwarePaginator = LengthAwarePaginator;
|
|
@@ -4892,18 +5150,24 @@ exports.applyMigrationToPrismaSchema = applyMigrationToPrismaSchema;
|
|
|
4892
5150
|
exports.applyOperationsToPrismaSchema = applyOperationsToPrismaSchema;
|
|
4893
5151
|
exports.buildFieldLine = buildFieldLine;
|
|
4894
5152
|
exports.buildIndexLine = buildIndexLine;
|
|
5153
|
+
exports.buildInverseRelationLine = buildInverseRelationLine;
|
|
4895
5154
|
exports.buildMigrationSource = buildMigrationSource;
|
|
4896
5155
|
exports.buildModelBlock = buildModelBlock;
|
|
5156
|
+
exports.buildRelationLine = buildRelationLine;
|
|
4897
5157
|
exports.configureArkormRuntime = configureArkormRuntime;
|
|
4898
5158
|
exports.createMigrationTimestamp = createMigrationTimestamp;
|
|
4899
5159
|
exports.createPrismaAdapter = createPrismaAdapter;
|
|
4900
5160
|
exports.createPrismaDelegateMap = createPrismaDelegateMap;
|
|
4901
5161
|
exports.defineConfig = defineConfig;
|
|
4902
5162
|
exports.defineFactory = defineFactory;
|
|
5163
|
+
exports.deriveCollectionFieldName = deriveCollectionFieldName;
|
|
5164
|
+
exports.deriveInverseRelationAlias = deriveInverseRelationAlias;
|
|
5165
|
+
exports.deriveRelationFieldName = deriveRelationFieldName;
|
|
4903
5166
|
exports.ensureArkormConfigLoading = ensureArkormConfigLoading;
|
|
4904
5167
|
exports.escapeRegex = escapeRegex;
|
|
4905
5168
|
exports.findModelBlock = findModelBlock;
|
|
4906
5169
|
exports.formatDefaultValue = formatDefaultValue;
|
|
5170
|
+
exports.formatRelationAction = formatRelationAction;
|
|
4907
5171
|
exports.generateMigrationFile = generateMigrationFile;
|
|
4908
5172
|
exports.getDefaultStubsPath = getDefaultStubsPath;
|
|
4909
5173
|
exports.getMigrationPlan = getMigrationPlan;
|
package/dist/index.d.cts
CHANGED
|
@@ -27,11 +27,22 @@ interface SchemaIndex {
|
|
|
27
27
|
columns: string[];
|
|
28
28
|
name?: string;
|
|
29
29
|
}
|
|
30
|
+
type SchemaForeignKeyAction = 'cascade' | 'restrict' | 'setNull' | 'noAction' | 'setDefault';
|
|
31
|
+
interface SchemaForeignKey {
|
|
32
|
+
column: string;
|
|
33
|
+
referencesTable: string;
|
|
34
|
+
referencesColumn: string;
|
|
35
|
+
onDelete?: SchemaForeignKeyAction;
|
|
36
|
+
relationAlias?: string;
|
|
37
|
+
inverseRelationAlias?: string;
|
|
38
|
+
fieldAlias?: string;
|
|
39
|
+
}
|
|
30
40
|
interface SchemaTableCreateOperation {
|
|
31
41
|
type: 'createTable';
|
|
32
42
|
table: string;
|
|
33
43
|
columns: SchemaColumn[];
|
|
34
44
|
indexes: SchemaIndex[];
|
|
45
|
+
foreignKeys: SchemaForeignKey[];
|
|
35
46
|
}
|
|
36
47
|
interface SchemaTableAlterOperation {
|
|
37
48
|
type: 'alterTable';
|
|
@@ -39,6 +50,7 @@ interface SchemaTableAlterOperation {
|
|
|
39
50
|
addColumns: SchemaColumn[];
|
|
40
51
|
dropColumns: string[];
|
|
41
52
|
addIndexes: SchemaIndex[];
|
|
53
|
+
addForeignKeys: SchemaForeignKey[];
|
|
42
54
|
}
|
|
43
55
|
interface SchemaTableDropOperation {
|
|
44
56
|
type: 'dropTable';
|
|
@@ -2286,6 +2298,60 @@ declare class SeedCommand extends Command<CliApp> {
|
|
|
2286
2298
|
private loadSeederClassesFromFile;
|
|
2287
2299
|
}
|
|
2288
2300
|
//#endregion
|
|
2301
|
+
//#region src/database/ForeignKeyBuilder.d.ts
|
|
2302
|
+
/**
|
|
2303
|
+
* The ForeignKeyBuilder class provides a fluent interface for defining
|
|
2304
|
+
* foreign key constraints in a migration. It allows you to specify
|
|
2305
|
+
* the referenced table and column, as well as actions to take on
|
|
2306
|
+
* delete and aliases for the relation.
|
|
2307
|
+
*
|
|
2308
|
+
* @author Legacy (3m1n3nc3)
|
|
2309
|
+
* @since 0.2.2
|
|
2310
|
+
*/
|
|
2311
|
+
declare class ForeignKeyBuilder {
|
|
2312
|
+
private readonly foreignKey;
|
|
2313
|
+
constructor(foreignKey: SchemaForeignKey);
|
|
2314
|
+
/**
|
|
2315
|
+
* Defines the referenced table and column for this foreign key constraint.
|
|
2316
|
+
*
|
|
2317
|
+
* @param table
|
|
2318
|
+
* @param column
|
|
2319
|
+
* @returns
|
|
2320
|
+
*/
|
|
2321
|
+
references(table: string, column: string): this;
|
|
2322
|
+
/**
|
|
2323
|
+
* Defines the action to take when a referenced record is deleted, such
|
|
2324
|
+
* as "CASCADE", "SET NULL", or "RESTRICT".
|
|
2325
|
+
*
|
|
2326
|
+
* @param action
|
|
2327
|
+
* @returns
|
|
2328
|
+
*/
|
|
2329
|
+
onDelete(action: SchemaForeignKeyAction): this;
|
|
2330
|
+
/**
|
|
2331
|
+
* Defines an alias for the relation represented by this foreign key, which
|
|
2332
|
+
* can be used in the ORM for more intuitive access to related models.
|
|
2333
|
+
*
|
|
2334
|
+
* @param name
|
|
2335
|
+
* @returns
|
|
2336
|
+
*/
|
|
2337
|
+
alias(name: string): this;
|
|
2338
|
+
/**
|
|
2339
|
+
* Defines an alias for the inverse relation represented by this foreign key.
|
|
2340
|
+
*
|
|
2341
|
+
* @param name
|
|
2342
|
+
* @returns
|
|
2343
|
+
*/
|
|
2344
|
+
inverseAlias(name: string): this;
|
|
2345
|
+
/**
|
|
2346
|
+
* Defines an alias for the foreign key field itself, which can be
|
|
2347
|
+
* used in the ORM for more intuitive access to the foreign key value.
|
|
2348
|
+
*
|
|
2349
|
+
* @param fieldName
|
|
2350
|
+
* @returns
|
|
2351
|
+
*/
|
|
2352
|
+
as(fieldName: string): this;
|
|
2353
|
+
}
|
|
2354
|
+
//#endregion
|
|
2289
2355
|
//#region src/database/TableBuilder.d.ts
|
|
2290
2356
|
/**
|
|
2291
2357
|
* The TableBuilder class provides a fluent interface for defining
|
|
@@ -2298,6 +2364,7 @@ declare class TableBuilder {
|
|
|
2298
2364
|
private readonly columns;
|
|
2299
2365
|
private readonly dropColumnNames;
|
|
2300
2366
|
private readonly indexes;
|
|
2367
|
+
private readonly foreignKeys;
|
|
2301
2368
|
private latestColumnName;
|
|
2302
2369
|
/**
|
|
2303
2370
|
* Defines a primary key column in the table.
|
|
@@ -2351,7 +2418,7 @@ declare class TableBuilder {
|
|
|
2351
2418
|
*
|
|
2352
2419
|
* @param name The name of the integer column.
|
|
2353
2420
|
* @param options Additional options for the integer column.
|
|
2354
|
-
* @returns
|
|
2421
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2355
2422
|
*/
|
|
2356
2423
|
integer(name: string, options?: Partial<SchemaColumn>): this;
|
|
2357
2424
|
/**
|
|
@@ -2359,7 +2426,7 @@ declare class TableBuilder {
|
|
|
2359
2426
|
*
|
|
2360
2427
|
* @param name The name of the big integer column.
|
|
2361
2428
|
* @param options Additional options for the big integer column.
|
|
2362
|
-
* @returns
|
|
2429
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2363
2430
|
*/
|
|
2364
2431
|
bigInteger(name: string, options?: Partial<SchemaColumn>): this;
|
|
2365
2432
|
/**
|
|
@@ -2367,15 +2434,23 @@ declare class TableBuilder {
|
|
|
2367
2434
|
*
|
|
2368
2435
|
* @param name The name of the float column.
|
|
2369
2436
|
* @param options Additional options for the float column.
|
|
2370
|
-
* @returns
|
|
2437
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2371
2438
|
*/
|
|
2372
2439
|
float(name: string, options?: Partial<SchemaColumn>): this;
|
|
2440
|
+
/**
|
|
2441
|
+
* Marks a column as unique in the table.
|
|
2442
|
+
*
|
|
2443
|
+
* @param name Optional explicit column name.
|
|
2444
|
+
* When omitted, applies to the latest defined column.
|
|
2445
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2446
|
+
*/
|
|
2447
|
+
unique(name?: string): this;
|
|
2373
2448
|
/**
|
|
2374
2449
|
* Defines a boolean column in the table.
|
|
2375
2450
|
*
|
|
2376
2451
|
* @param name The name of the boolean column.
|
|
2377
2452
|
* @param options Additional options for the boolean column.
|
|
2378
|
-
* @returns
|
|
2453
|
+
* @returns The current TableBuilder instance for chaining.
|
|
2379
2454
|
*/
|
|
2380
2455
|
boolean(name: string, options?: Partial<SchemaColumn>): this;
|
|
2381
2456
|
/**
|
|
@@ -2467,6 +2542,21 @@ declare class TableBuilder {
|
|
|
2467
2542
|
* @returns The current TableBuilder instance for chaining.
|
|
2468
2543
|
*/
|
|
2469
2544
|
index(columns?: string | string[], name?: string): this;
|
|
2545
|
+
/**
|
|
2546
|
+
* Defines a foreign key relation for an existing column.
|
|
2547
|
+
*
|
|
2548
|
+
* @param column The local foreign key column name.
|
|
2549
|
+
* @returns A fluent foreign key builder.
|
|
2550
|
+
*/
|
|
2551
|
+
foreignKey(column: string): ForeignKeyBuilder;
|
|
2552
|
+
/**
|
|
2553
|
+
* Defines a foreign key relation for a column, using a
|
|
2554
|
+
* conventional naming pattern.
|
|
2555
|
+
*
|
|
2556
|
+
* @param column
|
|
2557
|
+
* @returns
|
|
2558
|
+
*/
|
|
2559
|
+
foreign(column?: string): ForeignKeyBuilder;
|
|
2470
2560
|
/**
|
|
2471
2561
|
* Returns a deep copy of the defined columns for the table.
|
|
2472
2562
|
*
|
|
@@ -2485,6 +2575,12 @@ declare class TableBuilder {
|
|
|
2485
2575
|
* @returns
|
|
2486
2576
|
*/
|
|
2487
2577
|
getIndexes(): SchemaIndex[];
|
|
2578
|
+
/**
|
|
2579
|
+
* Returns a deep copy of the defined foreign keys for the table.
|
|
2580
|
+
*
|
|
2581
|
+
* @returns
|
|
2582
|
+
*/
|
|
2583
|
+
getForeignKeys(): SchemaForeignKey[];
|
|
2488
2584
|
/**
|
|
2489
2585
|
* Defines a column in the table with the given name.
|
|
2490
2586
|
*
|
|
@@ -2675,10 +2771,57 @@ declare const buildFieldLine: (column: SchemaColumn) => string;
|
|
|
2675
2771
|
/**
|
|
2676
2772
|
* Build a Prisma model-level @@index definition line.
|
|
2677
2773
|
*
|
|
2678
|
-
* @param index
|
|
2774
|
+
* @param index The schema index definition to convert to a Prisma \@\@index line.
|
|
2679
2775
|
* @returns
|
|
2680
2776
|
*/
|
|
2681
2777
|
declare const buildIndexLine: (index: SchemaIndex) => string;
|
|
2778
|
+
/**
|
|
2779
|
+
* Derive a relation field name from a foreign key column name by applying
|
|
2780
|
+
* common conventions, such as removing "Id" suffixes and converting to camelCase.
|
|
2781
|
+
*
|
|
2782
|
+
* @param columnName The name of the foreign key column.
|
|
2783
|
+
* @returns The derived relation field name.
|
|
2784
|
+
*/
|
|
2785
|
+
declare const deriveRelationFieldName: (columnName: string) => string;
|
|
2786
|
+
/**
|
|
2787
|
+
* Derive a relation name for the inverse side of a relation based on the
|
|
2788
|
+
* source and target model names, using an explicit alias if provided or a
|
|
2789
|
+
* convention of combining the target model name with the last segment of
|
|
2790
|
+
* the source model name.
|
|
2791
|
+
*
|
|
2792
|
+
* @param sourceModelName The name of the source model in the relation.
|
|
2793
|
+
* @param targetModelName The name of the target model in the relation.
|
|
2794
|
+
* @param explicitAlias An optional explicit alias for the inverse relation.
|
|
2795
|
+
* @returns The derived or explicit inverse relation alias.
|
|
2796
|
+
*/
|
|
2797
|
+
declare const deriveInverseRelationAlias: (sourceModelName: string, targetModelName: string, explicitAlias?: string) => string;
|
|
2798
|
+
declare const deriveCollectionFieldName: (modelName: string) => string;
|
|
2799
|
+
/**
|
|
2800
|
+
* Format a SchemaForeignKeyAction value as a Prisma onDelete action string.
|
|
2801
|
+
*
|
|
2802
|
+
* @param action The foreign key action to format.
|
|
2803
|
+
* @returns The corresponding Prisma onDelete action string.
|
|
2804
|
+
*/
|
|
2805
|
+
declare const formatRelationAction: (action: SchemaForeignKeyAction) => string;
|
|
2806
|
+
/**
|
|
2807
|
+
* Build a Prisma relation field line based on a SchemaForeignKey
|
|
2808
|
+
* definition, including relation name and onDelete action.
|
|
2809
|
+
*
|
|
2810
|
+
* @param foreignKey The foreign key definition to convert to a relation line.
|
|
2811
|
+
* @returns The corresponding Prisma schema line for the relation field.
|
|
2812
|
+
*/
|
|
2813
|
+
declare const buildRelationLine: (foreignKey: SchemaForeignKey) => string;
|
|
2814
|
+
/**
|
|
2815
|
+
* Build a Prisma relation field line for the inverse side of a relation, based
|
|
2816
|
+
* on the source and target model names and the foreign key definition, using
|
|
2817
|
+
* naming conventions and any explicit inverse alias provided.
|
|
2818
|
+
*
|
|
2819
|
+
* @param sourceModelName The name of the source model in the relation.
|
|
2820
|
+
* @param targetModelName The name of the target model in the relation.
|
|
2821
|
+
* @param foreignKey The foreign key definition for the relation.
|
|
2822
|
+
* @returns The Prisma schema line for the inverse relation field.
|
|
2823
|
+
*/
|
|
2824
|
+
declare const buildInverseRelationLine: (sourceModelName: string, targetModelName: string, foreignKey: SchemaForeignKey) => string;
|
|
2682
2825
|
/**
|
|
2683
2826
|
* Build a Prisma model block string based on a SchemaTableCreateOperation, including
|
|
2684
2827
|
* all fields and any necessary mapping.
|
|
@@ -2937,4 +3080,4 @@ declare class URLDriver {
|
|
|
2937
3080
|
url(page: number): string;
|
|
2938
3081
|
}
|
|
2939
3082
|
//#endregion
|
|
2940
|
-
export { ArkormCollection, ArkormException, CliApp, InitCommand, InlineFactory, LengthAwarePaginator, MIGRATION_BRAND, MakeFactoryCommand, MakeMigrationCommand, MakeModelCommand, MakeSeederCommand, MigrateCommand, Migration, Model, ModelFactory, ModelNotFoundException, ModelsSyncCommand, PRISMA_MODEL_REGEX, Paginator, PrismaDelegateMap, QueryBuilder, SEEDER_BRAND, SchemaBuilder, SeedCommand, Seeder, SeederCallArgument, SeederConstructor, SeederInput, TableBuilder, URLDriver, applyAlterTableOperation, applyCreateTableOperation, applyDropTableOperation, applyMigrationToPrismaSchema, applyOperationsToPrismaSchema, buildFieldLine, buildIndexLine, buildMigrationSource, buildModelBlock, configureArkormRuntime, createMigrationTimestamp, createPrismaAdapter, createPrismaDelegateMap, defineConfig, defineFactory, ensureArkormConfigLoading, escapeRegex, findModelBlock, formatDefaultValue, generateMigrationFile, getDefaultStubsPath, getMigrationPlan, getRuntimePaginationURLDriverFactory, getRuntimePrismaClient, getUserConfig, inferDelegateName, isDelegateLike, loadArkormConfig, pad, resetArkormRuntimeForTests, resolveCast, resolveMigrationClassName, resolvePrismaType, runMigrationWithPrisma, runPrismaCommand, toMigrationFileSlug, toModelName };
|
|
3083
|
+
export { ArkormCollection, ArkormException, CliApp, ForeignKeyBuilder, InitCommand, InlineFactory, LengthAwarePaginator, MIGRATION_BRAND, MakeFactoryCommand, MakeMigrationCommand, MakeModelCommand, MakeSeederCommand, MigrateCommand, Migration, Model, ModelFactory, ModelNotFoundException, ModelsSyncCommand, PRISMA_MODEL_REGEX, Paginator, PrismaDelegateMap, QueryBuilder, SEEDER_BRAND, SchemaBuilder, SeedCommand, Seeder, SeederCallArgument, SeederConstructor, SeederInput, TableBuilder, URLDriver, applyAlterTableOperation, applyCreateTableOperation, applyDropTableOperation, applyMigrationToPrismaSchema, applyOperationsToPrismaSchema, buildFieldLine, buildIndexLine, buildInverseRelationLine, buildMigrationSource, buildModelBlock, buildRelationLine, configureArkormRuntime, createMigrationTimestamp, createPrismaAdapter, createPrismaDelegateMap, defineConfig, defineFactory, deriveCollectionFieldName, deriveInverseRelationAlias, deriveRelationFieldName, ensureArkormConfigLoading, escapeRegex, findModelBlock, formatDefaultValue, formatRelationAction, generateMigrationFile, getDefaultStubsPath, getMigrationPlan, getRuntimePaginationURLDriverFactory, getRuntimePrismaClient, getUserConfig, inferDelegateName, isDelegateLike, loadArkormConfig, pad, resetArkormRuntimeForTests, resolveCast, resolveMigrationClassName, resolvePrismaType, runMigrationWithPrisma, runPrismaCommand, toMigrationFileSlug, toModelName };
|