2013-10-02 05:14:21 +00:00
|
|
|
module.exports = Connector;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for LooopBack connector. This is more a collection of useful
|
|
|
|
* methods for connectors than a super class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2013-10-04 19:54:14 +00:00
|
|
|
function Connector(name, settings) {
|
2014-01-24 17:09:53 +00:00
|
|
|
this._models = {};
|
|
|
|
this.name = name;
|
|
|
|
this.settings = settings || {};
|
2013-10-02 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the relational property to indicate the backend is a relational DB
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
Connector.prototype.relational = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a command with given parameters
|
|
|
|
* @param {String} command The command such as SQL
|
|
|
|
* @param {Object[]} [params] An array of parameters
|
|
|
|
* @param {Function} [callback] The callback function
|
|
|
|
*/
|
|
|
|
Connector.prototype.execute = function (command, params, callback) {
|
2014-01-24 17:09:53 +00:00
|
|
|
throw new Error('query method should be declared in connector');
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Look up the data source by model name
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @returns {DataSource} The data source
|
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
Connector.prototype.getDataSource = function (model) {
|
|
|
|
var m = this._models[model];
|
|
|
|
if (!m) {
|
|
|
|
console.trace('Model not found: ' + model);
|
|
|
|
}
|
|
|
|
return m && m.model.dataSource;
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id property name
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @returns {String} The id property name
|
|
|
|
*/
|
|
|
|
Connector.prototype.idName = function (model) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.getDataSource(model).idName(model);
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id property names
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @returns {[String]} The id property names
|
|
|
|
*/
|
|
|
|
Connector.prototype.idNames = function (model) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.getDataSource(model).idNames(model);
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id index (sequence number, starting from 1)
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {String} prop The property name
|
|
|
|
* @returns {Number} The id index, undefined if the property is not part of the primary key
|
|
|
|
*/
|
|
|
|
Connector.prototype.id = function (model, prop) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var p = this._models[model].properties[prop];
|
|
|
|
if (!p) {
|
|
|
|
console.trace('Property not found: ' + model + '.' + prop);
|
|
|
|
}
|
|
|
|
return p.id;
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to be called by DataSource for defining a model
|
|
|
|
* @param {Object} modelDefinition The model definition
|
|
|
|
*/
|
|
|
|
Connector.prototype.define = function (modelDefinition) {
|
2014-01-24 17:09:53 +00:00
|
|
|
if (!modelDefinition.settings) {
|
|
|
|
modelDefinition.settings = {};
|
|
|
|
}
|
|
|
|
this._models[modelDefinition.model.modelName] = modelDefinition;
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to be called by DataSource for defining a model property
|
|
|
|
* @param {String} model The model name
|
2013-10-02 22:18:50 +00:00
|
|
|
* @param {String} propertyName The property name
|
|
|
|
* @param {Object} propertyDefinition The object for property metadata
|
2013-10-02 05:14:21 +00:00
|
|
|
*/
|
2013-10-02 22:18:50 +00:00
|
|
|
Connector.prototype.defineProperty = function (model, propertyName, propertyDefinition) {
|
2014-01-24 17:09:53 +00:00
|
|
|
this._models[model].properties[propertyName] = propertyDefinition;
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from the connector
|
|
|
|
*/
|
|
|
|
Connector.prototype.disconnect = function disconnect(cb) {
|
2014-01-24 17:09:53 +00:00
|
|
|
// NO-OP
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id value for the given model
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {Object} data The model instance data
|
|
|
|
* @returns {*} The id value
|
|
|
|
*
|
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
Connector.prototype.getIdValue = function (model, data) {
|
|
|
|
return data && data[this.idName(model)];
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the id value for the given model
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {Object} data The model instance data
|
|
|
|
* @param {*} value The id value
|
|
|
|
*
|
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
Connector.prototype.setIdValue = function (model, data, value) {
|
|
|
|
if (data) {
|
|
|
|
data[this.idName(model)] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Connector.prototype.getType = function () {
|
|
|
|
return this.type;
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|