forge-sql-orm 2.0.27 → 2.0.29
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/ForgeSQLORM.js +51 -21
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +51 -21
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLAnalyseOperations.d.ts.map +1 -1
- package/dist/utils/sqlUtils.d.ts +13 -6
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts +5 -5
- package/dist/webtriggers/dropTablesMigrationWebTrigger.d.ts +26 -0
- package/dist/webtriggers/dropTablesMigrationWebTrigger.d.ts.map +1 -0
- package/dist/webtriggers/index.d.ts +1 -0
- package/dist/webtriggers/index.d.ts.map +1 -1
- package/package.json +16 -10
- package/src/core/ForgeSQLAnalyseOperations.ts +3 -2
- package/src/core/ForgeSQLQueryBuilder.ts +8 -8
- package/src/utils/sqlUtils.ts +40 -17
- package/src/webtriggers/dropMigrationWebTrigger.ts +6 -6
- package/src/webtriggers/dropTablesMigrationWebTrigger.ts +50 -0
- package/src/webtriggers/index.ts +1 -0
package/dist/ForgeSQLORM.mjs
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { isTable, sql, eq, and } from "drizzle-orm";
|
|
2
|
-
import
|
|
2
|
+
import { DateTime } from "luxon";
|
|
3
3
|
import { isSQLWrapper } from "drizzle-orm/sql/sql";
|
|
4
4
|
import { sql as sql$1, migrationRunner } from "@forge/sql";
|
|
5
5
|
import { drizzle } from "drizzle-orm/mysql-proxy";
|
|
6
6
|
import { customType, mysqlTable, timestamp, varchar, bigint } from "drizzle-orm/mysql-core";
|
|
7
|
-
import moment$1 from "moment/moment.js";
|
|
8
7
|
import { getTableName } from "drizzle-orm/table";
|
|
9
8
|
const parseDateTime = (value, format) => {
|
|
10
9
|
let result;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
if (value instanceof Date) {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
const dt = DateTime.fromFormat(value, format);
|
|
14
|
+
if (dt.isValid) {
|
|
15
|
+
result = dt.toJSDate();
|
|
16
|
+
} else {
|
|
17
|
+
const isoDt = DateTime.fromISO(value);
|
|
18
|
+
if (isoDt.isValid) {
|
|
19
|
+
result = isoDt.toJSDate();
|
|
16
20
|
} else {
|
|
17
21
|
result = new Date(value);
|
|
18
22
|
}
|
|
19
|
-
} else {
|
|
20
|
-
result = m.toDate();
|
|
21
23
|
}
|
|
22
24
|
if (isNaN(result.getTime())) {
|
|
23
25
|
result = new Date(value);
|
|
@@ -125,11 +127,20 @@ function getTableMetadata(table) {
|
|
|
125
127
|
...builders
|
|
126
128
|
};
|
|
127
129
|
}
|
|
128
|
-
function generateDropTableStatements(tables) {
|
|
130
|
+
function generateDropTableStatements(tables, options) {
|
|
129
131
|
const dropStatements = [];
|
|
132
|
+
const validOptions = options ?? { sequence: true, table: true };
|
|
133
|
+
if (!validOptions.sequence && !validOptions.table) {
|
|
134
|
+
console.warn('No drop operations requested: both "table" and "sequence" options are false');
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
130
137
|
tables.forEach((tableName) => {
|
|
131
|
-
|
|
132
|
-
|
|
138
|
+
if (validOptions.table) {
|
|
139
|
+
dropStatements.push(`DROP TABLE IF EXISTS \`${tableName}\`;`);
|
|
140
|
+
}
|
|
141
|
+
if (validOptions.sequence) {
|
|
142
|
+
dropStatements.push(`DROP SEQUENCE IF EXISTS \`${tableName}\`;`);
|
|
143
|
+
}
|
|
133
144
|
});
|
|
134
145
|
return dropStatements;
|
|
135
146
|
}
|
|
@@ -899,7 +910,7 @@ class ForgeSQLAnalyseOperation {
|
|
|
899
910
|
* @returns {string} The SQL query for cluster statement history
|
|
900
911
|
*/
|
|
901
912
|
buildClusterStatementQuery(tables, from, to) {
|
|
902
|
-
const formatDateTime = (date) =>
|
|
913
|
+
const formatDateTime = (date) => DateTime.fromJSDate(date).toFormat("yyyy-LL-dd'T'HH:mm:ss.SSS");
|
|
903
914
|
const tableConditions = tables.map((table) => `TABLE_NAMES LIKE CONCAT(SCHEMA_NAME, '.', '%', '${table}', '%')`).join(" OR ");
|
|
904
915
|
const timeConditions = [];
|
|
905
916
|
if (from) {
|
|
@@ -1189,10 +1200,10 @@ const forgeDateTimeString = customType({
|
|
|
1189
1200
|
return "datetime";
|
|
1190
1201
|
},
|
|
1191
1202
|
toDriver(value) {
|
|
1192
|
-
return
|
|
1203
|
+
return DateTime.fromJSDate(value).toFormat("yyyy-LL-dd'T'HH:mm:ss.SSS");
|
|
1193
1204
|
},
|
|
1194
1205
|
fromDriver(value) {
|
|
1195
|
-
const format = "
|
|
1206
|
+
const format = "yyyy-LL-dd'T'HH:mm:ss.SSS";
|
|
1196
1207
|
return parseDateTime(value, format);
|
|
1197
1208
|
}
|
|
1198
1209
|
});
|
|
@@ -1201,10 +1212,10 @@ const forgeTimestampString = customType({
|
|
|
1201
1212
|
return "timestamp";
|
|
1202
1213
|
},
|
|
1203
1214
|
toDriver(value) {
|
|
1204
|
-
return
|
|
1215
|
+
return DateTime.fromJSDate(value).toFormat("yyyy-LL-dd'T'HH:mm:ss.SSS");
|
|
1205
1216
|
},
|
|
1206
1217
|
fromDriver(value) {
|
|
1207
|
-
const format = "
|
|
1218
|
+
const format = "yyyy-LL-dd'T'HH:mm:ss.SSS";
|
|
1208
1219
|
return parseDateTime(value, format);
|
|
1209
1220
|
}
|
|
1210
1221
|
});
|
|
@@ -1213,10 +1224,10 @@ const forgeDateString = customType({
|
|
|
1213
1224
|
return "date";
|
|
1214
1225
|
},
|
|
1215
1226
|
toDriver(value) {
|
|
1216
|
-
return
|
|
1227
|
+
return DateTime.fromJSDate(value).toFormat("yyyy-LL-dd");
|
|
1217
1228
|
},
|
|
1218
1229
|
fromDriver(value) {
|
|
1219
|
-
const format = "
|
|
1230
|
+
const format = "yyyy-LL-dd";
|
|
1220
1231
|
return parseDateTime(value, format);
|
|
1221
1232
|
}
|
|
1222
1233
|
});
|
|
@@ -1225,7 +1236,7 @@ const forgeTimeString = customType({
|
|
|
1225
1236
|
return "time";
|
|
1226
1237
|
},
|
|
1227
1238
|
toDriver(value) {
|
|
1228
|
-
return
|
|
1239
|
+
return DateTime.fromJSDate(value).toFormat("HH:mm:ss.SSS");
|
|
1229
1240
|
},
|
|
1230
1241
|
fromDriver(value) {
|
|
1231
1242
|
return parseDateTime(value, "HH:mm:ss.SSS");
|
|
@@ -1244,7 +1255,7 @@ const forgeSystemTables = [migrations];
|
|
|
1244
1255
|
async function dropSchemaMigrations() {
|
|
1245
1256
|
try {
|
|
1246
1257
|
const tables = await getTables();
|
|
1247
|
-
const dropStatements = generateDropTableStatements(tables);
|
|
1258
|
+
const dropStatements = generateDropTableStatements(tables, { sequence: true, table: true });
|
|
1248
1259
|
for (const statement of dropStatements) {
|
|
1249
1260
|
console.warn(statement);
|
|
1250
1261
|
await sql$1.executeDDL(statement);
|
|
@@ -1324,6 +1335,24 @@ function formatCreateTableStatement(statement) {
|
|
|
1324
1335
|
function wrapWithForeignKeyChecks(statements) {
|
|
1325
1336
|
return ["SET foreign_key_checks = 0", ...statements, "SET foreign_key_checks = 1"];
|
|
1326
1337
|
}
|
|
1338
|
+
async function dropTableSchemaMigrations() {
|
|
1339
|
+
try {
|
|
1340
|
+
const tables = await getTables();
|
|
1341
|
+
const dropStatements = generateDropTableStatements(tables, { sequence: false, table: true });
|
|
1342
|
+
for (const statement of dropStatements) {
|
|
1343
|
+
console.warn(statement);
|
|
1344
|
+
await sql$1.executeDDL(statement);
|
|
1345
|
+
}
|
|
1346
|
+
return getHttpResponse(
|
|
1347
|
+
200,
|
|
1348
|
+
"⚠️ All data in these tables has been permanently deleted. This operation cannot be undone."
|
|
1349
|
+
);
|
|
1350
|
+
} catch (error) {
|
|
1351
|
+
console.error(error);
|
|
1352
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
|
|
1353
|
+
return getHttpResponse(500, errorMessage);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1327
1356
|
const getHttpResponse = (statusCode, body) => {
|
|
1328
1357
|
let statusText = "";
|
|
1329
1358
|
if (statusCode === 200) {
|
|
@@ -1345,6 +1374,7 @@ export {
|
|
|
1345
1374
|
applySchemaMigrations,
|
|
1346
1375
|
ForgeSQLORM as default,
|
|
1347
1376
|
dropSchemaMigrations,
|
|
1377
|
+
dropTableSchemaMigrations,
|
|
1348
1378
|
fetchSchemaWebTrigger,
|
|
1349
1379
|
forgeDateString,
|
|
1350
1380
|
forgeDateTimeString,
|