@nyffels/mynodeorm
Advanced tools
Comparing version 1.0.0-alpha82 to 1.0.0-alpha83
181
dist/app.js
@@ -16,2 +16,3 @@ #! /usr/bin/env node | ||
import { createRequire } from 'module'; | ||
import { uniq } from "lodash-es"; | ||
const require = createRequire(import.meta.url); | ||
@@ -33,2 +34,3 @@ const args = process.argv.slice(2); | ||
} | ||
// TODO Scan for classes? | ||
let migrationsScript = ` | ||
@@ -55,3 +57,3 @@ import {createMigration} from '@nyffels/mynodeorm/dist/logic/migration.logic.js'; | ||
const runIntegration = () => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j; | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
const connectionstringRaw = args.find(a => a.includes('--connectionstring=')); | ||
@@ -100,5 +102,13 @@ if (!connectionstringRaw) { | ||
const migrationLocation = path.join(process.cwd(), migrationLocationPath, "migrations"); | ||
const latestMigrationVersion = fs.readdirSync(migrationLocation, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name).sort().reverse().find(x => x); | ||
const latestMigrationVersion = fs.readdirSync(migrationLocation, { withFileTypes: true }) | ||
.filter(e => e.isDirectory()) | ||
.map(e => e.name) | ||
.sort() | ||
.reverse() | ||
.find(x => x); | ||
const migrationSchema = JSON.parse(fs.readFileSync(path.join(migrationLocation, "schema.json")) | ||
.toString()); | ||
if (migrationSchema === undefined) { | ||
console.error("Migration schema not found"); | ||
} | ||
/* Compare current schema to migration schema with script creation */ | ||
@@ -115,11 +125,15 @@ const scriptLines = [`USE ${database};`]; | ||
for (const table of addtables) { | ||
const tableSchema = migrationSchema[table]; | ||
const tableSchema = (_h = migrationSchema[table]) === null || _h === void 0 ? void 0 : _h.columns; | ||
if (tableSchema === undefined) { | ||
continue; | ||
} | ||
const columnSql = []; | ||
const primaryColumns = []; | ||
const uniqueColumns = []; | ||
for (const column of Object.keys(tableSchema.columns)) { | ||
const data = tableSchema.columns[column]; | ||
for (const column of Object.keys(tableSchema)) { | ||
const data = tableSchema[column]; | ||
if (data == null) { | ||
continue; | ||
} | ||
let sql = ""; | ||
console.log(column); | ||
console.log(data); | ||
sql += `${column} ${data.type}`; | ||
@@ -151,14 +165,153 @@ if (data.unsigned) { | ||
// TODO Foreign keys | ||
const sql = `CREATE TABLE ${table} (${columnSql.join(', ')});`; | ||
const sql = `CREATE TABLE ${table} | ||
( | ||
${columnSql.join(', ')} | ||
);`; | ||
scriptLines.push(sql); | ||
} | ||
const redoPrimaryKeys = []; | ||
for (const table of updateTables) { | ||
const existingSchema = schema[table]; | ||
const tableSchema = migrationSchema[table]; | ||
// TODO Check for differences. | ||
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 addColumnScript = []; | ||
let dropColumnScript = null; | ||
const modifyColumnScript = []; | ||
const addedUniqColumns = []; | ||
if (dbTableSchema === undefined || migrationTableSchema === undefined) { | ||
continue; | ||
} | ||
const columnsToAdd = Object.keys(migrationTableSchema) | ||
.filter(e => !Object.keys(dbTableSchema) | ||
.includes(e)); | ||
const columnsToDelete = Object.keys(dbTableSchema) | ||
.filter(e => !Object.keys(migrationTableSchema) | ||
.includes(e)); | ||
const columnsToCheck = Object.keys(migrationTableSchema) | ||
.filter(e => Object.keys(dbTableSchema) | ||
.includes(e)); | ||
if (columnsToAdd.length > 0) { | ||
for (const column of columnsToAdd) { | ||
const data = migrationTableSchema[column]; | ||
if (data === undefined) { | ||
continue; | ||
} | ||
let sql = ""; | ||
sql += `${column} ${data.type}`; | ||
if (data.unsigned) { | ||
sql += ` UNSIGNED`; | ||
} | ||
sql += ` ${data.nullable ? 'NULL' : 'NOT NULL'}`; | ||
if (data.defaultSql) { | ||
sql += ` DEFAULT ${data.defaultSql}`; | ||
} | ||
if (data.autoIncrement) { | ||
sql += ` AUTO_INCREMENT`; | ||
} | ||
addColumnScript.push(`ADD COLUMN ${sql}`); | ||
if (data.primary) { | ||
redoPrimaryKeys.push(table); | ||
} | ||
if (data.unique) { | ||
addedUniqColumns.push(column); | ||
} | ||
} | ||
} | ||
if (columnsToDelete.length > 0) { | ||
dropColumnScript = `DROP COLUMN ${columnsToDelete.join(', ')}`; | ||
} | ||
if (columnsToCheck.length > 0) { | ||
for (const column of columnsToCheck) { | ||
let hasDifferences = false; | ||
const differences = []; | ||
const dbColumn = dbTableSchema[column]; | ||
const migrationColumn = migrationTableSchema[column]; | ||
if (dbColumn === undefined || migrationColumn === undefined) { | ||
continue; | ||
} | ||
if (dbColumn.type.toLowerCase().replaceAll(" ", "") != migrationColumn.type.toLowerCase().replaceAll(" ", "")) { | ||
hasDifferences = true; | ||
differences.push("type"); | ||
if (table === "tbl_account_number" && hasDifferences) { | ||
console.log(column); | ||
console.log(dbColumn.type); | ||
console.log(migrationColumn.type); | ||
} | ||
} | ||
if (dbColumn.unique != migrationColumn.unique) { | ||
if (migrationColumn.unique) { | ||
addedUniqColumns.push(column); | ||
} | ||
else { | ||
// TODO DROP uniq column | ||
} | ||
} | ||
; | ||
if (dbColumn.autoIncrement != migrationColumn.autoIncrement) { | ||
hasDifferences = true; | ||
differences.push("autoIncrement"); | ||
} | ||
if (dbColumn.nullable != migrationColumn.nullable) { | ||
hasDifferences = true; | ||
differences.push("nullable"); | ||
} | ||
if (dbColumn.defaultSql != migrationColumn.defaultSql) { | ||
hasDifferences = true; | ||
differences.push("defaultSql"); | ||
} | ||
if (dbColumn.unsigned != migrationColumn.unsigned) { | ||
hasDifferences = true; | ||
differences.push("unsigned"); | ||
} | ||
if (hasDifferences) { | ||
let sql = ""; | ||
sql += `${column} ${migrationColumn.type}`; | ||
if (migrationColumn.unsigned) { | ||
sql += ` UNSIGNED`; | ||
} | ||
sql += ` ${migrationColumn.nullable ? 'NULL' : 'NOT NULL'}`; | ||
if (migrationColumn.defaultSql) { | ||
sql += ` DEFAULT ${migrationColumn.defaultSql}`; | ||
} | ||
if (migrationColumn.autoIncrement) { | ||
sql += ` AUTO_INCREMENT`; | ||
} | ||
modifyColumnScript.push(`MODIFY COLUMN ${sql}`); | ||
} | ||
// TODO Foreign keys | ||
if (dbColumn.primary != migrationColumn.primary) { | ||
redoPrimaryKeys.push(table); | ||
} | ||
} | ||
} | ||
let lines = []; | ||
if (addColumnScript.length > 0) { | ||
lines = lines.concat(addColumnScript); | ||
} | ||
if (dropColumnScript) { | ||
lines.push(dropColumnScript); | ||
} | ||
if (modifyColumnScript.length > 0) { | ||
lines = lines.concat(modifyColumnScript); | ||
} | ||
if (addedUniqColumns.length > 0) { | ||
for (const column of uniq(addedUniqColumns)) { | ||
lines.push(`ADD UNIQUE INDEX ${column}_UNIQUE (${column} ASC) VISIBLE`); | ||
} | ||
} | ||
scriptLines.push(`ALTER TABLE ${table} ${lines.join(', ')};`); | ||
} | ||
scriptLines.push(`CREATE TABLE __myNodeORM (version VARCHAR(36) NOT NULL, DATE DATETIME NOT NULL DEFAULT NOW());`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) VALUES (${latestMigrationVersion});`); | ||
if (redoPrimaryKeys.length > 0) { | ||
for (const table of uniq(redoPrimaryKeys)) { | ||
// TODO | ||
} | ||
} | ||
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 = (_j = (_h = args.find((a) => a.includes('--output='))) === null || _h === void 0 ? void 0 : _h.replace('--output=', '')) !== null && _j !== void 0 ? _j : "./"; | ||
const saveLocationPath = (_m = (_l = args.find((a) => a.includes('--output='))) === null || _l === void 0 ? void 0 : _l.replace('--output=', '')) !== null && _m !== void 0 ? _m : "./"; | ||
const saveLocation = path.join(process.cwd(), saveLocationPath, "integration-script.sql"); | ||
@@ -165,0 +318,0 @@ fs.writeFileSync(saveLocation, scriptLines.join('\n')); |
@@ -17,3 +17,2 @@ import 'reflect-metadata'; | ||
export declare function getObjectById(id: string): Object | null; | ||
export declare function getObjectsInIdTable(): ITableIdentification[]; | ||
export {}; |
@@ -30,5 +30,2 @@ import 'reflect-metadata'; | ||
} | ||
export function getObjectsInIdTable() { | ||
return TableIdentifications; | ||
} | ||
//# sourceMappingURL=class.decorator.js.map |
@@ -157,3 +157,3 @@ import 'reflect-metadata'; | ||
} | ||
return `INT(${intLength})`; | ||
return intLength === 255 ? `INT` : `INT(${intLength})`; | ||
} | ||
@@ -165,3 +165,3 @@ } | ||
case "boolean": { | ||
return "TINYINT(1)"; | ||
return "TINYINT"; | ||
} | ||
@@ -168,0 +168,0 @@ case "date": { |
@@ -0,0 +0,0 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
@@ -42,3 +42,3 @@ export declare abstract class MigrationFileBuilder { | ||
unsigned(): this; | ||
autoincrement(): this; | ||
autoIncrement(): this; | ||
defaultSql(sql: string): this; | ||
@@ -45,0 +45,0 @@ getValues(): { |
@@ -144,3 +144,3 @@ export class MigrationFileBuilder { | ||
} | ||
autoincrement() { | ||
autoIncrement() { | ||
this._autoIncrement = true; | ||
@@ -147,0 +147,0 @@ return this; |
{ | ||
"name": "@nyffels/mynodeorm", | ||
"version": "1.0.0-alpha82", | ||
"version": "1.0.0-alpha83", | ||
"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, |
@@ -0,0 +0,0 @@ # MyNodeORM |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
163082
2378