summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-08 19:50:19 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-08 19:50:19 -0400
commit5f3d47c4bea9c94152a8243c304ee0f43e84ac07 (patch)
tree632e484c4c8c7f299676e86f874feb79261d45a1 /app/node_modules/okresource/index.js
parentbb3d8da23279dc2e4cf275b08b03148b3980fb01 (diff)
Proper API for resource updates
Diffstat (limited to 'app/node_modules/okresource/index.js')
-rw-r--r--app/node_modules/okresource/index.js16
1 files changed, 7 insertions, 9 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index 6010d7d..80279dc 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -128,26 +128,24 @@ OKResource.prototype.get = function(id) {
});
};
-OKResource.prototype.update = function(data) {
+OKResource.prototype.update = function(id, data) {
data = data || {};
- var id = data[this.idField];
var db = this._db;
var type = this.type;
var idField = this.idField;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('Data does not contain ID property'));
+ reject(new Error('No resource ID provided'));
} else {
var query = {};
- query[idField] = data[idField];
+ query[idField] = id;
db.put(type, query, data).then(resolve).fail(reject);;
}
});
};
-OKResource.prototype.updateOrCreate = function(data) {
+OKResource.prototype.updateOrCreate = function(id, data) {
data = data || {};
- var id = data[this.idField];
var type = this.type;
var db = this._db;
var idField = this.idField;
@@ -155,7 +153,7 @@ OKResource.prototype.updateOrCreate = function(data) {
query[idField] = id;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('Cannot updateOrCreate without ID'));
+ reject(new Error('No resource ID provided'));
} else {
db.get(type, query).then(function(persisted) {
if (persisted) {
@@ -221,12 +219,12 @@ function OKResourceInstance(resource, options) {
} else {
// When updating static resources, we create them if
// they don't actually exist in the DB
- resource.updateOrCreate(data).then(resolve).fail(reject);
+ resource.updateOrCreate(id, data).then(resolve).fail(reject);
}
});
};
- this.updateOrCreate = function(id) {
+ this.updateOrCreate = function(data) {
return Q.promise(function(resolve, reject) {
reject(new Error('Cannot updateOrCreate static resource'));
});