working server with migrations as defined
BOOM BABY!!!
This commit is contained in:
36
dist/processDef.js
vendored
36
dist/processDef.js
vendored
@ -23,19 +23,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var path = __importStar(require("path"));
|
||||
var simmer_def_example_1 = __importDefault(require("./simmer-def-example"));
|
||||
// import simmer from './simmer-def-example';
|
||||
var ncp = require('ncp').ncp;
|
||||
var fs = __importStar(require("fs"));
|
||||
var example_def_1 = __importDefault(require("./example-def"));
|
||||
var outDir = 'test';
|
||||
function createDatabase(storageDef) {
|
||||
var tableCreationQueries = [];
|
||||
var tableDeletionQueries = [];
|
||||
for (var i in storageDef.tables) {
|
||||
tableCreationQueries.push(getTableCreationString(storageDef.tables[i]));
|
||||
tableDeletionQueries.push(getTableDeletionString(storageDef.tables[i]));
|
||||
}
|
||||
for (var i in storageDef.relations) {
|
||||
tableCreationQueries.push(getTableRelationsCreationString(storageDef.relations[i]));
|
||||
tableDeletionQueries.push(getTableRelationsDeletionString(storageDef.relations[i]));
|
||||
}
|
||||
tableCreationQueries.map(function (query) {
|
||||
console.log(query);
|
||||
});
|
||||
tableDeletionQueries.map(function (query) {
|
||||
console.log(query);
|
||||
});
|
||||
return {
|
||||
up: tableCreationQueries,
|
||||
down: tableDeletionQueries,
|
||||
};
|
||||
}
|
||||
function getTableCreationString(tableDef) {
|
||||
var primaryKeyString = tableDef.name + "_id VARCHAR(36) PRIMARY KEY";
|
||||
@ -56,6 +69,9 @@ function getTableCreationString(tableDef) {
|
||||
columnString = columnString + ", " + defaultColumns;
|
||||
return "CREATE TABLE IF NOT EXISTS " + tableDef.name + " (" + columnString + (indexString !== '' ? ', ' + indexString : '') + ");";
|
||||
}
|
||||
function getTableDeletionString(tableDef) {
|
||||
return "DROP TABLE IF EXISTS " + tableDef.name + ";";
|
||||
}
|
||||
function createColumnString(column) {
|
||||
var typeMap = {
|
||||
"blob": "BLOB",
|
||||
@ -78,12 +94,26 @@ function getTableRelationsCreationString(relationsDef) {
|
||||
}
|
||||
return "CREATE TABLE IF NOT EXISTS " + relationsDef.left + "_" + relationsDef.right + " (" + columnString + (indexString !== '' ? ', ' + indexString : '') + ");";
|
||||
}
|
||||
ncp(path(__dirname, 'frame'), path(__dirname, 'test'), function (err) {
|
||||
function getTableRelationsDeletionString(relationsDef) {
|
||||
return "DROP TABLE IF EXISTS " + relationsDef.left + "_" + relationsDef.right + ";";
|
||||
}
|
||||
function dateString() {
|
||||
var date = new Date();
|
||||
return "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate();
|
||||
}
|
||||
function writeMigrationsToFile(migrations) {
|
||||
var migrationFileContents = "\n--up\n" + migrations.up.join('\n') + "\n--down\n" + migrations.down.join('\n') + "\n ";
|
||||
var migrationFilePath = path.join(__dirname, outDir, 'src', 'migrationJobs', dateString() + "-create-database.sql");
|
||||
fs.writeFileSync(migrationFilePath, migrationFileContents);
|
||||
}
|
||||
ncp(path.join(__dirname, 'frame'), path.join(__dirname, outDir), function (err) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
else {
|
||||
console.log('success copying files');
|
||||
var creationMigrations = createDatabase(example_def_1.default.storage);
|
||||
// let creationMigrations = createDatabase(simmer.storage);
|
||||
writeMigrationsToFile(creationMigrations);
|
||||
}
|
||||
});
|
||||
createDatabase(simmer_def_example_1.default.storage);
|
||||
|
@ -4,7 +4,7 @@
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "yarn build && node --inspect ./build/bin/www",
|
||||
"typescript": "tsc && cp -R ./src/views ./build/ && cp -R ./src/public ./build/",
|
||||
"typescript": "tsc && cp -R ./src/views ./build/ && cp -R ./src/public ./build/ && cp ./src/migrationJobs/*.sql ./build/migrationJobs/",
|
||||
"build": "yarn typescript",
|
||||
"dist": "yarn build && cp -R ./build ./dist/ && cp -R ./node_modules ./dist/"
|
||||
},
|
||||
@ -23,6 +23,7 @@
|
||||
"morgan": "~1.9.1",
|
||||
"passport": "^0.4.1",
|
||||
"passport-oauth2": "^1.5.0",
|
||||
"passport-oauth2-refresh": "^2.0.0"
|
||||
"passport-oauth2-refresh": "^2.0.0",
|
||||
"path": "^0.12.7"
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ var strategy = new OAuth2Strategy({
|
||||
},
|
||||
function(accessToken, refreshToken, params, _, cb) {
|
||||
console.log(params);
|
||||
userService.findOrCreate(params.profile).then((user) => {
|
||||
userService.findOrCreate(params.profile).then((user: any) => {
|
||||
user.refreshToken = refreshToken;
|
||||
user.accessToken = accessToken;
|
||||
return cb(null, user);
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { Database } from './db/db';
|
||||
import { Mapper } from './db/Mapper';
|
||||
import { Altitude, Job } from './db/migrations/Altitude';
|
||||
import { Altitude, Job } from './db/Altitude';
|
||||
import jobs from './migrationJobs';
|
||||
|
||||
let db = new Database({
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Mapper } from '../Mapper';
|
||||
import { Mapper } from './Mapper';
|
||||
|
||||
class Altitude {
|
||||
private migrations: Job[];
|
@ -1,4 +1,4 @@
|
||||
import { Database } from '../db/db';
|
||||
import { Database } from './db';
|
||||
|
||||
class Mapper {
|
||||
constructor(public database: Database) {}
|
||||
|
@ -1,2 +1,2 @@
|
||||
import { config } from 'dotenv';
|
||||
config({ silent: true })
|
||||
config();
|
||||
|
@ -1,11 +1,29 @@
|
||||
import { Job } from '../db/migrations/Altitude';
|
||||
import { Job } from '../db/Altitude';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
let jobs: Job[] = [];
|
||||
|
||||
let files = fs.readdirSync(path.join(__dirname)).filter(file => file !== 'index.js');
|
||||
for (let i in files) {
|
||||
try {
|
||||
var data = fs.readFileSync(path.join(__dirname, files[i]), 'utf8');
|
||||
let parts = data.split('--down').filter(part => part.indexOf('--up') !== -1);
|
||||
if (parts.length) {
|
||||
// there is an up command in this file
|
||||
let upParts = parts[0].split('--up');
|
||||
jobs.push({
|
||||
creator: 'Mason Payne',
|
||||
name: 'initializeDB',
|
||||
query: `CREATE TABLE Users (sub VARCHAR(36) NOT NULL UNIQUE, email VARCHAR(255) NOT NULL UNIQUE, accessToken VARCHAR(255), refreshToken VARCHAR(255), CreationTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (sub));`
|
||||
creator: 'system',
|
||||
name: files[i],
|
||||
query: upParts[1].split('\n').join('')
|
||||
});
|
||||
}
|
||||
} catch(e) {
|
||||
console.log('Error:', e.stack);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(jobs);
|
||||
|
||||
// SYSTEM-BUILDER-jobs.push
|
||||
|
||||
|
@ -534,6 +534,14 @@ path-to-regexp@0.1.7:
|
||||
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
|
||||
integrity sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=
|
||||
|
||||
path@^0.12.7:
|
||||
version "0.12.7"
|
||||
resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f"
|
||||
integrity sha1-1NwqUGxM4hl+tIHr/NWzbAFAsQ8=
|
||||
dependencies:
|
||||
process "^0.11.1"
|
||||
util "^0.10.3"
|
||||
|
||||
pause@0.0.1:
|
||||
version "0.0.1"
|
||||
resolved "https://registry.yarnpkg.com/pause/-/pause-0.0.1.tgz#1d408b3fdb76923b9543d96fb4c9dfd535d9cb5d"
|
||||
@ -544,6 +552,11 @@ process-nextick-args@~2.0.0:
|
||||
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
|
||||
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==
|
||||
|
||||
process@^0.11.1:
|
||||
version "0.11.10"
|
||||
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
|
||||
integrity sha1-czIwDoQBYb2j5podHZGn1LwW8YI=
|
||||
|
||||
proxy-addr@~2.0.4:
|
||||
version "2.0.6"
|
||||
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf"
|
||||
@ -701,6 +714,13 @@ util-deprecate@~1.0.1:
|
||||
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
|
||||
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=
|
||||
|
||||
util@^0.10.3:
|
||||
version "0.10.4"
|
||||
resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901"
|
||||
integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==
|
||||
dependencies:
|
||||
inherits "2.0.3"
|
||||
|
||||
utils-merge@1.0.1, utils-merge@1.x.x:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
|
||||
|
@ -4,6 +4,9 @@
|
||||
"ncp": "^2.0.0",
|
||||
"path": "^0.12.7"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "rm -rf dist && tsc && cp -r frame dist/ && node dist/processDef.js"
|
||||
},
|
||||
"name": "system-builder",
|
||||
"version": "1.0.0",
|
||||
"main": "dist/processDef.js",
|
||||
|
@ -1,21 +1,36 @@
|
||||
import * as path from 'path';
|
||||
import simmer from './simmer-def-example';
|
||||
// import simmer from './simmer-def-example';
|
||||
const ncp = require('ncp').ncp;
|
||||
// import defs from './example-def';
|
||||
import * as fs from 'fs';
|
||||
import defs from './example-def';
|
||||
|
||||
declare var __dirname: any;
|
||||
|
||||
let outDir = 'test';
|
||||
|
||||
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 {
|
||||
@ -42,6 +57,10 @@ function getTableCreationString(tableDef: TableDef): string {
|
||||
return `CREATE TABLE IF NOT EXISTS ${tableDef.name} (${columnString}${indexString !== '' ? ', ' + indexString : ''});`;
|
||||
}
|
||||
|
||||
function getTableDeletionString(tableDef: TableDef): string {
|
||||
return `DROP TABLE IF EXISTS ${tableDef.name};`
|
||||
}
|
||||
|
||||
function createColumnString(column: ColumnDef) {
|
||||
const typeMap: {[type: string]: string} = {
|
||||
"blob": "BLOB",
|
||||
@ -69,16 +88,38 @@ function getTableRelationsCreationString(relationsDef: ManyToManyDef): string {
|
||||
return `CREATE TABLE IF NOT EXISTS ${relationsDef.left}_${relationsDef.right} (${columnString}${indexString !== '' ? ', ' + indexString : ''});`;
|
||||
}
|
||||
|
||||
function getTableRelationsDeletionString(relationsDef: ManyToManyDef) : string {
|
||||
return `DROP TABLE IF EXISTS ${relationsDef.left}_${relationsDef.right};`
|
||||
}
|
||||
|
||||
ncp(path.join(__dirname, 'frame'), path.join(__dirname, 'test'), (err: any) => {
|
||||
function dateString() {
|
||||
let date = new Date();
|
||||
return `${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`;
|
||||
}
|
||||
|
||||
function writeMigrationsToFile(migrations: {up: string[], down: string[]}) {
|
||||
let migrationFileContents = `
|
||||
--up
|
||||
${migrations.up.join('\n')}
|
||||
--down
|
||||
${migrations.down.join('\n')}
|
||||
`;
|
||||
let migrationFilePath = path.join(__dirname, outDir, 'src', 'migrationJobs', `${dateString()}-create-database.sql`);
|
||||
fs.writeFileSync(migrationFilePath, migrationFileContents);
|
||||
}
|
||||
|
||||
|
||||
ncp(path.join(__dirname, 'frame'), path.join(__dirname, outDir), (err: any) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log('success copying files');
|
||||
let creationMigrations = createDatabase(defs.storage);
|
||||
// let creationMigrations = createDatabase(simmer.storage);
|
||||
writeMigrationsToFile(creationMigrations);
|
||||
}
|
||||
});
|
||||
|
||||
createDatabase(simmer.storage);
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user