forge-sql-orm 2.0.4 → 2.0.6
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 +4 -197
- package/dist/ForgeSQLORM.js +62 -0
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +63 -1
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/utils/sqlUtils.d.ts +6 -0
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts +10 -0
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts.map +1 -0
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts +17 -0
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts.map +1 -0
- package/dist/webtriggers/index.d.ts +10 -0
- package/dist/webtriggers/index.d.ts.map +1 -0
- package/dist-cli/cli.js +87 -40
- package/dist-cli/cli.js.map +1 -1
- package/dist-cli/cli.mjs +87 -40
- package/dist-cli/cli.mjs.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/utils/sqlUtils.ts +21 -0
- package/src/webtriggers/applyMigrationsWebTrigger.ts +25 -0
- package/src/webtriggers/dropMigrationWebTrigger.ts +47 -0
- package/src/webtriggers/index.ts +26 -0
package/dist-cli/cli.js
CHANGED
|
@@ -8,8 +8,11 @@ const path = require("path");
|
|
|
8
8
|
require("reflect-metadata");
|
|
9
9
|
const child_process = require("child_process");
|
|
10
10
|
const mysql = require("mysql2/promise");
|
|
11
|
-
require("moment");
|
|
11
|
+
const moment = require("moment");
|
|
12
12
|
const uniqueConstraint = require("drizzle-orm/mysql-core/unique-constraint");
|
|
13
|
+
require("@forge/sql");
|
|
14
|
+
const mysqlCore = require("drizzle-orm/mysql-core");
|
|
15
|
+
const moment$1 = require("moment/moment.js");
|
|
13
16
|
function replaceMySQLTypes(schemaContent) {
|
|
14
17
|
const imports = `import { forgeDateTimeString, forgeTimeString, forgeDateString, forgeTimestampString } from "forge-sql-orm";
|
|
15
18
|
|
|
@@ -224,6 +227,13 @@ const createMigration = async (options) => {
|
|
|
224
227
|
process.exit(1);
|
|
225
228
|
}
|
|
226
229
|
};
|
|
230
|
+
const parseDateTime = (value, format) => {
|
|
231
|
+
const m = moment(value, format, true);
|
|
232
|
+
if (!m.isValid()) {
|
|
233
|
+
return moment(value).toDate();
|
|
234
|
+
}
|
|
235
|
+
return m.toDate();
|
|
236
|
+
};
|
|
227
237
|
function processForeignKeys(table, foreignKeysSymbol, extraSymbol) {
|
|
228
238
|
const foreignKeys = [];
|
|
229
239
|
if (foreignKeysSymbol) {
|
|
@@ -306,6 +316,17 @@ function getTableMetadata(table) {
|
|
|
306
316
|
...builders
|
|
307
317
|
};
|
|
308
318
|
}
|
|
319
|
+
function generateDropTableStatements(tables) {
|
|
320
|
+
const dropStatements = [];
|
|
321
|
+
tables.forEach((table) => {
|
|
322
|
+
const tableMetadata = getTableMetadata(table);
|
|
323
|
+
if (tableMetadata.tableName) {
|
|
324
|
+
dropStatements.push(`DROP TABLE IF EXISTS \`${tableMetadata.tableName}\`;`);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
dropStatements.push(`DELETE FROM __migrations;`);
|
|
328
|
+
return dropStatements;
|
|
329
|
+
}
|
|
309
330
|
function generateMigrationFile$1(createStatements, version) {
|
|
310
331
|
const versionPrefix = `v${version}_MIGRATION`;
|
|
311
332
|
const migrationLines = createStatements.map(
|
|
@@ -483,7 +504,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
483
504
|
}
|
|
484
505
|
const columns2 = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
485
506
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
486
|
-
changes.push(`CREATE if not exists
|
|
507
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns2});`);
|
|
487
508
|
}
|
|
488
509
|
for (const [fkName, dbFK] of Object.entries(dbTable.foreignKeys)) {
|
|
489
510
|
changes.push(`ALTER TABLE \`${tableName}\` ADD CONSTRAINT \`${fkName}\` FOREIGN KEY (\`${dbFK.column}\`) REFERENCES \`${dbFK.referencedTable}\` (\`${dbFK.referencedColumn}\`);`);
|
|
@@ -528,7 +549,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
528
549
|
if (!drizzleIndex) {
|
|
529
550
|
const columns = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
530
551
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
531
|
-
changes.push(`CREATE if not exists
|
|
552
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns});`);
|
|
532
553
|
continue;
|
|
533
554
|
}
|
|
534
555
|
const dbColumns = dbIndex.columns.join(", ");
|
|
@@ -537,7 +558,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
537
558
|
changes.push(`DROP INDEX \`${indexName}\` ON \`${tableName}\`;`);
|
|
538
559
|
const columns = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
539
560
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
540
|
-
changes.push(`CREATE
|
|
561
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns});`);
|
|
541
562
|
}
|
|
542
563
|
}
|
|
543
564
|
for (const [fkName, dbFK] of Object.entries(dbTable.foreignKeys)) {
|
|
@@ -554,7 +575,13 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
554
575
|
});
|
|
555
576
|
if (!isDbFk) {
|
|
556
577
|
const fkName = getForeignKeyName(drizzleForeignKey);
|
|
557
|
-
|
|
578
|
+
if (fkName) {
|
|
579
|
+
changes.push(`ALTER TABLE \`${tableName}\` DROP FOREIGN KEY \`${fkName}\`;`);
|
|
580
|
+
} else {
|
|
581
|
+
const columns = drizzleForeignKey?.columns;
|
|
582
|
+
const columnNames = columns?.length ? columns.map((c) => c.name).join(", ") : "unknown columns";
|
|
583
|
+
console.warn(`⚠️ Drizzle model for table '${tableName}' does not provide a name for FOREIGN KEY constraint on columns: ${columnNames}`);
|
|
584
|
+
}
|
|
558
585
|
}
|
|
559
586
|
}
|
|
560
587
|
}
|
|
@@ -564,8 +591,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
564
591
|
const updateMigration = async (options) => {
|
|
565
592
|
try {
|
|
566
593
|
let version = await loadMigrationVersion(options.output);
|
|
567
|
-
|
|
568
|
-
const prevVersion = 1;
|
|
594
|
+
const prevVersion = version;
|
|
569
595
|
if (version < 1) {
|
|
570
596
|
console.log(
|
|
571
597
|
`⚠️ Initial migration not found. Run "npx forge-sql-orm migrations:create" first.`
|
|
@@ -632,6 +658,53 @@ const updateMigration = async (options) => {
|
|
|
632
658
|
process.exit(1);
|
|
633
659
|
}
|
|
634
660
|
};
|
|
661
|
+
mysqlCore.customType({
|
|
662
|
+
dataType() {
|
|
663
|
+
return "datetime";
|
|
664
|
+
},
|
|
665
|
+
toDriver(value) {
|
|
666
|
+
return moment$1(value).format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
667
|
+
},
|
|
668
|
+
fromDriver(value) {
|
|
669
|
+
const format = "YYYY-MM-DDTHH:mm:ss.SSS";
|
|
670
|
+
return parseDateTime(value, format);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
mysqlCore.customType({
|
|
674
|
+
dataType() {
|
|
675
|
+
return "timestamp";
|
|
676
|
+
},
|
|
677
|
+
toDriver(value) {
|
|
678
|
+
return moment$1(value).format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
679
|
+
},
|
|
680
|
+
fromDriver(value) {
|
|
681
|
+
const format = "YYYY-MM-DDTHH:mm:ss.SSS";
|
|
682
|
+
return parseDateTime(value, format);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
mysqlCore.customType({
|
|
686
|
+
dataType() {
|
|
687
|
+
return "date";
|
|
688
|
+
},
|
|
689
|
+
toDriver(value) {
|
|
690
|
+
return moment$1(value).format("YYYY-MM-DD");
|
|
691
|
+
},
|
|
692
|
+
fromDriver(value) {
|
|
693
|
+
const format = "YYYY-MM-DD";
|
|
694
|
+
return parseDateTime(value, format);
|
|
695
|
+
}
|
|
696
|
+
});
|
|
697
|
+
mysqlCore.customType({
|
|
698
|
+
dataType() {
|
|
699
|
+
return "time";
|
|
700
|
+
},
|
|
701
|
+
toDriver(value) {
|
|
702
|
+
return moment$1(value).format("HH:mm:ss.SSS");
|
|
703
|
+
},
|
|
704
|
+
fromDriver(value) {
|
|
705
|
+
return parseDateTime(value, "HH:mm:ss.SSS");
|
|
706
|
+
}
|
|
707
|
+
});
|
|
635
708
|
function generateMigrationUUID(version) {
|
|
636
709
|
const now = /* @__PURE__ */ new Date();
|
|
637
710
|
const timestamp = now.getTime();
|
|
@@ -693,42 +766,16 @@ const dropMigration = async (options) => {
|
|
|
693
766
|
if (!schemaModule) {
|
|
694
767
|
throw new Error(`Invalid schema file at: ${schemaPath}. Schema must export tables.`);
|
|
695
768
|
}
|
|
696
|
-
const drizzleSchema = {};
|
|
697
769
|
const tables = Object.values(schemaModule);
|
|
698
|
-
tables.
|
|
699
|
-
const symbols = Object.getOwnPropertySymbols(table);
|
|
700
|
-
const nameSymbol = symbols.find((s) => s.toString().includes("Name"));
|
|
701
|
-
const columnsSymbol = symbols.find((s) => s.toString().includes("Columns"));
|
|
702
|
-
const indexesSymbol = symbols.find((s) => s.toString().includes("Indexes"));
|
|
703
|
-
const foreignKeysSymbol = symbols.find((s) => s.toString().includes("ForeignKeys"));
|
|
704
|
-
if (table && nameSymbol && columnsSymbol) {
|
|
705
|
-
drizzleSchema[table[nameSymbol]] = {
|
|
706
|
-
// @ts-ignore
|
|
707
|
-
columns: table[columnsSymbol],
|
|
708
|
-
// @ts-ignore
|
|
709
|
-
indexes: indexesSymbol ? table[indexesSymbol] || {} : {},
|
|
710
|
-
// @ts-ignore
|
|
711
|
-
foreignKeys: foreignKeysSymbol ? table[foreignKeysSymbol] || {} : {}
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
});
|
|
715
|
-
if (Object.keys(drizzleSchema).length === 0) {
|
|
770
|
+
if (tables.length === 0) {
|
|
716
771
|
throw new Error(`No valid tables found in schema at: ${schemaPath}`);
|
|
717
772
|
}
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
}
|
|
725
|
-
for (const [indexName, index] of Object.entries(tableInfo.indexes)) {
|
|
726
|
-
if (indexName === "PRIMARY") continue;
|
|
727
|
-
dropStatements.push(`DROP INDEX \`${indexName}\` ON \`${tableName}\`;`);
|
|
728
|
-
}
|
|
729
|
-
dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
|
|
730
|
-
}
|
|
731
|
-
dropStatements.push(`DELETE FROM __migrations;`);
|
|
773
|
+
const tableNames = tables.map((table) => {
|
|
774
|
+
const metadata = getTableMetadata(table);
|
|
775
|
+
return metadata.tableName;
|
|
776
|
+
}).filter(Boolean);
|
|
777
|
+
console.log("Found tables:", tableNames);
|
|
778
|
+
const dropStatements = generateDropTableStatements(tables);
|
|
732
779
|
const migrationFile = generateMigrationFile(dropStatements, version);
|
|
733
780
|
saveMigrationFiles(migrationFile, version, options.output);
|
|
734
781
|
console.log(`✅ Migration successfully created!`);
|