coal
Version:
Mysql ORM base on knexjs
73 lines (55 loc) • 1.78 kB
JavaScript
(function() {
var Coal, _Model, _Schema, _knex, _path, _sload, _table_struct;
_path = require('path');
_knex = require('knex');
_sload = require('sload');
_Schema = require('./schema');
_Model = require('./model');
_table_struct = require('./table-struct');
Coal = (function() {
function Coal(config1, updateSchema) {
this.config = config1;
if (updateSchema == null) {
updateSchema = false;
}
this.initConnection(this.config.database);
if (updateSchema) {
this.prepareSchema();
}
this.prepareTableStruct();
}
Coal.prototype.initConnection = function(config) {
return this.dbConnection = _knex(config);
};
Coal.prototype.prepareSchema = function() {
var schemaDir;
schemaDir = this.config.schema;
return this.tableSchema = new _Schema(this.dbConnection, schemaDir);
};
Coal.prototype.prepareTableStruct = function() {
var i, len, results, schema, schemaDir, schemas;
schemaDir = this.config.schema;
schemas = _sload.scan(schemaDir);
results = [];
for (i = 0, len = schemas.length; i < len; i++) {
schema = schemas[i];
results.push(_table_struct[schema.name] = schema);
}
return results;
};
Coal.prototype.getConnection = function() {
return this.dbConnection;
};
Coal.prototype.Model = function(tabName, developer) {
if (developer == null) {
developer = false;
}
return new _Model(tabName, this.dbConnection, developer);
};
Coal.prototype.clearFields = function(tabName, beClearData) {
return this.tableSchema.clearSchema(tabName, beClearData);
};
return Coal;
})();
module.exports = Coal;
}).call(this);