Created
June 11, 2016 01:34
-
-
Save brandonros/b58ce6f17520dbbe4f84ae860ab1aecc 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 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