Created
November 27, 2015 19:16
-
-
Save oscaroceguera/56719e6dfc8604895cd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Bookshelf = require('../commons/bookshelf'); | |
var Schema = require('../schema/schema'); | |
var sequence = require('when/sequence'); | |
var _ = require('lodash'); | |
function createTable(tableName) { | |
return knex.schema.createTable(tableName, function (table) { | |
var column; | |
var columnKeys = _.keys(Schema[tableName]); | |
_.each(columnKeys, function (key) { | |
// creation distinguishes between text with fieldtype, string with maxlength and all others | |
if (Schema[tableName][key].type === 'text' && Schema[tableName][key].hasOwnProperty('fieldtype')) { | |
column = table[Schema[tableName][key].type](key, Schema[tableName][key].fieldtype); | |
} | |
else if (Schema[tableName][key].type === 'string' && Schema[tableName][key].hasOwnProperty('maxlength')) { | |
column = table[Schema[tableName][key].type](key, Schema[tableName][key].maxlength); | |
} | |
else { | |
column = table[Schema[tableName][key].type](key); | |
} | |
if (Schema[tableName][key].hasOwnProperty('nullable') && Schema[tableName][key].nullable === true) { | |
column.nullable(); | |
} | |
else { | |
column.notNullable(); | |
} | |
if (Schema[tableName][key].hasOwnProperty('primary') && Schema[tableName][key].primary === true) { | |
column.primary(); | |
} | |
if (Schema[tableName][key].hasOwnProperty('unique') && Schema[tableName][key].unique) { | |
column.unique(); | |
} | |
if (Schema[tableName][key].hasOwnProperty('unsigned') && Schema[tableName][key].unsigned) { | |
column.unsigned(); | |
} | |
if (Schema[tableName][key].hasOwnProperty('references')) { | |
//check if table exists? | |
column.references(Schema[tableName][key].references); | |
} | |
if (Schema[tableName][key].hasOwnProperty('defaultTo')) { | |
column.defaultTo(Schema[tableName][key].defaultTo); | |
} | |
}); | |
}); | |
} | |
function createTables () { | |
var tables = []; | |
var tableNames = _.keys(Schema); | |
console.log('Creating tables...'); | |
tables = _.map(tableNames, function (tableName) { | |
return function () { | |
return createTable(tableName); | |
}; | |
}); | |
return sequence(tables); | |
} | |
createTables() | |
.then(function() { | |
console.log('Tables created!!'); | |
process.exit(0); | |
}) | |
.otherwise(function (error) { | |
throw error; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment