summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-07 23:05:44 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-07 23:05:44 -0400
commit68fc2c92fad18c1685f5d67048d3147bb38d55c7 (patch)
tree7418995f1ff084b9a92349547ae4f2e9a9ae3c13 /app/node_modules/okresource/index.js
parent007384de007844e865bf906b8cb6eadaed4027e6 (diff)
ID fields for resources now determined by app config
Diffstat (limited to 'app/node_modules/okresource/index.js')
-rw-r--r--app/node_modules/okresource/index.js110
1 files changed, 96 insertions, 14 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index 2eba03a..7cd51c7 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -15,21 +15,47 @@ function OKResource(options) {
throw new Error('No schema provided to OKResource');
if (!options.db)
throw new Error('No DB provided to OKResource');
+
var schema = options.schema;
+ // Iterate through spec to find field which will act as the
+ // resource id in da db.
+ var idField = Object.keys(schema.spec).reduce(function(idField, prop) {
+ var spec = schema.spec[prop];
+ if (spec.id)
+ idField = prop;
+ return idField;
+ // If schema has a prop called 'id', default to that one
+ }, schema.id && 'id');
+ if (!idField)
+ throw new Error('Bad schema: no ID field');
+
var type = options.type;
this._db = options.db;
+
// Define properties which are part of the API
+
Object.defineProperty(this, 'schema', {
value: schema,
- writable: false
+ writable: false,
+ enumerable: true
});
+
Object.defineProperty(this, 'spec', {
value: schema.spec,
- writable: false
+ writable: false,
+ enumerable: true
});
+
Object.defineProperty(this, 'type', {
value: type,
- writable: false
+ writable: false,
+ enumerable: true
+ });
+
+ Object.defineProperty(this, 'idField', {
+ value: idField,
+ writable: false,
+ enumerable: true
});
}
@@ -45,37 +71,93 @@ OKResource.prototype.all = function() {
};
OKResource.prototype.create = function(data) {
- return this._db.create(this.type, data);
+ data = data || {};
+ var id = data[this.idField];
+ return Q.promise(function(resolve, reject) {
+ if (!id) {
+ reject(new Error('Data does not contain ID property'));
+ } else {
+ this._db.create(this.type, data).then(resolve, reject);
+ }
+ });
};
OKResource.prototype.destroy = function(data) {
- return this._db.remove(this.type, data.id, data);
+ data = data || {};
+ var id = data[this.idField];
+ return Q.promise(function(resolve, reject) {
+ if (!id) {
+ reject(new Error('Data does not contain ID property'));
+ } else {
+ this._db.remove(this.type, data.id, data).then(resolve, reject);
+ }
+ });
};
OKResource.prototype.find = function(query) {
- return this._db.find(this.type, query);
+ return Q.promise(function(resolve, reject) {
+ if (!query) {
+ throw new Error('No query given');
+ } else {
+ this._db.find(this.type, query).then(resolve, reject);
+ }
+ });
};
OKResource.prototype.get = function(id) {
- return this._db.get(this.type, id);
+ var db = this._db;
+ var type = this.type;
+ var idField = this.idField;
+ return Q.promise(function(resolve, reject) {
+ if (!id) {
+ throw new Error('No ID given');
+ } else {
+ // We have the id, but we still need
+ // to resolve which field is the id field
+ // to match
+ var query = {};
+ query[idField] = id;
+ db.get(type, query).then(resolve, reject);
+ }
+ });
};
OKResource.prototype.update = function(data) {
data = data || {};
- return this._db.put(this.type, data.id, 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'));
+ } else {
+ var query = {};
+ query[idField] = data[idField];
+ db.put(type, query, data).then(resolve, reject);;
+ }
+ });
};
OKResource.prototype.updateOrCreate = function(data) {
data = data || {};
+ var id = data[this.idField];
var type = this.type;
var db = this._db;
+ var idField = this.idField;
return Q.promise(function(resolve, reject) {
- db.get(type, data.id).then(function(cached) {
- if (cached)
- db.put(type, data.id, data).then(resolve, reject);
- else
- db.create(type, data).then(resolve, reject);
- }, reject);
+ if (!id) {
+ reject(new Error('Cannot updateOrCreate without ID'));
+ } else {
+ db.get(type, data.id).then(function(cached) {
+ var query = {};
+ query[idField] = id;
+ if (cached)
+ db.put(type, query, data).then(resolve, reject);
+ else
+ db.create(type, data).then(resolve, reject);
+ }, reject);
+ }
});
};