Fixed memory adapter filtering + asteroid compatibility upddates
This commit is contained in:
parent
3628bd5be8
commit
7e23393e99
|
@ -191,10 +191,18 @@ function applyFilter(filter) {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
if(isNum(example.gt) && example.gt < value) return true;
|
||||
if(isNum(example.gte) && example.gte <= value) return true;
|
||||
if(isNum(example.lt) && example.lt > value) return true;
|
||||
if(isNum(example.lte) && example.lte >= value) return true;
|
||||
}
|
||||
// not strict equality
|
||||
return (example !== null ? example.toString() : example) == (value !== null ? value.toString() : value);
|
||||
}
|
||||
|
||||
function isNum(n) {
|
||||
return typeof n === 'number';
|
||||
}
|
||||
}
|
||||
|
||||
Memory.prototype.destroyAll = function destroyAll(model, callback) {
|
||||
|
@ -209,15 +217,11 @@ Memory.prototype.count = function count(model, callback, where) {
|
|||
var cache = this.cache[model];
|
||||
var data = Object.keys(cache)
|
||||
if (where) {
|
||||
data = data.filter(function (id) {
|
||||
var ok = true;
|
||||
Object.keys(where).forEach(function (key) {
|
||||
if (JSON.parse(cache[id])[key] != where[key]) {
|
||||
ok = false;
|
||||
}
|
||||
});
|
||||
return ok;
|
||||
});
|
||||
var filter = {where: where};
|
||||
data = data.map(function (id) {
|
||||
return this.fromDb(model, cache[id]);
|
||||
}.bind(this));
|
||||
data = data.filter(applyFilter(filter));
|
||||
}
|
||||
process.nextTick(function () {
|
||||
callback(null, data.length);
|
||||
|
|
18
lib/dao.js
18
lib/dao.js
|
@ -157,6 +157,10 @@ DataAccessObject.create = function (data, callback) {
|
|||
return obj;
|
||||
};
|
||||
|
||||
DataAccessObject.create.shared = true;
|
||||
DataAccessObject.create.accepts = {arg: 'data', type: 'object'};
|
||||
DataAccessObject.create.returns = {arg: 'data', type: 'object'};
|
||||
|
||||
function stillConnecting(schema, obj, args) {
|
||||
if (schema.connected) return false; // Connected
|
||||
|
||||
|
@ -358,6 +362,10 @@ DataAccessObject.findOne = function findOne(params, cb) {
|
|||
});
|
||||
};
|
||||
|
||||
DataAccessObject.findOne.shared = true;
|
||||
DataAccessObject.findOne.accepts = {arg: 'filter', type: 'object'};
|
||||
DataAccessObject.findOne.returns = {arg: 'data', type: 'object'};
|
||||
|
||||
/**
|
||||
* Destroy all records
|
||||
* @param {Function} cb - callback called with (err)
|
||||
|
@ -529,6 +537,13 @@ DataAccessObject.prototype.updateAttribute = function updateAttribute(name, valu
|
|||
this.updateAttributes(data, callback);
|
||||
};
|
||||
|
||||
DataAccessObject.prototype.updateAttribute.shared = true;
|
||||
DataAccessObject.prototype.updateAttribute.accepts = [
|
||||
{arg: 'name', type: 'string', required: true},
|
||||
{arg: 'value', type: 'any', required: true}
|
||||
];
|
||||
DataAccessObject.prototype.updateAttribute.returns = {arg: 'data', type: 'object'};
|
||||
|
||||
/**
|
||||
* Update set of attributes
|
||||
*
|
||||
|
@ -609,6 +624,9 @@ DataAccessObject.prototype.reload = function reload(callback) {
|
|||
this.constructor.find(this.id, callback);
|
||||
};
|
||||
|
||||
DataAccessObject.prototype.reload.shared = true;
|
||||
DataAccessObject.prototype.reload.returns = {arg: 'data', type: 'object'};
|
||||
|
||||
/**
|
||||
* Define readonly property on object
|
||||
*
|
||||
|
|
|
@ -57,8 +57,11 @@ function DataSource(name, settings) {
|
|||
ModelBuilder.call(this, arguments);
|
||||
this.setup(name, settings);
|
||||
|
||||
// default DataAccessObject
|
||||
this.DataAccessObject = this.constructor.DataAccessObject;
|
||||
// connector
|
||||
var connector = this.connector();
|
||||
|
||||
// DataAccessObject - connector defined or supply the default
|
||||
this.DataAccessObject = connector.DataAccessObject || this.constructor.DataAccessObject;
|
||||
this.DataAccessObject.call(this, arguments);
|
||||
|
||||
// operation metadata
|
||||
|
@ -73,8 +76,9 @@ function DataSource(name, settings) {
|
|||
accepts: fn.accepts,
|
||||
returns: fn.returns,
|
||||
http: fn.http,
|
||||
remoteEnabled: fn.shared || false,
|
||||
fn: fn
|
||||
remoteEnabled: fn.shared ? true : false,
|
||||
scope: this.DataAccessObject,
|
||||
fnName: name
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
|
@ -89,8 +93,9 @@ function DataSource(name, settings) {
|
|||
accepts: fn.accepts,
|
||||
returns: fn.returns,
|
||||
http: fn.http,
|
||||
remoteEnabled: fn.share,
|
||||
fn: fn
|
||||
remoteEnabled: fn.shared ? true : false,
|
||||
scope: this.DataAccessObject.prototype,
|
||||
fnName: name
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
|
@ -279,27 +284,39 @@ DataSource.prototype.createModel = DataSource.prototype.define;
|
|||
|
||||
DataSource.prototype.mixin = function (ModelCtor) {
|
||||
var ops = this.operations();
|
||||
var self = this;
|
||||
var DAO = this.DataAccessObject;
|
||||
|
||||
// mixin enabled operations as alias functions
|
||||
// mixin DAO
|
||||
jutil.mixin(ModelCtor, DAO);
|
||||
|
||||
// decorate operations as alias functions
|
||||
Object.keys(ops).forEach(function (name) {
|
||||
var op = ops[name];
|
||||
var fn = op.fn;
|
||||
var fn = op.scope[op.fnName];
|
||||
var scope;
|
||||
|
||||
if(op.enabled) {
|
||||
scope = op.prototype ? ModelCtor.prototype : ModelCtor;
|
||||
var sfn = scope[name] = function () {
|
||||
fn.apply(this, arguments);
|
||||
}
|
||||
Object.keys(op).forEach(function (key) {
|
||||
if(key !== 'fn' && key !== 'prototype') {
|
||||
// var sfn = scope[name] = function () {
|
||||
// op.scope[op.fnName].apply(self, arguments);
|
||||
// }
|
||||
Object.keys(op)
|
||||
.filter(function (key) {
|
||||
// filter out the following keys
|
||||
return ~ [
|
||||
'scope',
|
||||
'fnName',
|
||||
'prototype'
|
||||
].indexOf(key)
|
||||
})
|
||||
.forEach(function (key) {
|
||||
if(typeof op[key] !== 'undefined') {
|
||||
sfn[key] = op[key];
|
||||
op.scope[op.fnName][key] = op[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -324,6 +341,7 @@ DataSource.prototype.attach = function (ModelCtor) {
|
|||
|
||||
// redefine the schema
|
||||
hiddenProperty(ModelCtor, 'schema', this);
|
||||
ModelCtor.dataSource = this;
|
||||
|
||||
// add to def
|
||||
this.definitions[className] = {
|
||||
|
@ -1142,19 +1160,6 @@ DataSource.prototype.transaction = function() {
|
|||
return transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable a data source operation.
|
||||
*/
|
||||
|
||||
DataSource.prototype.enable = function (operation) {
|
||||
var op = this.getOperation(operation);
|
||||
if(op) {
|
||||
op.enabled = true;
|
||||
} else {
|
||||
throw new Error(operation + ' is provided by the attached connector');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable a data source operation remotely.
|
||||
*/
|
||||
|
@ -1163,20 +1168,6 @@ DataSource.prototype.enableRemote = function (operation) {
|
|||
var op = this.getOperation(operation);
|
||||
if(op) {
|
||||
op.remoteEnabled = true;
|
||||
} else {
|
||||
throw new Error(operation + ' is provided by the attached connector');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable a data source operation.
|
||||
*/
|
||||
|
||||
DataSource.prototype.disable = function (operation) {
|
||||
var op = this.getOperation(operation);
|
||||
if(op) {
|
||||
op.enabled =
|
||||
op.remoteEnabled = false;
|
||||
} else {
|
||||
throw new Error(operation + ' is not provided by the attached connector');
|
||||
}
|
||||
|
@ -1227,7 +1218,6 @@ DataSource.prototype.operations = function () {
|
|||
DataSource.prototype.defineOperation = function (name, options, fn) {
|
||||
options.fn = fn;
|
||||
options.name = name;
|
||||
options.enabled = true;
|
||||
this._operations[name] = options;
|
||||
}
|
||||
|
||||
|
|
|
@ -167,6 +167,10 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
|||
Object.keys(properties).forEach(cb);
|
||||
};
|
||||
|
||||
ModelClass.attachTo = function (dataSource) {
|
||||
dataSource.attach(this);
|
||||
}
|
||||
|
||||
ModelClass.registerProperty = function (attr) {
|
||||
var DataType = properties[attr].type;
|
||||
if(!DataType) {
|
||||
|
|
Loading…
Reference in New Issue