Skip to content

Instantly share code, notes, and snippets.

@brandonros
Created June 11, 2016 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonros/b58ce6f17520dbbe4f84ae860ab1aecc to your computer and use it in GitHub Desktop.
Save brandonros/b58ce6f17520dbbe4f84ae860ab1aecc to your computer and use it in GitHub Desktop.
var Schema = function (input) {
var output = {};
Object.keys(input).forEach(function (key) {
if (input[key] === Number) {
output[key] = 'REAL';
}
else if (input[key] === String || input[key] === 'TEXT') {
output[key] = 'TEXT';
}
else if (input[key] === Boolean) {
output[key] = 'BOOLEAN';
}
else if (input[key] === Date) {
output[key] = 'TIMESTAMP';
}
else if (Array.isArray(input[key]) || typeof input[key] === 'object' || input[key] === 'JSONB' || typeof input[key] === 'function') {
output[key] = 'JSONB';
}
});
this.index = function () {
};
this.toSql = function (name) {
var sql = '';
sql += 'CREATE TABLE IF NOT EXISTS ' + name + '(\n';
var keys = Object.keys(output);
keys.forEach(function (key, index) {
if (index !== keys.length -1) {
sql += '\t' + key + ' ' + output[key] + ',\n';
}
else {
sql += '\t' + key + ' ' + output[key] + '\n';
}
});
sql += ');';
return sql;
};
};
Schema.Types = {
ObjectId: 'TEXT',
Mixed: 'JSONB'
};
module.exports = {
Schema: Schema
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment