Created
May 24, 2015 19:52
-
-
Save zone117x/160e02c9a8794bfbfd69 to your computer and use it in GitHub Desktop.
knex
This file contains 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 db = knex({ | |
client: 'pg', | |
connection: { | |
host: 'localhost', | |
port: 5432, | |
user: 'postgres', | |
password: 'test', | |
database: 'syndicate' | |
}, | |
debug: true | |
}); | |
// for each column in an object, create a promise to check if column exists and add column if not exists | |
function createColumnPromises(tableName, table, obj) { | |
// flatten the nested series object to the db model representation | |
var flattened = dottie.flatten(obj, '.'); | |
var properties = Object.keys(flattened); | |
// for each property.. | |
return Promise.map(properties, function (property) { | |
// if property does not have column.. | |
return db.schema.hasColumn(tableName, property).then(function (exists) { | |
if (exists) | |
return; | |
var val = flattened[property]; | |
var propType = util.getType(val); | |
var typeHint = obj.constructor.prototype.typeHints[property]; | |
var attributeHints = obj.constructor.prototype.attributeHints[property] || {}; | |
var tableChain; | |
// figure out the correct column type for this property | |
switch (typeHint || propType) { | |
case 'string': | |
tableChain = table.string(property); | |
break; | |
case 'boolean': | |
tableChain = table.boolean(property); | |
break; | |
case 'number': | |
if (attributeHints.primaryKey) { | |
tableChain = table.increments(property).primary(); | |
} | |
else { | |
tableChain = table.integer(property); | |
} | |
break; | |
case 'float': | |
tableChain = table.float(property); | |
break; | |
case Date: | |
tableChain = table.timestamp(property); | |
break; | |
case Array: | |
tableChain = table.specificType(property, "text[]"); | |
break; | |
default: | |
var typeName = typeof (propType) === "string" ? propType : propType.name; | |
throw { | |
name: "Sql column mapping failed", | |
message: "Cannot create column with unknown property type of: " + typeName | |
}; | |
break; | |
} | |
}).then(function (f) { | |
console.log("done with property " + property); | |
}).catch(function (err) { | |
console.log("error with property " + property + ", " + err); | |
}); | |
}); | |
} | |
db.schema.hasTable("series").then(function (exists) { | |
if (!exists) { | |
return db.schema.createTable('series', function (table) { | |
//table.increments("seriesId").primary(); | |
//table.string("showName"); | |
}).catch(function (err) { | |
console.log(err); | |
}); | |
} | |
}).then(function (f) { | |
return db.schema.table('series', function (table) { | |
var f = createColumnPromises('series', table, Show.empty()); | |
return f; | |
}).then(function () { | |
console.log("finished mapping table series"); | |
}); | |
}).then(function (f) { | |
console.log('done'); | |
}).catch(function (err) { | |
console.log(err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment