2011-10-08 17:11:26 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
var mysql = require('mysql');
|
|
|
|
|
|
|
|
exports.initialize = function initializeSchema(schema, callback) {
|
|
|
|
var s = schema.settings;
|
|
|
|
schema.client = mysql.createClient({
|
|
|
|
host: s.host || 'localhost',
|
|
|
|
port: s.port || 3306,
|
2011-10-23 19:43:53 +00:00
|
|
|
user: s.username,
|
2011-10-08 17:11:26 +00:00
|
|
|
password: s.password,
|
|
|
|
database: s.database,
|
|
|
|
debug: s.debug
|
|
|
|
});
|
|
|
|
|
|
|
|
schema.adapter = new MySQL(schema.client);
|
2011-11-13 08:05:50 +00:00
|
|
|
process.nextTick(callback);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function MySQL(client) {
|
|
|
|
this._models = {};
|
|
|
|
this.client = client;
|
|
|
|
}
|
|
|
|
|
|
|
|
MySQL.prototype.define = function (descr) {
|
|
|
|
this._models[descr.model.modelName] = descr;
|
|
|
|
};
|
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
MySQL.prototype.defineProperty = function (model, prop, params) {
|
|
|
|
this._models[model].properties[prop] = params;
|
|
|
|
};
|
|
|
|
|
2011-11-11 13:16:09 +00:00
|
|
|
MySQL.prototype.query = function (sql, callback) {
|
|
|
|
var time = Date.now();
|
|
|
|
var log = this.log;
|
2011-12-09 15:23:29 +00:00
|
|
|
if (typeof callback !== 'function') throw new Error('callback should be a function');
|
2011-11-11 13:16:09 +00:00
|
|
|
this.client.query(sql, function (err, data) {
|
|
|
|
log(sql, time);
|
|
|
|
callback(err, data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
MySQL.prototype.save = function (model, data, callback) {
|
2011-10-23 19:43:53 +00:00
|
|
|
var sql = 'UPDATE ' + model + ' SET ' + this.toFields(model, data) +
|
|
|
|
' WHERE id = ' + data.id;
|
|
|
|
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err) {
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-10-23 19:43:53 +00:00
|
|
|
/**
|
|
|
|
* Must invoke callback(err, id)
|
|
|
|
*/
|
2011-10-08 17:11:26 +00:00
|
|
|
MySQL.prototype.create = function (model, data, callback) {
|
2011-10-23 19:43:53 +00:00
|
|
|
var fields = this.toFields(model, data);
|
|
|
|
var sql = 'INSERT ' + model;
|
|
|
|
if (fields) {
|
|
|
|
sql += ' SET ' + fields;
|
|
|
|
} else {
|
|
|
|
sql += ' VALUES ()';
|
|
|
|
}
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err, info) {
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err, info && info.insertId);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.toFields = function (model, data) {
|
|
|
|
var fields = [];
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
if (props[key]) {
|
2011-11-22 22:43:05 +00:00
|
|
|
fields.push('`' + key.replace(/\./g, '`.`') + '` = ' + this.toDatabase(props[key], data[key]));
|
2011-10-23 19:43:53 +00:00
|
|
|
}
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
2011-10-23 19:43:53 +00:00
|
|
|
return fields.join(',');
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-10-23 19:43:53 +00:00
|
|
|
MySQL.prototype.toDatabase = function (prop, val) {
|
|
|
|
if (prop.type.name === 'Number') return val;
|
|
|
|
if (val === null) return 'NULL';
|
|
|
|
if (prop.type.name === 'Date') {
|
2011-11-12 12:04:07 +00:00
|
|
|
if (!val) return 'NULL';
|
2011-10-23 19:43:53 +00:00
|
|
|
if (!val.toUTCString) {
|
|
|
|
val = new Date(val);
|
|
|
|
}
|
|
|
|
val = [
|
|
|
|
val.getFullYear(),
|
|
|
|
val.getMonth() + 1,
|
|
|
|
val.getDate(),
|
|
|
|
val.getHours(),
|
|
|
|
val.getMinutes(),
|
|
|
|
val.getSeconds()
|
|
|
|
].join('-');
|
|
|
|
return this.client.escape(val);
|
|
|
|
}
|
2011-11-19 17:16:37 +00:00
|
|
|
if (prop.type.name == "Boolean") return val ? 1 : 0;
|
2011-10-23 19:43:53 +00:00
|
|
|
return this.client.escape(val.toString());
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.fromDatabase = function (model, data) {
|
|
|
|
if (!data) return null;
|
|
|
|
var props = this._models[model].properties;
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
var val = data[key];
|
|
|
|
if (props[key]) {
|
|
|
|
// if (props[key])
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-10-23 19:43:53 +00:00
|
|
|
data[key] = val;
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.exists = function (model, id, callback) {
|
|
|
|
var sql = 'SELECT 1 FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err, data) {
|
2011-10-23 19:43:53 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
callback(null, data.length === 1);
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.find = function find(model, id, callback) {
|
2011-10-23 19:43:53 +00:00
|
|
|
var sql = 'SELECT * FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err, data) {
|
2011-10-23 19:43:53 +00:00
|
|
|
if (data && data.length === 1) {
|
|
|
|
data[0].id = id;
|
2011-10-08 17:11:26 +00:00
|
|
|
} else {
|
2011-10-23 19:43:53 +00:00
|
|
|
data = [null];
|
2011-10-08 17:11:26 +00:00
|
|
|
}
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err, this.fromDatabase(model, data[0]));
|
|
|
|
}.bind(this));
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.destroy = function destroy(model, id, callback) {
|
2011-10-23 19:43:53 +00:00
|
|
|
var sql = 'DELETE FROM ' + model + ' WHERE id = ' + id + ' LIMIT 1';
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query(sql, function (err) {
|
2011-10-08 17:11:26 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-04 07:30:25 +00:00
|
|
|
// TODO: hook up where, order, limit and offset conditions
|
2011-10-08 17:11:26 +00:00
|
|
|
MySQL.prototype.all = function all(model, filter, callback) {
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query('SELECT * FROM ' + model, function (err, data) {
|
2011-10-08 17:11:26 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err, []);
|
|
|
|
}
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err, filter ? data.filter(applyFilter(filter)) : data);
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
function applyFilter(filter) {
|
2011-11-04 07:30:25 +00:00
|
|
|
if (typeof filter.where === 'function') {
|
2011-10-08 17:11:26 +00:00
|
|
|
return filter;
|
|
|
|
}
|
2011-11-04 07:30:25 +00:00
|
|
|
var keys = Object.keys(filter.where);
|
2011-10-08 17:11:26 +00:00
|
|
|
return function (obj) {
|
|
|
|
var pass = true;
|
|
|
|
keys.forEach(function (key) {
|
2011-11-04 07:30:25 +00:00
|
|
|
if (!test(filter.where[key], obj[key])) {
|
2011-10-08 17:11:26 +00:00
|
|
|
pass = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
function test(example, value) {
|
|
|
|
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
|
|
|
|
return value.match(example);
|
|
|
|
}
|
|
|
|
// not strict equality
|
|
|
|
return example == value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MySQL.prototype.destroyAll = function destroyAll(model, callback) {
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query('DELETE FROM ' + model, function (err) {
|
2011-10-08 17:11:26 +00:00
|
|
|
if (err) {
|
|
|
|
return callback(err, []);
|
|
|
|
}
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err);
|
2011-10-08 17:11:26 +00:00
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.count = function count(model, callback) {
|
2011-11-11 13:16:09 +00:00
|
|
|
this.query('SELECT count(*) as cnt FROM ' + model, function (err, res) {
|
2011-10-23 19:43:53 +00:00
|
|
|
callback(err, err ? null : res[0].cnt);
|
2011-10-08 17:11:26 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
|
2011-10-23 19:43:53 +00:00
|
|
|
data.id = id;
|
|
|
|
this.save(model, data, cb);
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2011-11-11 13:16:09 +00:00
|
|
|
MySQL.prototype.disconnect = function disconnect() {
|
|
|
|
this.client.end();
|
|
|
|
};
|
|
|
|
|
2011-12-09 15:23:29 +00:00
|
|
|
MySQL.prototype.automigrate = function (cb) {
|
|
|
|
var self = this;
|
|
|
|
var wait = 0;
|
|
|
|
Object.keys(this._models).forEach(function (model) {
|
|
|
|
wait += 1;
|
|
|
|
self.dropTable(model, function () {
|
|
|
|
self.createTable(model, function (err) {
|
|
|
|
if (err) console.log(err);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done() {
|
|
|
|
if (--wait === 0 && cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.autoupdate = function (cb) {
|
|
|
|
var self = this;
|
|
|
|
var wait = 0;
|
|
|
|
Object.keys(this._models).forEach(function (model) {
|
|
|
|
wait += 1;
|
|
|
|
self.query('SHOW FIELDS FROM ' + model, function (err, fields) {
|
|
|
|
self.alterTable(model, fields, done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
if (--wait === 0 && cb) {
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.alterTable = function (model, actualFields, done) {
|
|
|
|
var self = this;
|
|
|
|
var m = this._models[model];
|
|
|
|
var propNames = Object.keys(m.properties);
|
|
|
|
var sql = [];
|
|
|
|
actualFields.forEach(function (f) {
|
|
|
|
if (f.Field !== 'id') {
|
|
|
|
actualize(f.Field, f);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (sql.length) {
|
|
|
|
this.query('ALTER TABLE `' + model + '` ' + sql.join(',\n'), done);
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
function actualize(propName, oldSettings) {
|
|
|
|
var newSettings = m.properties[propName];
|
|
|
|
if (!newSettings) {
|
|
|
|
sql.push('ADD COLUMN `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
|
|
|
} else if (changed(newSettings, oldSettings)) {
|
|
|
|
sql.push('CHANGE COLUMN `' + propName + '` `' + propName + '` ' + self.propertySettingsSQL(model, propName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function changed(newSettings, oldSettings) {
|
|
|
|
if (oldSettings.Null === 'YES' && (newSettings.allowNull === false || newSettings.null === false)) return true;
|
|
|
|
if (oldSettings.Null === 'NO' && !(newSettings.allowNull === false || newSettings.null === false)) return true;
|
|
|
|
if (oldSettings.Type.toUpperCase() !== datatype(newSettings)) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.dropTable = function (model, cb) {
|
|
|
|
this.query('DROP TABLE IF EXISTS ' + model, cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.createTable = function (model, cb) {
|
|
|
|
this.query('CREATE TABLE ' + model +
|
|
|
|
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.propertiesSQL = function (model) {
|
|
|
|
var self = this;
|
|
|
|
var sql = ['`id` INT(11) NOT NULL AUTO_INCREMENT UNIQUE PRIMARY KEY'];
|
|
|
|
Object.keys(this._models[model].properties).forEach(function (prop) {
|
|
|
|
sql.push('`' + prop + '` ' + self.propertySettingsSQL(model, prop));
|
|
|
|
});
|
|
|
|
return sql.join(',\n ');
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
MySQL.prototype.propertySettingsSQL = function (model, prop) {
|
|
|
|
var p = this._models[model].properties[prop];
|
|
|
|
return datatype(p) + ' ' +
|
|
|
|
(p.allowNull === false || p['null'] === false ? 'NOT NULL' : 'NULL') +
|
|
|
|
''; // (p.index ? ' KEY ix' + model + '_' + prop : '');
|
|
|
|
};
|
|
|
|
|
|
|
|
function datatype(p) {
|
|
|
|
switch (p.type.name) {
|
|
|
|
case 'String':
|
|
|
|
return 'VARCHAR(' + (p.limit || 255) + ')';
|
|
|
|
case 'Text':
|
|
|
|
return 'TEXT';
|
|
|
|
case 'Number':
|
|
|
|
return 'INT(11)';
|
|
|
|
case 'Date':
|
|
|
|
return 'DATETIME';
|
|
|
|
case 'Boolean':
|
|
|
|
return 'TINYINT(1)';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|