116 lines
3.9 KiB
TypeScript
116 lines
3.9 KiB
TypeScript
import * as path from 'path';
|
|
import * as fs from 'fs';
|
|
import {
|
|
SystemDef,
|
|
StorageDef,
|
|
EndpointDef,
|
|
Order,
|
|
Filter,
|
|
TableDef,
|
|
ColumnDef,
|
|
BelongsToDef,
|
|
ManyToManyDef
|
|
} from '../systemGenService';
|
|
|
|
let outDir = 'test';
|
|
|
|
export function createDatabase(storageDef: StorageDef) {
|
|
let tableCreationQueries: string[] = [];
|
|
let tableDeletionQueries: string[] = [];
|
|
for (let i in storageDef.tables) {
|
|
tableCreationQueries.push(getTableCreationString(storageDef.tables[i]));
|
|
tableDeletionQueries.push(getTableDeletionString(storageDef.tables[i]));
|
|
}
|
|
for (let i in storageDef.relations) {
|
|
tableCreationQueries.push(getTableRelationsCreationString(storageDef.relations[i]));
|
|
tableDeletionQueries.push(getTableRelationsDeletionString(storageDef.relations[i]))
|
|
}
|
|
tableCreationQueries.map((query: string) => {
|
|
console.log(query);
|
|
});
|
|
|
|
tableDeletionQueries.map((query: string) => {
|
|
console.log(query);
|
|
});
|
|
|
|
return {
|
|
up: tableCreationQueries,
|
|
down: tableDeletionQueries,
|
|
};
|
|
}
|
|
|
|
function getTableCreationString(tableDef: TableDef): string {
|
|
let primaryKeyString: string = `${tableDef.name}_id VARCHAR(36) PRIMARY KEY`;
|
|
let columnString: string = `${primaryKeyString}`;
|
|
let defaultColumns: string = `created DATETIME NOT NULL DEFAULT NOW(), modified DATETIME NOT NULL DEFAULT NOW()`
|
|
let indexString: string = ``;
|
|
|
|
// columns
|
|
for (let i in tableDef.columns) {
|
|
let column: ColumnDef = tableDef.columns[i];
|
|
columnString = `${columnString}, ${createColumnString(column)}`;
|
|
}
|
|
|
|
// relations
|
|
for (let i in tableDef.relations) {
|
|
let relation: BelongsToDef = tableDef.relations[i];
|
|
columnString = `${columnString}, ${relation.table}_id VARCHAR(36)`;
|
|
}
|
|
|
|
// default created & modified
|
|
columnString = `${columnString}, ${defaultColumns}`;
|
|
|
|
return `CREATE TABLE IF NOT EXISTS ${tableDef.name} (${columnString}${indexString !== '' ? ', ' + indexString : ''});`;
|
|
}
|
|
|
|
function getTableRelationsCreationString(relationsDef: ManyToManyDef): string {
|
|
let columnString: string = `${relationsDef.left}_id VARCHAR(36) NOT NULL, ${relationsDef.right}_id VARCHAR(36) NOT NULL`;
|
|
let indexString: string = `INDEX ${relationsDef.left}_id_index (${relationsDef.left}_id), INDEX ${relationsDef.right}_id_index (${relationsDef.right}_id)`;
|
|
|
|
if (relationsDef.columns && relationsDef.columns.length) {
|
|
for (let i in relationsDef.columns) {
|
|
let column: ColumnDef = relationsDef.columns[i];
|
|
columnString = `${columnString}, ${createColumnString(column)}`;
|
|
}
|
|
}
|
|
|
|
return `CREATE TABLE IF NOT EXISTS ${relationsDef.left}_${relationsDef.right} (${columnString}${indexString !== '' ? ', ' + indexString : ''});`;
|
|
}
|
|
|
|
function createColumnString(column: ColumnDef) {
|
|
const typeMap: {[type: string]: string} = {
|
|
"blob": "BLOB",
|
|
"boolean": "BOOLEAN",
|
|
"date": "DATE",
|
|
"dateTIME": "DATETIME",
|
|
"number": "INT",
|
|
"string": "VARCHAR(255)",
|
|
};
|
|
|
|
return `${column.name} ${typeMap[column.type]}${column.nullable ? '' : ' NOT NULL'}${column.default ? ' DEFAULT ' + column.default : ''}${column.autoIncrement ? ' AUTO_INCREMENT' : ''}${column.unique ? ' UNIQUE' : ''}`;
|
|
}
|
|
|
|
function getTableDeletionString(tableDef: TableDef): string {
|
|
return `DROP TABLE IF EXISTS ${tableDef.name};`
|
|
}
|
|
|
|
function getTableRelationsDeletionString(relationsDef: ManyToManyDef) : string {
|
|
return `DROP TABLE IF EXISTS ${relationsDef.left}_${relationsDef.right};`
|
|
}
|
|
|
|
function dateString() {
|
|
let date = new Date();
|
|
return `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`;
|
|
}
|
|
|
|
export function writeMigrationsToFile(migrations: {up: string[], down: string[]}, outDir: string) {
|
|
let migrationFileContents = `
|
|
--up
|
|
${migrations.up.join('\n')}
|
|
--down
|
|
${migrations.down.join('\n')}
|
|
`;
|
|
let migrationFilePath = path.join(process.cwd(), outDir, 'src', 'migrationJobs', `${dateString()}-create-database.sql`);
|
|
fs.writeFileSync(migrationFilePath, migrationFileContents);
|
|
}
|