summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-11 01:36:09 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-11 01:38:43 -0400
commita20297451b88c604b16a35223be4b25528713c6d (patch)
tree98c44d57dc805bcf2453b23f74e60eb244f46e99 /app
parent4021d7846ce164f3f0c3cb37d3a1d82d2de489d9 (diff)
Implement FSDB.updateBatch and OKResource.updateBatch
Diffstat (limited to 'app')
-rw-r--r--app/node_modules/okdb/index.js20
-rw-r--r--app/node_modules/okresource/index.js36
2 files changed, 34 insertions, 22 deletions
diff --git a/app/node_modules/okdb/index.js b/app/node_modules/okdb/index.js
index c00087c..ffe3ff1 100644
--- a/app/node_modules/okdb/index.js
+++ b/app/node_modules/okdb/index.js
@@ -101,6 +101,26 @@ FSDB.prototype.update = function(collection, id, data) {
}
};
+/**
+ * TODO Should be atomic ¯\_(ツ)_/¯
+ */
+FSDB.prototype.updateBatch = function(collection, ids, datas) {
+ var schema = this._schemas[collection];
+ if (!schema)
+ return resolve(null, new Error('No such collection type'));
+ if (!ids || !ids.length || !datas || !datas.length ||
+ ids.length !== datas.length) {
+ return resolve(null, new Error('Bad input'));
+ }
+
+ var doc = this._db(collection);
+ var results = ids.map(function(id, i) {
+ return doc.chain().find(getQuery(schema, id)).assign(datas[i]).value();
+ });
+
+ return resolve(results);
+};
+
FSDB.prototype.remove = function(collection, id) {
var schema = this._schemas[collection];
if (!schema)
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index 8c0bb16..c3f9adb 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -128,6 +128,20 @@ OKResource.prototype.update = function(id, data) {
});
};
+OKResource.prototype.updateBatch = function(ids, datas) {
+ var self = this;
+ var db = this._db;
+ var type = this.type;
+ return Q.promise(function(resolve, reject) {
+ if (!ids || !ids.length || !datas || !datas.length ||
+ ids.length !== datas.length) {
+ reject(new Error('Bad input'));
+ } else {
+ db.updateBatch(type, ids, datas).then(resolve).fail(reject);
+ }
+ });
+}
+
/**
* Get all documents in collection sorted by property,
@@ -137,28 +151,6 @@ 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
- */
-OKResource.prototype.updateBatch = function(ids, datas) {
- // var type = this.type;
- // var db = this._db;
- // var idField = this.idField;
- // return Q.promise(function(resolve, reject) {
- // if (!ids || !ids.length || !datas || !datas.length ||
- // ids.length !== datas.length) {
- // reject(new Error('Bad input'));
- // } else {
- // var queries = ids.map(function(id, i) {
- // var query = {};
- // query[idField] = datas[i][idField];
- // return query;
- // });
- // db.putAll(type, queries, datas).then(resolve).fail(reject);
- // }
- // });
-};
-
OKResource.prototype.updateOrCreate = function(id, data) {
data = data || {};
var type = this.type;