forge-sql-orm 2.0.5 → 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 +79 -37
- package/dist-cli/cli.js.map +1 -1
- package/dist-cli/cli.mjs +79 -37
- 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.mjs
CHANGED
|
@@ -7,8 +7,11 @@ import path from "path";
|
|
|
7
7
|
import "reflect-metadata";
|
|
8
8
|
import { execSync } from "child_process";
|
|
9
9
|
import mysql from "mysql2/promise";
|
|
10
|
-
import "moment";
|
|
10
|
+
import moment from "moment";
|
|
11
11
|
import { UniqueConstraintBuilder } from "drizzle-orm/mysql-core/unique-constraint";
|
|
12
|
+
import "@forge/sql";
|
|
13
|
+
import { customType } from "drizzle-orm/mysql-core";
|
|
14
|
+
import moment$1 from "moment/moment.js";
|
|
12
15
|
function replaceMySQLTypes(schemaContent) {
|
|
13
16
|
const imports = `import { forgeDateTimeString, forgeTimeString, forgeDateString, forgeTimestampString } from "forge-sql-orm";
|
|
14
17
|
|
|
@@ -223,6 +226,13 @@ const createMigration = async (options) => {
|
|
|
223
226
|
process.exit(1);
|
|
224
227
|
}
|
|
225
228
|
};
|
|
229
|
+
const parseDateTime = (value, format) => {
|
|
230
|
+
const m = moment(value, format, true);
|
|
231
|
+
if (!m.isValid()) {
|
|
232
|
+
return moment(value).toDate();
|
|
233
|
+
}
|
|
234
|
+
return m.toDate();
|
|
235
|
+
};
|
|
226
236
|
function processForeignKeys(table, foreignKeysSymbol, extraSymbol) {
|
|
227
237
|
const foreignKeys = [];
|
|
228
238
|
if (foreignKeysSymbol) {
|
|
@@ -305,6 +315,17 @@ function getTableMetadata(table) {
|
|
|
305
315
|
...builders
|
|
306
316
|
};
|
|
307
317
|
}
|
|
318
|
+
function generateDropTableStatements(tables) {
|
|
319
|
+
const dropStatements = [];
|
|
320
|
+
tables.forEach((table) => {
|
|
321
|
+
const tableMetadata = getTableMetadata(table);
|
|
322
|
+
if (tableMetadata.tableName) {
|
|
323
|
+
dropStatements.push(`DROP TABLE IF EXISTS \`${tableMetadata.tableName}\`;`);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
dropStatements.push(`DELETE FROM __migrations;`);
|
|
327
|
+
return dropStatements;
|
|
328
|
+
}
|
|
308
329
|
function generateMigrationFile$1(createStatements, version) {
|
|
309
330
|
const versionPrefix = `v${version}_MIGRATION`;
|
|
310
331
|
const migrationLines = createStatements.map(
|
|
@@ -482,7 +503,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
482
503
|
}
|
|
483
504
|
const columns2 = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
484
505
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
485
|
-
changes.push(`CREATE if not exists
|
|
506
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns2});`);
|
|
486
507
|
}
|
|
487
508
|
for (const [fkName, dbFK] of Object.entries(dbTable.foreignKeys)) {
|
|
488
509
|
changes.push(`ALTER TABLE \`${tableName}\` ADD CONSTRAINT \`${fkName}\` FOREIGN KEY (\`${dbFK.column}\`) REFERENCES \`${dbFK.referencedTable}\` (\`${dbFK.referencedColumn}\`);`);
|
|
@@ -527,7 +548,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
527
548
|
if (!drizzleIndex) {
|
|
528
549
|
const columns = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
529
550
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
530
|
-
changes.push(`CREATE if not exists
|
|
551
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns});`);
|
|
531
552
|
continue;
|
|
532
553
|
}
|
|
533
554
|
const dbColumns = dbIndex.columns.join(", ");
|
|
@@ -536,7 +557,7 @@ function generateSchemaChanges(drizzleSchema, dbSchema, schemaModule) {
|
|
|
536
557
|
changes.push(`DROP INDEX \`${indexName}\` ON \`${tableName}\`;`);
|
|
537
558
|
const columns = dbIndex.columns.map((col) => `\`${col}\``).join(", ");
|
|
538
559
|
const unique = dbIndex.unique ? "UNIQUE " : "";
|
|
539
|
-
changes.push(`CREATE
|
|
560
|
+
changes.push(`CREATE ${unique}INDEX if not exists \`${indexName}\` ON \`${tableName}\` (${columns});`);
|
|
540
561
|
}
|
|
541
562
|
}
|
|
542
563
|
for (const [fkName, dbFK] of Object.entries(dbTable.foreignKeys)) {
|
|
@@ -636,6 +657,53 @@ const updateMigration = async (options) => {
|
|
|
636
657
|
process.exit(1);
|
|
637
658
|
}
|
|
638
659
|
};
|
|
660
|
+
customType({
|
|
661
|
+
dataType() {
|
|
662
|
+
return "datetime";
|
|
663
|
+
},
|
|
664
|
+
toDriver(value) {
|
|
665
|
+
return moment$1(value).format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
666
|
+
},
|
|
667
|
+
fromDriver(value) {
|
|
668
|
+
const format = "YYYY-MM-DDTHH:mm:ss.SSS";
|
|
669
|
+
return parseDateTime(value, format);
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
customType({
|
|
673
|
+
dataType() {
|
|
674
|
+
return "timestamp";
|
|
675
|
+
},
|
|
676
|
+
toDriver(value) {
|
|
677
|
+
return moment$1(value).format("YYYY-MM-DDTHH:mm:ss.SSS");
|
|
678
|
+
},
|
|
679
|
+
fromDriver(value) {
|
|
680
|
+
const format = "YYYY-MM-DDTHH:mm:ss.SSS";
|
|
681
|
+
return parseDateTime(value, format);
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
customType({
|
|
685
|
+
dataType() {
|
|
686
|
+
return "date";
|
|
687
|
+
},
|
|
688
|
+
toDriver(value) {
|
|
689
|
+
return moment$1(value).format("YYYY-MM-DD");
|
|
690
|
+
},
|
|
691
|
+
fromDriver(value) {
|
|
692
|
+
const format = "YYYY-MM-DD";
|
|
693
|
+
return parseDateTime(value, format);
|
|
694
|
+
}
|
|
695
|
+
});
|
|
696
|
+
customType({
|
|
697
|
+
dataType() {
|
|
698
|
+
return "time";
|
|
699
|
+
},
|
|
700
|
+
toDriver(value) {
|
|
701
|
+
return moment$1(value).format("HH:mm:ss.SSS");
|
|
702
|
+
},
|
|
703
|
+
fromDriver(value) {
|
|
704
|
+
return parseDateTime(value, "HH:mm:ss.SSS");
|
|
705
|
+
}
|
|
706
|
+
});
|
|
639
707
|
function generateMigrationUUID(version) {
|
|
640
708
|
const now = /* @__PURE__ */ new Date();
|
|
641
709
|
const timestamp = now.getTime();
|
|
@@ -697,42 +765,16 @@ const dropMigration = async (options) => {
|
|
|
697
765
|
if (!schemaModule) {
|
|
698
766
|
throw new Error(`Invalid schema file at: ${schemaPath}. Schema must export tables.`);
|
|
699
767
|
}
|
|
700
|
-
const drizzleSchema = {};
|
|
701
768
|
const tables = Object.values(schemaModule);
|
|
702
|
-
tables.
|
|
703
|
-
const symbols = Object.getOwnPropertySymbols(table);
|
|
704
|
-
const nameSymbol = symbols.find((s) => s.toString().includes("Name"));
|
|
705
|
-
const columnsSymbol = symbols.find((s) => s.toString().includes("Columns"));
|
|
706
|
-
const indexesSymbol = symbols.find((s) => s.toString().includes("Indexes"));
|
|
707
|
-
const foreignKeysSymbol = symbols.find((s) => s.toString().includes("ForeignKeys"));
|
|
708
|
-
if (table && nameSymbol && columnsSymbol) {
|
|
709
|
-
drizzleSchema[table[nameSymbol]] = {
|
|
710
|
-
// @ts-ignore
|
|
711
|
-
columns: table[columnsSymbol],
|
|
712
|
-
// @ts-ignore
|
|
713
|
-
indexes: indexesSymbol ? table[indexesSymbol] || {} : {},
|
|
714
|
-
// @ts-ignore
|
|
715
|
-
foreignKeys: foreignKeysSymbol ? table[foreignKeysSymbol] || {} : {}
|
|
716
|
-
};
|
|
717
|
-
}
|
|
718
|
-
});
|
|
719
|
-
if (Object.keys(drizzleSchema).length === 0) {
|
|
769
|
+
if (tables.length === 0) {
|
|
720
770
|
throw new Error(`No valid tables found in schema at: ${schemaPath}`);
|
|
721
771
|
}
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
for (const [indexName, index] of Object.entries(tableInfo.indexes)) {
|
|
730
|
-
if (indexName === "PRIMARY") continue;
|
|
731
|
-
dropStatements.push(`DROP INDEX \`${indexName}\` ON \`${tableName}\`;`);
|
|
732
|
-
}
|
|
733
|
-
dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
|
|
734
|
-
}
|
|
735
|
-
dropStatements.push(`DELETE FROM __migrations;`);
|
|
772
|
+
const tableNames = tables.map((table) => {
|
|
773
|
+
const metadata = getTableMetadata(table);
|
|
774
|
+
return metadata.tableName;
|
|
775
|
+
}).filter(Boolean);
|
|
776
|
+
console.log("Found tables:", tableNames);
|
|
777
|
+
const dropStatements = generateDropTableStatements(tables);
|
|
736
778
|
const migrationFile = generateMigrationFile(dropStatements, version);
|
|
737
779
|
saveMigrationFiles(migrationFile, version, options.output);
|
|
738
780
|
console.log(`✅ Migration successfully created!`);
|