Files
system-builder/src/processDef.ts

150 lines
4.0 KiB
TypeScript

import * as path from 'path';
import simmer from './simmer-def-example';
const ncp = require('ncp').ncp;
// import defs from './example-def';
declare var __dirname: any;
function createDatabase(storageDef: StorageDef) {
let tableCreationQueries: string[] = [];
for (let i in storageDef.tables) {
tableCreationQueries.push(getTableCreationString(storageDef.tables[i]));
}
for (let i in storageDef.relations) {
tableCreationQueries.push(getTableRelationsCreationString(storageDef.relations[i]));
}
tableCreationQueries.map((query: string) => {
console.log(query);
});
}
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 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 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 : ''});`;
}
ncp(path.join(__dirname, 'frame'), path.join(__dirname, 'test'), (err: any) => {
if (err) {
console.log(err);
} else {
console.log('success copying files');
}
});
createDatabase(simmer.storage);
interface SystemDef {
storage: StorageDef;
views: ViewDef[];
// TODO: add Views, ACLs, Behaviors, UX
}
interface StorageDef {
tables: TableDef[];
relations: ManyToManyDef[];
}
interface ViewDef {
component: string;
type: ('list' | 'count' | 'item' | 'distinct')[];
columns: string[];
orderBy?: Order[];
filters?: Filter[];
// if type is 'list' it will always include skip and limit for pagination
}
interface Order {
column: string;
direction: 'asc' | 'desc';
}
interface Filter {
param: string; // the query param used to get the value
column: string;
comparison: '=' | '!=' | '>' | '<' | 'contains';
value?: string;
required?: boolean;
}
interface TableDef {
name: string;
columns: ColumnDef[],
relations: BelongsToDef[],
}
interface ColumnDef {
name: string;
type: "blob" | "boolean" | "date" | "dateTIME" | "number" | "string";
nullable: boolean;
unique?: boolean;
autoIncrement?: boolean;
default?: string;
}
interface BelongsToDef {
type: 'belongs-to';
table: string;
}
interface ManyToManyDef {
left: string;
relation: 'many-to-many';
right: string;
columns?: ColumnDef[];
}
export {
SystemDef
}