Permalink
Browse files

Move implementation of "select" into "all.attributes"

  • Loading branch information...
1 parent a59bdd9 commit a000cc3475b77f6858879d619ba856dca3a5c6d4 @1602 committed Jul 3, 2014
Showing with 31 additions and 77 deletions.
  1. +0 −64 lib/adapters/memory.js
  2. +27 −9 lib/model.js
  3. +4 −4 test/basic-querying.test.js
View
@@ -105,70 +105,6 @@ Memory.prototype.fromDb = function(model, data) {
return data;
};
-Memory.prototype.select = function all(model, filter, callback) {
- var self = this;
- var table = this.table(model);
- var nodes;
- if (filter && filter.attributes) {
- nodes = filter.attributes;
- } else {
- nodes = Object.keys(this.cache[table]).map(function (key) {
- return this.fromDb(model, this.cache[table][key]);
- }.bind(this));
- }
- if (filter) {
-
- // do we need some sorting?
- if (filter.order) {
- var props = this._models[model].properties;
- var orders = filter.order;
- if (typeof filter.order === "string") {
- orders = [filter.order];
- }
- orders.forEach(function (key, i) {
- var reverse = 1;
- var m = key.match(/\s+(A|DE)SC$/i);
- if (m) {
- key = key.replace(/\s+(A|DE)SC/i, '');
- if (m[1].toLowerCase() === 'de') reverse = -1;
- }
- orders[i] = {"key": key, "reverse": reverse};
- });
- nodes = nodes.sort(sorting.bind(orders));
- }
-
- // do we need some filtration?
- if (filter.where) {
- nodes = nodes ? nodes.filter(applyFilter(filter)) : nodes;
- }
-
- // limit/skip
- filter.skip = filter.skip || 0;
- filter.limit = filter.limit || nodes.length;
- nodes = nodes.slice(filter.skip, filter.skip + filter.limit);
-
- }
-
- process.nextTick(function () {
- if (filter && filter.include) {
- self._models[model].model.include(nodes, filter.include, callback);
- } else {
- callback(null, nodes);
- }
- });
-
- function sorting(a, b) {
- for (var i=0, l=this.length; i<l; i++) {
- if (a[this[i].key] > b[this[i].key]) {
- return 1*this[i].reverse;
- } else if (a[this[i].key] < b[this[i].key]) {
- return -1*this[i].reverse;
- }
- }
- return 0;
- }
-};
-
Memory.prototype.all = function all(model, filter, callback) {
var self = this;
var table = this.table(model);
View
@@ -455,6 +455,7 @@ AbstractClass.find = function find(id, cb) {
* @param {Object} params (optional)
*
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
+ * - attributes: Array '['id, 'name'] or String 'id'
* - include: String, Object or Array. See AbstractClass.include documentation.
* - order: String
* - limit: Number
@@ -486,16 +487,34 @@ AbstractClass.all = function all(params, cb) {
}
this.schema.adapter.all(this.modelName, paramsFiltered, function (err, data) {
if (data && data.forEach) {
+ if (params && params.attributes && data && data.length) {
+ if ('string' === typeof params.attributes) {
+ return cb(err, data.map(function(n) {
+ return n[params.attributes];
+ }));
+ } else if ('function' === typeof data.map) {
+ var isClean = Object.getOwnPropertyNames(data[0]).every(function(key) {
+ return params.attributes.indexOf(key) !== -1;
+ });
+ if (!isClean) {
+ data.forEach(function(node) {
+ Object.keys(node).forEach(function(key) {
+ if (params.attributes.indexOf(key) === -1) {
+ delete node[key];
+ }
+ });
+ });
+ }
+ }
+ }
if (!params || !params.onlyKeys) {
data.forEach(function (d, i) {
var obj = new constr;
- if (params && !params.attributes) {
- obj._initProperties(d, false);
- if (params && params.include && params.collect) {
- data[i] = obj.__cachedRelations[params.collect];
- } else {
- data[i] = obj;
- }
+ obj._initProperties(d, false);
+ if (params && params.include && params.collect) {
+ data[i] = obj.__cachedRelations[params.collect];
+ } else {
+ data[i] = obj;
}
});
}
@@ -518,7 +537,6 @@ AbstractClass.all = function all(params, cb) {
* @param {Object} params (optional)
*
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
-* - attributes: Array '['id, 'name'] or String 'id'
* - include: String, Object or Array. See AbstractClass.include documentation.
* - order: String
* - limit: Number
@@ -1029,4 +1047,4 @@ function defineReadonlyProp(obj, key, value) {
configurable: true,
value: value
});
-}
+}
@@ -133,10 +133,10 @@ describe('basic-querying', function() {
});
});
- describe('select', function () {
+ describe('#all.attributes', function () {
it('should query collection and return given attribute as an array of Objects', function(done) {
- User.select({attributes: ['id']}, function(err, users) {
+ User.all({attributes: ['id']}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.be.instanceOf(Array);
@@ -146,7 +146,7 @@ describe('basic-querying', function() {
});
it('should query collection and return given attribute as an array of Numbers', function(done) {
- User.select({attributes: 'id'}, function(err, users) {
+ User.all({attributes: 'id'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.be.instanceOf(Array);
@@ -156,7 +156,7 @@ describe('basic-querying', function() {
});
it('should query collection and return given attributes as an array of objects', function(done) {
- User.select({attributes: ['id', 'name']}, function(err, users) {
+ User.all({attributes: ['id', 'name']}, function(err, users) {
should.exists(users);
should.not.exists(err);
should.not.exists(users.pop().mail);

0 comments on commit a000cc3

Please sign in to comment.