initial commit
This commit is contained in:
3
dist/example-def.d.ts
vendored
Normal file
3
dist/example-def.d.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
import { SystemDef } from './processDef';
|
||||
declare let def: SystemDef;
|
||||
export default def;
|
88
dist/example-def.js
vendored
Normal file
88
dist/example-def.js
vendored
Normal file
@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var def = {
|
||||
storage: {
|
||||
tables: [
|
||||
{
|
||||
name: "tasks",
|
||||
relations: [
|
||||
{
|
||||
type: 'belongs-to',
|
||||
table: 'lists',
|
||||
}
|
||||
],
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
type: "string",
|
||||
nullable: false,
|
||||
},
|
||||
{
|
||||
name: "description",
|
||||
type: "string",
|
||||
nullable: true,
|
||||
},
|
||||
{
|
||||
name: "completed",
|
||||
type: "boolean",
|
||||
nullable: false,
|
||||
},
|
||||
{
|
||||
name: "completed_date",
|
||||
type: "date",
|
||||
nullable: true,
|
||||
},
|
||||
{
|
||||
name: "metadata",
|
||||
type: "blob",
|
||||
nullable: true,
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "lists",
|
||||
relations: [],
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
type: "string",
|
||||
unique: false,
|
||||
nullable: false,
|
||||
},
|
||||
]
|
||||
},
|
||||
{
|
||||
name: "users",
|
||||
relations: [],
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
type: "string",
|
||||
nullable: false,
|
||||
},
|
||||
{
|
||||
name: "password",
|
||||
type: "string",
|
||||
nullable: false,
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
relations: [
|
||||
{
|
||||
left: 'lists',
|
||||
relation: 'many-to-many',
|
||||
right: 'users',
|
||||
columns: [
|
||||
{
|
||||
name: 'access',
|
||||
type: 'string',
|
||||
unique: false,
|
||||
nullable: false,
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
exports.default = def;
|
31
dist/processDef.d.ts
vendored
Normal file
31
dist/processDef.d.ts
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
interface SystemDef {
|
||||
storage: StorageDef;
|
||||
}
|
||||
interface StorageDef {
|
||||
tables: TableDef[];
|
||||
relations: ManyToManyDef[];
|
||||
}
|
||||
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 };
|
60
dist/processDef.js
vendored
Normal file
60
dist/processDef.js
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var example_def_1 = __importDefault(require("./example-def"));
|
||||
function createDatabase(storageDef) {
|
||||
var tableCreationQueries = [];
|
||||
for (var i in storageDef.tables) {
|
||||
tableCreationQueries.push(getTableCreationString(storageDef.tables[i]));
|
||||
}
|
||||
for (var i in storageDef.relations) {
|
||||
tableCreationQueries.push(getTableRelationsCreationString(storageDef.relations[i]));
|
||||
}
|
||||
tableCreationQueries.map(function (query) {
|
||||
console.log(query);
|
||||
});
|
||||
}
|
||||
function getTableCreationString(tableDef) {
|
||||
var primaryKeyString = tableDef.name + "_id VARCHAR(36) PRIMARY KEY";
|
||||
var columnString = "" + primaryKeyString;
|
||||
var defaultColumns = "created DATETIME NOT NULL DEFAULT NOW(), modified DATETIME NOT NULL DEFAULT NOW()";
|
||||
var indexString = "";
|
||||
// columns
|
||||
for (var i in tableDef.columns) {
|
||||
var column = tableDef.columns[i];
|
||||
columnString = columnString + ", " + createColumnString(column);
|
||||
}
|
||||
// relations
|
||||
for (var i in tableDef.relations) {
|
||||
var relation = 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) {
|
||||
var typeMap = {
|
||||
"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) {
|
||||
var columnString = relationsDef.left + "_id VARCHAR(36) NOT NULL, " + relationsDef.right + "_id VARCHAR(36) NOT NULL";
|
||||
var indexString = "INDEX " + relationsDef.left + "_id_index (" + relationsDef.left + "_id), INDEX " + relationsDef.right + "_id_index (" + relationsDef.right + "_id)";
|
||||
if (relationsDef.columns && relationsDef.columns.length) {
|
||||
for (var i in relationsDef.columns) {
|
||||
var column = relationsDef.columns[i];
|
||||
columnString = columnString + ", " + createColumnString(column);
|
||||
}
|
||||
}
|
||||
return "CREATE TABLE IF NOT EXISTS " + relationsDef.left + "_" + relationsDef.right + " (" + columnString + (indexString !== '' ? ', ' + indexString : '') + ");";
|
||||
}
|
||||
createDatabase(example_def_1.default.storage);
|
Reference in New Issue
Block a user