@nyffels/mynodeorm
Advanced tools
Comparing version 1.0.0-alpha92 to 1.0.0-alpha93
154
dist/app.js
@@ -17,2 +17,3 @@ #! /usr/bin/env node | ||
import { uniq } from "lodash-es"; | ||
import { ForeignKeyOption } from "./decorators/index.js"; | ||
const require = createRequire(import.meta.url); | ||
@@ -56,3 +57,3 @@ const args = process.argv.slice(2); | ||
const runIntegration = () => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y; | ||
const connectionstringRaw = args.find(a => a.includes('--connectionstring=')); | ||
@@ -83,5 +84,7 @@ if (!connectionstringRaw) { | ||
const [indexes] = yield connection.query(`SHOW INDEXES FROM ${table};`); | ||
const [keys] = yield connection.query(`SELECT i.CONSTRAINT_NAME, i.TABLE_NAME, k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME, r.UPDATE_RULE, r.DELETE_RULE, k.CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS i LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME LEFT JOIN information_schema.REFERENTIAL_CONSTRAINTS r ON i.CONSTRAINT_NAME = r.CONSTRAINT_NAME WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY' AND i.TABLE_NAME = '${table}' AND i.TABLE_SCHEMA = DATABASE();`); | ||
for (const column of columns) { | ||
const index = indexes.filter(e => e.Column_name == column.Field); | ||
const isUnique = !!index.find(e => !e.Non_unique && e.Key_name != "PRIMARY"); | ||
const foreignKey = (_f = keys.find(e => e.TABLE_NAME == table && e.COLUMN_NAME == column.Field)) !== null && _f !== void 0 ? _f : null; | ||
schema[table].columns[column.Field] = { | ||
@@ -95,3 +98,9 @@ type: column.Type.replace(" unsigned", ""), | ||
defaultSql: column.Default, | ||
foreignKey: null // TODO | ||
foreignKey: foreignKey ? { | ||
table: foreignKey.REFERENCED_TABLE_NAME, | ||
column: foreignKey.REFERENCED_COLUMN_NAME, | ||
onDelete: { "CASCADE": ForeignKeyOption.Cascade, "SET NULL": ForeignKeyOption.SetNull, "RESTRICT": ForeignKeyOption.Restrict }[foreignKey.DELETE_RULE], | ||
onUpdate: { "CASCADE": ForeignKeyOption.Cascade, "SET NULL": ForeignKeyOption.SetNull, "RESTRICT": ForeignKeyOption.Restrict }[foreignKey.UPDATE_RULE], | ||
name: foreignKey.CONSTRAINT_NAME | ||
} : null, | ||
}; | ||
@@ -101,3 +110,3 @@ } | ||
/* Load migration schema */ | ||
const migrationLocationPath = (_g = (_f = args.find((a) => a.includes('--migration-location='))) === null || _f === void 0 ? void 0 : _f.replace('--migration-location=', '')) !== null && _g !== void 0 ? _g : "./"; | ||
const migrationLocationPath = (_h = (_g = args.find((a) => a.includes('--migration-location='))) === null || _g === void 0 ? void 0 : _g.replace('--migration-location=', '')) !== null && _h !== void 0 ? _h : "./"; | ||
const migrationLocation = path.join(process.cwd(), migrationLocationPath, "migrations"); | ||
@@ -129,3 +138,3 @@ if (!fs.existsSync(migrationLocation)) { | ||
for (const table of addtables) { | ||
const tableSchema = (_h = migrationSchema[table]) === null || _h === void 0 ? void 0 : _h.columns; | ||
const tableSchema = (_j = migrationSchema[table]) === null || _j === void 0 ? void 0 : _j.columns; | ||
if (tableSchema === undefined) { | ||
@@ -137,2 +146,3 @@ continue; | ||
const uniqueColumns = []; | ||
const foreignKeys = []; | ||
for (const column of Object.keys(tableSchema)) { | ||
@@ -162,2 +172,5 @@ const data = tableSchema[column]; | ||
} | ||
if (data.foreignKey) { | ||
foreignKeys.push({ column: data.foreignKey.column, table: data.foreignKey.table, sourceColumn: column, onDelete: data.foreignKey.onDelete, onUpdate: data.foreignKey.onUpdate }); | ||
} | ||
} | ||
@@ -170,12 +183,36 @@ if (primaryColumns.length > 0) { | ||
} | ||
// TODO Foreign keys | ||
const sql = `CREATE TABLE ${table} | ||
( | ||
${columnSql.join(', ')} | ||
);`; | ||
for (const key of foreignKeys) { | ||
let onDeleteAction = "CASCADE"; | ||
let onUpdateAction = "CASCADE"; | ||
switch (key.onDelete) { | ||
case ForeignKeyOption.SetNull: | ||
onDeleteAction = "SET NULL"; | ||
break; | ||
case ForeignKeyOption.Restrict: | ||
onDeleteAction = "RESTRICT"; | ||
break; | ||
case ForeignKeyOption.Cascade: | ||
onDeleteAction = "CASCADE"; | ||
break; | ||
} | ||
switch (key.onUpdate) { | ||
case ForeignKeyOption.SetNull: | ||
onUpdateAction = "SET NULL"; | ||
break; | ||
case ForeignKeyOption.Restrict: | ||
onUpdateAction = "RESTRICT"; | ||
break; | ||
case ForeignKeyOption.Cascade: | ||
onUpdateAction = "CASCADE"; | ||
break; | ||
} | ||
columnSql.push(`INDEX \`fk_${key.table}_${key.column}_idx\` (\`${key.sourceColumn}\` ASC) VISIBLE`); | ||
columnSql.push(`CONSTRAINT \`fk_${key.table}_${key.column}\` FOREIGN KEY (\`${key.sourceColumn}\`) REFERENCES \`${key.table}\` (\`${key.column}\`) ON DELETE ${onDeleteAction} ON UPDATE ${onUpdateAction}`); | ||
} | ||
const sql = `CREATE TABLE ${table}(${columnSql.join(', ')});`; | ||
scriptLines.push(sql); | ||
} | ||
for (const table of updateTables) { | ||
const dbTableSchema = (_j = schema[table]) === null || _j === void 0 ? void 0 : _j.columns; | ||
const migrationTableSchema = (_k = migrationSchema[table]) === null || _k === void 0 ? void 0 : _k.columns; | ||
const dbTableSchema = (_k = schema[table]) === null || _k === void 0 ? void 0 : _k.columns; | ||
const migrationTableSchema = (_l = migrationSchema[table]) === null || _l === void 0 ? void 0 : _l.columns; | ||
const addColumnScript = []; | ||
@@ -187,2 +224,4 @@ let dropColumnScript = []; | ||
let redoPrimary = false; | ||
const dropkeys = []; | ||
const addedKeys = []; | ||
if (dbTableSchema === undefined || migrationTableSchema === undefined) { | ||
@@ -225,2 +264,11 @@ continue; | ||
} | ||
if (data.foreignKey) { | ||
addedKeys.push({ | ||
column: data.foreignKey.column, | ||
table: data.foreignKey.table, | ||
sourceColumn: column, | ||
onDelete: data.foreignKey.onDelete, | ||
onUpdate: data.foreignKey.onUpdate | ||
}); | ||
} | ||
} | ||
@@ -262,3 +310,5 @@ } | ||
} | ||
if ((dbColumn.defaultSql ? ((_l = dbColumn.defaultSql) !== null && _l !== void 0 ? _l : "").replace(/^\'/, "").replace(/\'$/, "") : dbColumn.defaultSql) != (migrationColumn.defaultSql ? ((_m = migrationColumn.defaultSql) !== null && _m !== void 0 ? _m : "").replace(/^\'/, "").replace(/\'$/, "") : migrationColumn.defaultSql)) { | ||
if ((dbColumn.defaultSql ? ((_m = dbColumn.defaultSql) !== null && _m !== void 0 ? _m : "").replace(/^\'/, "") | ||
.replace(/\'$/, "") : dbColumn.defaultSql) != (migrationColumn.defaultSql ? ((_o = migrationColumn.defaultSql) !== null && _o !== void 0 ? _o : "").replace(/^\'/, "") | ||
.replace(/\'$/, "") : migrationColumn.defaultSql)) { | ||
hasDifferences = true; | ||
@@ -280,2 +330,14 @@ } | ||
} | ||
if (dbColumn.foreignKey && (!migrationColumn.foreignKey || dbColumn.foreignKey.column != ((_p = migrationColumn.foreignKey) === null || _p === void 0 ? void 0 : _p.column) || dbColumn.foreignKey.table != ((_q = migrationColumn.foreignKey) === null || _q === void 0 ? void 0 : _q.table) || dbColumn.foreignKey.onUpdate != ((_r = migrationColumn.foreignKey) === null || _r === void 0 ? void 0 : _r.onUpdate) || dbColumn.foreignKey.onDelete != ((_s = migrationColumn.foreignKey) === null || _s === void 0 ? void 0 : _s.onDelete))) { | ||
dropkeys.push(dbColumn.foreignKey['name']); | ||
} | ||
if (migrationColumn.foreignKey !== null && (!dbColumn.foreignKey || dbColumn.foreignKey.column != ((_t = migrationColumn.foreignKey) === null || _t === void 0 ? void 0 : _t.column) || dbColumn.foreignKey.table != ((_u = migrationColumn.foreignKey) === null || _u === void 0 ? void 0 : _u.table) || dbColumn.foreignKey.onUpdate != ((_v = migrationColumn.foreignKey) === null || _v === void 0 ? void 0 : _v.onUpdate) || dbColumn.foreignKey.onDelete != ((_w = migrationColumn.foreignKey) === null || _w === void 0 ? void 0 : _w.onDelete))) { | ||
addedKeys.push({ | ||
column: migrationColumn.foreignKey.column, | ||
table: migrationColumn.foreignKey.table, | ||
sourceColumn: column, | ||
onDelete: migrationColumn.foreignKey.onDelete, | ||
onUpdate: migrationColumn.foreignKey.onUpdate, | ||
}); | ||
} | ||
if (hasDifferences) { | ||
@@ -330,2 +392,35 @@ let sql = ""; | ||
} | ||
if (dropkeys.length > 0) { | ||
scriptLines.push(`ALTER TABLE \`${table}\` ${dropkeys.map(k => `DROP FOREIGN KEY \`${k}\``).join(", ")}, ${dropkeys.map(k => `DROP INDEX \`${k}_idx\``)}`); | ||
} | ||
if (addedKeys.length > 0) { | ||
for (const key of addedKeys) { | ||
let onDeleteAction = "CASCADE"; | ||
let onUpdateAction = "CASCADE"; | ||
switch (key.onDelete) { | ||
case ForeignKeyOption.SetNull: | ||
onDeleteAction = "SET NULL"; | ||
break; | ||
case ForeignKeyOption.Restrict: | ||
onDeleteAction = "RESTRICT"; | ||
break; | ||
case ForeignKeyOption.Cascade: | ||
onDeleteAction = "CASCADE"; | ||
break; | ||
} | ||
switch (key.onUpdate) { | ||
case ForeignKeyOption.SetNull: | ||
onUpdateAction = "SET NULL"; | ||
break; | ||
case ForeignKeyOption.Restrict: | ||
onUpdateAction = "RESTRICT"; | ||
break; | ||
case ForeignKeyOption.Cascade: | ||
onUpdateAction = "CASCADE"; | ||
break; | ||
} | ||
lines.push(`ADD INDEX \`fk_${key.table}_${key.column}_idx\` (\`${key.sourceColumn}\` ASC) VISIBLE`); | ||
lines.push(`ADD CONSTRAINT \`fk_${key.table}_${key.column}\` FOREIGN KEY (\`${key.sourceColumn}\`) REFERENCES \`${key.table}\` (\`${key.column}\`) ON DELETE ${onDeleteAction} ON UPDATE ${onUpdateAction}`); | ||
} | ||
} | ||
if (lines.length > 0) { | ||
@@ -335,36 +430,7 @@ scriptLines.push(`ALTER TABLE ${table} ${lines.join(', ')};`); | ||
} | ||
// Foreign key naming scheme => FK_ChildTable_childColumn_ParentTable_parentColumn | ||
// TODO Create | ||
/* | ||
ALTER TABLE `doffice`.`tbl_account_number` | ||
ADD INDEX `FK_tbl_account_number_subscriber_id_tbl_subscriber_subscrib_idx` (`subscriber_id` ASC) VISIBLE; | ||
; | ||
ALTER TABLE `doffice`.`tbl_account_number` | ||
ADD CONSTRAINT `FK_tbl_account_number_subscriber_id_tbl_subscriber_subscriber_id` | ||
FOREIGN KEY (`subscriber_id`) | ||
REFERENCES `doffice`.`tbl_subscriber` (`subscriber_id`) | ||
ON DELETE NO ACTION | ||
ON UPDATE NO ACTION; | ||
*/ | ||
// TODO Delete | ||
/* | ||
ALTER TABLE `doffice`.`tbl_account_number` | ||
DROP FOREIGN KEY `FK_tbl_account_number_subscriber_id_tbl_subscriber_subscriber_id`; | ||
ALTER TABLE `doffice`.`tbl_account_number` | ||
DROP INDEX `FK_tbl_account_number_subscriber_id_tbl_subscriber_subscrib_idx` ; | ||
; | ||
*/ | ||
// TODO Modify | ||
// Drop first | ||
// Add later | ||
scriptLines.push(`DROP TABLE IF EXISTS __myNodeORM;`); | ||
scriptLines.push(`CREATE TABLE __myNodeORM | ||
( | ||
version VARCHAR(36) NOT NULL, | ||
DATE DATETIME NOT NULL DEFAULT NOW() | ||
);`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) | ||
VALUES ('${latestMigrationVersion}');`); | ||
scriptLines.push(`CREATE TABLE __myNodeORM (version VARCHAR(36) NOT NULL, DATE DATETIME NOT NULL DEFAULT NOW());`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) VALUES ('${latestMigrationVersion}');`); | ||
/* Save the script */ | ||
const saveLocationPath = (_p = (_o = args.find((a) => a.includes('--output='))) === null || _o === void 0 ? void 0 : _o.replace('--output=', '')) !== null && _p !== void 0 ? _p : "./"; | ||
const saveLocationPath = (_y = (_x = args.find((a) => a.includes('--output='))) === null || _x === void 0 ? void 0 : _x.replace('--output=', '')) !== null && _y !== void 0 ? _y : "./"; | ||
const saveLocation = path.join(process.cwd(), saveLocationPath, "integration-script.sql"); | ||
@@ -371,0 +437,0 @@ fs.writeFileSync(saveLocation, scriptLines.join('\n')); |
@@ -42,4 +42,3 @@ import 'reflect-metadata'; | ||
Cascade = 1, | ||
SetNull = 2, | ||
NoAction = 3 | ||
SetNull = 2 | ||
} | ||
@@ -46,0 +45,0 @@ export declare function getColumn<T>(sourceObject: Object, propertyKey: keyof T): string; |
@@ -45,3 +45,2 @@ import 'reflect-metadata'; | ||
ForeignKeyOption[ForeignKeyOption["SetNull"] = 2] = "SetNull"; | ||
ForeignKeyOption[ForeignKeyOption["NoAction"] = 3] = "NoAction"; | ||
})(ForeignKeyOption || (ForeignKeyOption = {})); | ||
@@ -48,0 +47,0 @@ export function getColumn(sourceObject, propertyKey) { |
{ | ||
"name": "@nyffels/mynodeorm", | ||
"version": "1.0.0-alpha92", | ||
"version": "1.0.0-alpha93", | ||
"description": "A full-fledged ORM framework for NodeJS and MySQL with develop friendly code aimed to handle database migrations, MySQL Query builder / helper and property mapping.", | ||
@@ -5,0 +5,0 @@ "private": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
180809
2533