coal
Version:
Mysql ORM base on knexjs
58 lines (46 loc) • 1.33 kB
JavaScript
(function() {
var Model, _path, _table_struct;
_path = require('path');
_table_struct = require('./table-struct');
Model = (function() {
function Model(tableName, connection, developer) {
this.tableName = tableName;
this.connection = connection;
this.developer = developer;
this.schema = _table_struct[this.tableName];
}
Model.prototype.save = function(data) {
var sql, tab;
tab = this.table();
if (this.schema.auto_create_at && !data.create_at) {
data.create_at = Date.now();
}
sql = tab.insert(data);
if (this.developer) {
console.log(sql.toString());
}
return sql;
};
Model.prototype.update = function(data) {
var tab;
tab = this.table();
if (this.schema.auto_update_at && !data.update_at) {
data.update_at = Date.now();
}
return tab.update(data);
};
Model.prototype.sql = function(sql, fields) {
var pro;
pro = !fields ? this.connection.raw(sql) : this.connection.raw(sql, fields);
if (this.developer) {
console.log(sql.toString());
}
return pro;
};
Model.prototype.table = function() {
return this.connection(this.tableName);
};
return Model;
})();
module.exports = Model;
}).call(this);