loopback-datasource-juggler/lib/sql.js

393 lines
10 KiB
JavaScript
Raw Normal View History

var util = require('util');
var async = require('async');
var assert = require('assert');
var Connector = require('./connector');
2012-03-10 08:40:00 +00:00
module.exports = BaseSQL;
2012-03-27 14:22:24 +00:00
/**
2013-08-09 22:16:32 +00:00
* Base class for connectors that are backed by relational databases/SQL
2014-03-12 23:28:46 +00:00
* @class
2012-03-27 14:22:24 +00:00
*/
function BaseSQL() {
2014-01-24 17:09:53 +00:00
Connector.apply(this, [].slice.call(arguments));
2012-03-27 14:22:24 +00:00
}
2012-03-10 08:40:00 +00:00
util.inherits(BaseSQL, Connector);
2013-08-09 22:16:32 +00:00
/**
* Set the relational property to indicate the backend is a relational DB
* @type {boolean}
*/
2013-07-15 17:38:54 +00:00
BaseSQL.prototype.relational = true;
/**
* Get types associated with the connector
2014-03-12 23:28:46 +00:00
* Returns {String[]} The types for the connector
*/
BaseSQL.prototype.getTypes = function() {
return ['db', 'rdbms', 'sql'];
};
/*!
* Get the default data type for ID
2014-03-12 23:28:46 +00:00
* Returns {Function}
*/
BaseSQL.prototype.getDefaultIdType = function() {
return Number;
};
2012-03-10 10:32:14 +00:00
BaseSQL.prototype.query = function () {
2014-01-24 17:09:53 +00:00
throw new Error('query method should be declared in connector');
2012-03-10 10:32:14 +00:00
};
2013-08-09 22:16:32 +00:00
BaseSQL.prototype.command = function (sql, params, callback) {
2014-01-24 17:09:53 +00:00
return this.query(sql, params, callback);
2012-03-10 10:32:14 +00:00
};
BaseSQL.prototype.queryOne = function (sql, callback) {
2014-01-24 17:09:53 +00:00
return this.query(sql, function (err, data) {
if (err) {
return callback(err);
}
callback(err, data && data[0]);
2014-01-24 17:09:53 +00:00
});
2012-03-10 10:32:14 +00:00
};
2013-05-24 05:40:33 +00:00
/**
2014-03-12 23:28:46 +00:00
* Get the table name for a given model.
* Returns the table name (String).
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
2013-05-24 05:40:33 +00:00
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.table = function (model) {
2014-01-24 17:09:53 +00:00
var name = this.getDataSource(model).tableName(model);
var dbName = this.dbName;
if (typeof dbName === 'function') {
name = dbName(name);
}
return name;
2012-03-10 08:40:00 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the column name for given model property
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
* @param {String} property The property name
2013-05-24 05:40:33 +00:00
* @returns {String} The column name
*/
BaseSQL.prototype.column = function (model, property) {
2014-01-24 17:09:53 +00:00
var name = this.getDataSource(model).columnName(model, property);
var dbName = this.dbName;
if (typeof dbName === 'function') {
name = dbName(name);
}
return name;
2013-05-23 01:04:05 +00:00
};
2013-05-29 17:03:01 +00:00
/**
* Get the column name for given model property
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
* @param {String} property The property name
2013-05-29 17:03:01 +00:00
* @returns {Object} The column metadata
*/
BaseSQL.prototype.columnMetadata = function (model, property) {
2014-01-24 17:09:53 +00:00
return this.getDataSource(model).columnMetadata(model, property);
2013-05-29 17:03:01 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the corresponding property name for a given column name
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
* @param {String} column The column name
* @returns {String} The property name for a given column
2013-05-24 05:40:33 +00:00
*/
BaseSQL.prototype.propertyName = function (model, column) {
2014-01-24 17:09:53 +00:00
var props = this._models[model].properties;
for (var p in props) {
if (this.column(model, p) === column) {
return p;
}
2014-01-24 17:09:53 +00:00
}
return null;
};
2013-05-24 05:40:33 +00:00
/**
* Get the id column name
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
2013-05-24 05:40:33 +00:00
* @returns {String} The column name
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.idColumn = function (model) {
2014-01-24 17:09:53 +00:00
var name = this.getDataSource(model).idColumnName(model);
var dbName = this.dbName;
if (typeof dbName === 'function') {
name = dbName(name);
}
return name;
2013-05-23 01:04:05 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped id column name
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
2013-05-24 05:40:33 +00:00
* @returns {String} the escaped id column name
*/
2013-05-23 01:04:05 +00:00
BaseSQL.prototype.idColumnEscaped = function (model) {
2014-01-24 17:09:53 +00:00
return this.escapeName(this.getDataSource(model).idColumnName(model));
2013-05-23 01:04:05 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Escape the name for the underlying database
2013-08-09 22:16:32 +00:00
* @param {String} name The name
2013-05-24 05:40:33 +00:00
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.escapeName = function (name) {
2014-01-24 17:09:53 +00:00
throw new Error('escapeName method should be declared in connector');
2012-03-10 08:40:00 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped table name
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
2013-05-24 05:40:33 +00:00
* @returns {String} the escaped table name
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.tableEscaped = function (model) {
2014-01-24 17:09:53 +00:00
return this.escapeName(this.table(model));
2012-03-10 08:40:00 +00:00
};
2013-05-24 05:40:33 +00:00
/**
* Get the escaped column name for a given model property
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
* @param {String} property The property name
2013-05-24 05:40:33 +00:00
* @returns {String} The escaped column name
*/
BaseSQL.prototype.columnEscaped = function (model, property) {
2014-01-24 17:09:53 +00:00
return this.escapeName(this.column(model, property));
};
function isIdValuePresent(idValue, callback, returningNull) {
try {
assert(idValue !== null && idValue !== undefined, 'id value is required');
return true;
} catch (err) {
process.nextTick(function () {
callback && callback(returningNull ? null: err);
});
return false;
}
}
2013-08-09 22:16:32 +00:00
/**
* Save the model instance into the backend store
* @param {String} model The model name
* @param {Object} data The model instance data
* @param {Function} callback The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.save = function (model, data, callback) {
var idName = this.getDataSource(model).idName(model);
var idValue = data[idName];
if (!isIdValuePresent(idValue, callback)) {
return;
}
idValue = this._escapeIdValue(model, idValue);
2014-01-24 17:09:53 +00:00
var sql = 'UPDATE ' + this.tableEscaped(model) + ' SET '
+ this.toFields(model, data) + ' WHERE ' + this.idColumnEscaped(model) + ' = '
+ idValue;
2012-03-10 08:40:00 +00:00
this.query(sql, function (err, result) {
callback && callback(err, result);
2014-01-24 17:09:53 +00:00
});
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Check if a model instance exists for the given id value
* @param {String} model The model name
* @param {*} id The id value
* @param {Function} callback The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.exists = function (model, id, callback) {
if (!isIdValuePresent(id, callback, true)) {
return;
}
2014-01-24 17:09:53 +00:00
var sql = 'SELECT 1 FROM ' +
this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = '
+ this._escapeIdValue(model, id) + ' LIMIT 1';
2012-03-10 08:40:00 +00:00
2014-01-24 17:09:53 +00:00
this.query(sql, function (err, data) {
if (err) {
return callback && callback(err);
}
callback && callback(null, data.length >= 1);
2014-01-24 17:09:53 +00:00
});
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Find a model instance by id
* @param {String} model The model name
* @param {*} id The id value
* @param {Function} callback The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.find = function find(model, id, callback) {
if (!isIdValuePresent(id, callback, true)) {
return;
}
var self = this;
var idQuery = this.idColumnEscaped(model) + ' = ' + this._escapeIdValue(model, id);
2014-01-24 17:09:53 +00:00
var sql = 'SELECT * FROM ' +
2014-03-13 23:43:38 +00:00
this.tableEscaped(model) + ' WHERE ' + idQuery + ' LIMIT 1';
2014-01-24 17:09:53 +00:00
this.query(sql, function (err, data) {
var result = (data && data.length >= 1) ? data[0] : null;
callback && callback(err, self.fromDatabase(model, result));
});
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Delete a model instance by id value
* @param {String} model The model name
* @param {*} id The id value
* @param {Function} callback The callback function
*/
2013-06-26 03:36:00 +00:00
BaseSQL.prototype.delete = BaseSQL.prototype.destroy = function destroy(model, id, callback) {
if (!isIdValuePresent(id, callback, true)) {
return;
}
2014-01-24 17:09:53 +00:00
var sql = 'DELETE FROM ' +
this.tableEscaped(model) + ' WHERE ' + this.idColumnEscaped(model) + ' = '
+ this._escapeIdValue(model, id);
2012-03-10 08:40:00 +00:00
this.command(sql, function (err, result) {
callback && callback(err, result);
2014-01-24 17:09:53 +00:00
});
2012-03-10 08:40:00 +00:00
};
BaseSQL.prototype._escapeIdValue = function(model, idValue) {
var idProp = this.getDataSource(model).idProperty(model);
if(typeof this.toDatabase === 'function') {
return this.toDatabase(idProp, idValue);
} else {
if(idProp.type === Number) {
return idValue;
} else {
return "'" + idValue + "'";
}
}
};
2013-08-09 22:16:32 +00:00
/**
* Delete all model instances
*
* @param {String} model The model name
* @param {Function} callback The callback function
*/
2013-06-26 03:36:00 +00:00
BaseSQL.prototype.deleteAll = BaseSQL.prototype.destroyAll = function destroyAll(model, callback) {
this.command('DELETE FROM ' + this.tableEscaped(model), function (err, result) {
callback && callback(err, result);
});
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Count all model instances by the where filter
*
* @param {String} model The model name
* @param {Function} callback The callback function
* @param {Object} where The where clause
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.count = function count(model, callback, where) {
2014-01-24 17:09:53 +00:00
var self = this;
var props = this._models[model].properties;
this.queryOne('SELECT count(*) as cnt FROM ' +
this.tableEscaped(model) + ' ' + buildWhere(where), function (err, res) {
if (err) {
return callback(err);
}
2014-01-24 17:09:53 +00:00
callback(err, res && res.cnt);
});
function buildWhere(conds) {
var cs = [];
Object.keys(conds || {}).forEach(function (key) {
var keyEscaped = self.columnEscaped(model, key);
if (conds[key] === null) {
cs.push(keyEscaped + ' IS NULL');
} else {
cs.push(keyEscaped + ' = ' + self.toDatabase(props[key], conds[key]));
}
2012-03-10 08:40:00 +00:00
});
2014-01-24 17:09:53 +00:00
return cs.length ? ' WHERE ' + cs.join(' AND ') : '';
}
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Update attributes for a given model instance
* @param {String} model The model name
* @param {*} id The id value
* @param {Object} data The model data instance containing all properties to be updated
* @param {Function} cb The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
if (!isIdValuePresent(id, cb)) {
return;
}
var idName = this.getDataSource(model).idName(model);
data[idName] = id;
2014-01-24 17:09:53 +00:00
this.save(model, data, cb);
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Disconnect from the connector
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.disconnect = function disconnect() {
// No operation
2012-03-10 08:40:00 +00:00
};
/**
* Recreate the tables for the given models
* @param {[String]|String} [models] A model name or an array of model names,
* if not present, apply to all models defined in the connector
2013-08-09 22:16:32 +00:00
* @param {Function} [cb] The callback function
*/
BaseSQL.prototype.automigrate = function (models, cb) {
2014-01-24 17:09:53 +00:00
var self = this;
2014-01-24 17:09:53 +00:00
if ((!cb) && ('function' === typeof models)) {
cb = models;
models = undefined;
}
// First argument is a model name
if ('string' === typeof models) {
models = [models];
}
models = models || Object.keys(self._models);
async.each(models, function (model, callback) {
2014-01-24 17:09:53 +00:00
if (model in self._models) {
self.dropTable(model, function (err, result) {
self.createTable(model, function (err, result) {
if (err) {
console.error(err);
}
callback(err, result);
2014-01-24 17:09:53 +00:00
});
});
}
}, cb);
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Drop the table for the given model from the database
2013-08-09 22:16:32 +00:00
* @param {String} model The model name
* @param {Function} [cb] The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.dropTable = function (model, cb) {
2014-01-24 17:09:53 +00:00
this.command('DROP TABLE IF EXISTS ' + this.tableEscaped(model), cb);
2012-03-10 08:40:00 +00:00
};
2013-08-09 22:16:32 +00:00
/**
* Create the table for the given model
* @param {String} model The model name
* @param {Function} [cb] The callback function
*/
2012-03-10 08:40:00 +00:00
BaseSQL.prototype.createTable = function (model, cb) {
2014-01-24 17:09:53 +00:00
this.command('CREATE TABLE ' + this.tableEscaped(model) +
' (\n ' + this.propertiesSQL(model) + '\n)', cb);
2012-03-10 08:40:00 +00:00
};