@nyffels/mynodeorm
Advanced tools
Comparing version 1.0.0-alpha97 to 1.0.0-alpha98
@@ -117,3 +117,3 @@ #! /usr/bin/env node | ||
.find(x => x); | ||
const migrationSchema = JSON.parse(fs.readFileSync(path.join(migrationLocation, "schema.json")) | ||
const migrationSchema = JSON.parse(fs.readFileSync(path.join(migrationLocation, (latestMigrationVersion !== null && latestMigrationVersion !== void 0 ? latestMigrationVersion : ""), "schema.json")) | ||
.toString()); | ||
@@ -418,4 +418,4 @@ if (migrationSchema === undefined) { | ||
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 INT NOT NULL, DATE DATETIME NOT NULL DEFAULT NOW());`); | ||
scriptLines.push(`INSERT INTO __myNodeORM (version) VALUES (${(latestMigrationVersion !== null && latestMigrationVersion !== void 0 ? latestMigrationVersion : "").split(".").find(x => x)});`); | ||
/* Save the script */ | ||
@@ -422,0 +422,0 @@ const saveLocationPath = (_y = (_x = args.find((a) => a.includes('--output='))) === null || _x === void 0 ? void 0 : _x.replace('--output=', '')) !== null && _y !== void 0 ? _y : "./"; |
@@ -7,3 +7,3 @@ import path from "node:path"; | ||
export function createMigration(name, migrationLocationPath, classes) { | ||
var _a, _b; | ||
var _a, _b, _c; | ||
if (!name) { | ||
@@ -22,2 +22,3 @@ console.error("❌ Name is required for a migration. Use '--name={{name}}' to declare a name of this migration."); | ||
let version = 0; | ||
const migrationName = `${version}.${getDateFormat()}_${name}`; | ||
let oldSchema; | ||
@@ -29,6 +30,5 @@ if ((folders).length > 0) { | ||
.reverse()[0]) + 1; | ||
oldSchema = JSON.parse(fs.readFileSync(path.join(migrationLocation, "schema.json")) | ||
oldSchema = JSON.parse(fs.readFileSync(path.join(migrationLocation, ((_a = folders.sort().reverse().find(x => x)) !== null && _a !== void 0 ? _a : ""), "schema.json")) | ||
.toString()); | ||
} | ||
const migrationName = `${version}.${getDateFormat()}_${name}`; | ||
console.log("• Creating schema..."); | ||
@@ -56,3 +56,3 @@ const schema = {}; | ||
autoIncrement: getAutoIncrement(dbClass, property), | ||
defaultSql: (_a = getDefaultSql(dbClass, property)) !== null && _a !== void 0 ? _a : null, | ||
defaultSql: (_b = getDefaultSql(dbClass, property)) !== null && _b !== void 0 ? _b : null, | ||
foreignKey: getForeignKey(dbClass, property), | ||
@@ -62,3 +62,3 @@ }; | ||
} | ||
fs.writeFileSync(path.join(migrationLocation, "schema.json"), JSON.stringify(schema)); | ||
fs.writeFileSync(path.join(migrationLocation, migrationName, "schema.json"), JSON.stringify(schema)); | ||
console.log("• Schema created."); | ||
@@ -72,3 +72,3 @@ if (version === 0) { | ||
downQueries.push(`DROP TABLE ${table}`); | ||
const tableSchema = (_b = schema[table]) === null || _b === void 0 ? void 0 : _b.columns; | ||
const tableSchema = (_c = schema[table]) === null || _c === void 0 ? void 0 : _c.columns; | ||
if (tableSchema === undefined) { | ||
@@ -148,2 +148,3 @@ continue; | ||
migrationFileContent = migrationFileContent.replace("{{{{TEMPLATE-DATA-DOWN}}}}", downQueries.map(q => ` this._builder.addQuery('${q.replaceAll("'", "\\'")}');`).join("\n")); | ||
migrationFileContent = migrationFileContent.replace("{{{{VERSION}}}}", version.toString()); | ||
mkdirSync(path.join(migrationLocation, migrationName), { recursive: true }); | ||
@@ -154,4 +155,9 @@ fs.writeFileSync(path.join(migrationLocation, migrationName, "migration-plan.ts"), migrationFileContent); | ||
else { | ||
console.log("• Creating migration file..."); | ||
let migrationFileContent = MigrationFileBuilder.GetFileTemplate(); | ||
const scriptLines = []; | ||
// const newSchema = Object.keys(schema); | ||
// const oldSchema = Object.keys(oldSchema); | ||
// A schema exists! | ||
// console.log("• Creating migration file..."); | ||
// | ||
// let migrationFileContent = MigrationFileBuilder.GetFileTemplate(); | ||
@@ -305,3 +311,3 @@ // | ||
// fs.writeFileSync(path.join(migrationLocation, migrationName, "migration-plan.ts"), migrationFileContent); | ||
console.log("• Migration file created."); | ||
// console.log("• Migration file created."); | ||
} | ||
@@ -308,0 +314,0 @@ console.log("✅ Migration completed."); |
@@ -7,2 +7,3 @@ export class MigrationFileBuilder { | ||
private _builder = new MigrationBuilder(); | ||
private _version = {{{{VERSION}}}} | ||
@@ -23,3 +24,3 @@ public async up() { | ||
public async down() { | ||
public async down(this._version) { | ||
/* | ||
@@ -35,3 +36,3 @@ You can add custom data here to be run before the migration plan. | ||
this._builder.execute(); | ||
this._builder.execute(this._version - 1); | ||
} | ||
@@ -38,0 +39,0 @@ }`; |
{ | ||
"name": "@nyffels/mynodeorm", | ||
"version": "1.0.0-alpha97", | ||
"version": "1.0.0-alpha98", | ||
"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
Sorry, the diff of this file is not supported yet
172580
2403