From e01c2a20352d252e40ea133d0f4665b2e7513582 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Thu, 9 Apr 2015 18:48:10 -0400 Subject: Lil fixes + defensive programming --- app/node_modules/okquery/index.js | 9 +++++++++ app/node_modules/okquery/package.json | 1 + 2 files changed, 10 insertions(+) (limited to 'app/node_modules/okquery') diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index 2d93e2a..89c8b73 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -1,3 +1,4 @@ +var cloneDeep = require('lodash.clonedeep'); var assign = require('object-assign'); var isobject = require('lodash.isobject'); var Q = require('q'); @@ -16,6 +17,9 @@ function OKQuery(options) { var resource = options.resource; var type = resource.type; var query = options.query || '*'; + // Ensure immutability + if (isobject(query)) + query = cloneDeep(query); Object.defineProperty(this, 'resource', { value: resource, @@ -55,6 +59,9 @@ function createQuery(resource, query, options) { function queryComplex(resource, query) { var dynamicProp; + // Query is an object specifying key value pairs against which + // to match DB entries. Iterate through and check if any of the values + // is unbound e.g. :id var notDynamic = Object.keys(query).every(function(prop) { var matcher = query[prop]; if (isDynamic(matcher)) { @@ -71,6 +78,8 @@ function queryComplex(resource, query) { } } else { return function(id) { + // Bind the dynamic property to its value + // and add the pair to the query var dynamicQuery = {}; dynamicQuery[dynamicProp] = id; var query = assign({}, query, dynamicQuery); diff --git a/app/node_modules/okquery/package.json b/app/node_modules/okquery/package.json index 606d45b..5ba9dd5 100644 --- a/app/node_modules/okquery/package.json +++ b/app/node_modules/okquery/package.json @@ -9,6 +9,7 @@ "author": "OKFocus", "license": "None", "dependencies": { + "lodash.clonedeep": "^3.0.0", "lodash.isobject": "^3.0.1", "object-assign": "^2.0.0", "q": "^1.2.0" -- cgit v1.2.3-70-g09d2 From 4021d7846ce164f3f0c3cb37d3a1d82d2de489d9 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 23:09:05 -0400 Subject: Give views sorted resources --- app/node_modules/okdb/index.js | 11 +++++++++++ app/node_modules/okquery/index.js | 3 ++- app/node_modules/okresource/index.js | 11 ++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) (limited to 'app/node_modules/okquery') diff --git a/app/node_modules/okdb/index.js b/app/node_modules/okdb/index.js index 7639ec6..c00087c 100644 --- a/app/node_modules/okdb/index.js +++ b/app/node_modules/okdb/index.js @@ -117,6 +117,17 @@ FSDB.prototype.remove = function(collection, id) { } }; +FSDB.prototype.sortBy = function(collection, prop, descend) { + var schema = this._schemas[collection]; + if (!schema) + return resolve(null, new Error('No such collection type')); + if (!prop) + return resolve(null, new Error('Bad input')); + + var result = this._db(collection).sortByOrder([prop], [!descend]); + return resolve(result); +}; + FSDB.prototype.find = function(collection, id) { var schema = this._schemas[collection]; if (!schema) diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index 89c8b73..9cc8b78 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -96,7 +96,8 @@ function queryDynamic(resource) { function queryAll(resource) { return function() { - return resource.all(); + // Always return sorted results + return resource.sortBy('__index'); }; } diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js index df89617..8c0bb16 100644 --- a/app/node_modules/okresource/index.js +++ b/app/node_modules/okresource/index.js @@ -122,12 +122,21 @@ OKResource.prototype.update = function(id, data) { reject(new Error('No resource ID provided')); } else if (!data) { reject(new Error('No data provided')); - }else { + } else { db.update(type, id, data).then(resolve).fail(reject);; } }); }; + +/** + * Get all documents in collection sorted by property, + * optionally in descending order + */ +OKResource.prototype.sortBy = function(prop, descend) { + return this._db.sortBy(this.type, prop, descend); +}; + /** * Update all resources with the given ids with the given data */ -- cgit v1.2.3-70-g09d2