summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-10 21:38:31 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-10 21:41:01 -0400
commit6e1f689cfd8f820090c4ab15519114f4d3bf929f (patch)
tree3ce5f2515fb37777660bb36f635071afbb9bf224 /app/node_modules/okresource/index.js
parent5abbdf600396144fbbe32b97e83beaabf6ed5c39 (diff)
Overhaul DB impl
Make DB schema aware Add autoincrement support Add custom ID field support
Diffstat (limited to 'app/node_modules/okresource/index.js')
-rw-r--r--app/node_modules/okresource/index.js126
1 files changed, 54 insertions, 72 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index 0e8498f..df89617 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -20,17 +20,6 @@ function OKResource(options) {
var schema = options.schema;
var spec = schema.spec;
- // Iterate through spec to find field which will act as the
- // resource id in da db.
- var idField = Object.keys(spec).reduce(function(idField, prop) {
- var propSpec = spec[prop];
- if (propSpec.id)
- idField = prop;
- return idField;
- // If schema has a prop called 'id', default to that one
- }, spec.id && 'id');
- if (!idField)
- throw new Error('Bad schema: no ID field');
var type = options.type;
this._db = options.db;
@@ -51,12 +40,6 @@ function OKResource(options) {
enumerable: true
});
- Object.defineProperty(this, 'idField', {
- value: idField,
- writable: false,
- enumerable: true
- });
-
// Whether this resource represents a specific data point
// or a whole class of data
Object.defineProperty(this, 'bound', {
@@ -74,19 +57,22 @@ OKResource.prototype.assertValid = function(data) {
};
OKResource.prototype.all = function() {
- return this._db.getAll(this.type);
+ return this._db.all(this.type);
};
-OKResource.prototype.create = function(data) {
+OKResource.prototype.getID = function(data) {
data = data || {};
+ return data[this._schema.idField];
+};
+
+OKResource.prototype.create = function(data) {
var type = this.type;
var db = this._db;
- var id = data[this.idField];
return Q.promise(function(resolve, reject) {
- if (!id) {
- reject(new Error('Data does not contain ID property'));
+ if (!data) {
+ reject(new Error('No data provided'));
} else {
- db.create(type, data).then(resolve).fail(reject);
+ db.insert(type, data).then(resolve).fail(reject);
}
});
};
@@ -94,13 +80,11 @@ OKResource.prototype.create = function(data) {
OKResource.prototype.destroy = function(id) {
var db = this._db;
var type = this.type;
- var query = {};
- query[this.idField] = id;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('No ID given'));
+ reject(new Error('No ID provided'));
} else {
- db.remove(type, query).then(resolve).fail(reject);
+ db.remove(type, id).then(resolve).fail(reject);
}
});
};
@@ -111,7 +95,7 @@ OKResource.prototype.find = function(query) {
var type = this.type;
return Q.promise(function(resolve, reject) {
if (!query) {
- throw new Error('No query given');
+ throw new Error('No query provided');
} else {
db.find(type, query).then(resolve).fail(reject);
}
@@ -121,53 +105,64 @@ OKResource.prototype.find = function(query) {
OKResource.prototype.get = function(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');
+ throw new Error('No ID provided');
} 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).fail(reject);
+ db.get(type, id).then(resolve).fail(reject);
}
});
};
OKResource.prototype.update = function(id, data) {
- data = data || {};
var db = this._db;
var type = this.type;
- var idField = this.idField;
return Q.promise(function(resolve, reject) {
if (!id) {
reject(new Error('No resource ID provided'));
- } else {
- var query = {};
- query[idField] = id;
- db.put(type, query, data).then(resolve).fail(reject);;
+ } else if (!data) {
+ reject(new Error('No data provided'));
+ }else {
+ db.update(type, id, data).then(resolve).fail(reject);;
}
});
};
+/**
+ * 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;
var db = this._db;
- var idField = this.idField;
- var query = {};
- query[idField] = id;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('No resource ID provided'));
+ reject(new Error('No ID provided'));
} else {
- db.get(type, query).then(function(persisted) {
+ db.get(type, id).then(function(persisted) {
if (persisted) {
- db.put(type, query, data).then(resolve).fail(reject);
+ db.update(type, id, data).then(resolve).fail(reject);
} else {
- db.create(type, data).then(resolve).fail(reject);
+ db.insert(type, data).then(resolve).fail(reject);
}
}).fail(reject);
}
@@ -196,11 +191,15 @@ function OKResourceInstance(resource, options) {
// configuration, but may not actually be present
// in the database and need custom logic to handle this.
var staticData = cloneDeep(options.static);
- var id = staticData[resource.idField];
+ var id = resource.getID(staticData);
if (!id)
throw new Error(
'Cannot create static OKResourceInstance without an ID field');
+ this.getID = function() {
+ return id;
+ };
+
/**
* Ensure that static data is provided on get
*/
@@ -209,9 +208,9 @@ function OKResourceInstance(resource, options) {
resource.get(id).then(function(data) {
// Note the assign call. Don't expose private references!
if (data) {
- resolve(assign({}, data, cloneDeep(staticData)));
+ resolve(assign(data, cloneDeep(staticData)));
} else {
- resolve(assign({}, cloneDeep(staticData)));
+ resolve(cloneDeep(staticData));
}
}).fail(reject);
});
@@ -286,14 +285,9 @@ function OKResourceInstance(resource, options) {
});
Object.defineProperty(this, 'spec', {
- value: resource.spec,
- writable: false,
- enumerable: true
- });
-
- Object.defineProperty(this, 'id', {
- value: id,
- writable: false,
+ get: function() {
+ return resource.spec
+ },
enumerable: true
});
@@ -303,23 +297,11 @@ function OKResourceInstance(resource, options) {
enumerable: true
});
- Object.defineProperty(this, 'idField', {
- value: resource.idField,
- writable: false,
- enumerable: true
- });
-
Object.defineProperty(this, 'bound', {
value: true,
writable: false,
enumerable: true
});
-
- Object.defineProperty(this, 'class', {
- value: resource,
- writable: false,
- enumerable: true
- });
}
module.exports = OKResource;