UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

103 lines 4.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lib_1 = require("../../lib"); exports.default = (cmd) => { const { dir, cwd, fs, env, push, generate, filename } = cmd; if (dir) { try { fs.accessSync(`${cwd}/${dir}`, fs.F_OK, { recursive: true }); } catch (e) { fs.mkdirSync(`${cwd}/${dir}`, { recursive: true }); } } if (push == null && generate == null) { throw new Error("Do you want to generate or push changes ? use '--generate' or '--push"); } if (push) { const filePath = `${cwd}/${dir}/${filename ?? 'migrations.sql'}`; const sqlString = fs.readFileSync(filePath, 'utf8'); const sqlStatements = sqlString.split(';'); const pushMigration = async (sqlStatements) => { const createTables = []; const inserts = []; for (const sql of sqlStatements) { if (sql.trim() === '') continue; const match = sql.match(/CREATE\s+DATABASE\s+IF\s+NOT\s+EXISTS\s+`([^`]+)`/i); const createDatabase = match ? match[0] : null; if (createDatabase) { const result = await new lib_1.DB() .rawQuery(createDatabase) .catch(e => console.log(`Failed to push changes errors: '${e.message}'`)); if (result != null) { console.log(`The database '${createDatabase}' has been created`); } continue; } const createTableStatements = sql .split(';') .filter((statement) => statement.replace(/--.*\n/g, '').trim().startsWith('CREATE TABLE IF NOT EXISTS')); const inserttatements = sql .split(';') .filter((statement) => statement.replace(/--.*\n/g, '').trim().startsWith('INSERT INTO')); if (!createTableStatements.length && !inserttatements.length) continue; if (createTableStatements.length) { const match = createTableStatements.join(' ').match(/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+`[^`]+`\.`([^`]+)`/i); const table = match ? match[1] : null; createTables.push({ table: table == null ? '' : table, sql: createTableStatements.join(' ') }); } if (inserttatements.length) { const match = inserttatements.join(' ').match(/INSERT\s+INTO\s+`[^`]+`\.`([^`]+)`/i); const table = match ? match[1] : null; inserts.push({ table: table == null ? '' : table, sql: inserttatements.join(' ') }); } } for (const c of createTables) { const result = await new lib_1.DB() .rawQuery(c.sql) .catch(e => console.log(`Failed to push changes errors: '${e.message}'`)); if (result != null) { console.log(`The table '${c.table}' has been created`); } } for (const c of inserts) { const result = await new lib_1.DB() .rawQuery(c.sql) .catch(e => console.log(`Failed to push changes errors: '${e.message}'`)); if (result != null) { console.log(`The data inserted into the '${c.table}' table has been successful.`); } } }; pushMigration(sqlStatements) .then(_ => console.log(`Migrations are migrating successfully`)) .catch(e => console.log(`Failed to migrate errors: '${e.message}'`)) .finally(() => process.exit(0)); return; } if (generate) { const directory = `${cwd}/${dir}/${filename ?? 'migrations.sql'}`; new lib_1.DB() .loadEnv(env) .backupToFile({ filePath: directory }) .then(_ => console.log(`Migrations are migrating successfully`)) .catch(e => console.log(`Failed to migrate errors: '${e.message}'`)) .finally(() => process.exit(0)); return; } }; //# sourceMappingURL=make-db.js.map