summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-04-03 12:39:01 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-04-03 12:39:20 -0400
commit7d15bc8a49121dd85171d7837c4f6a97a7e096dc (patch)
tree88f06e2957cd4f1d0b91dd3cd2ebe0b41ac7401e /app/node_modules/okresource/index.js
parent0c4f6ad96c2ee1b1c948dd227e291ff73023b95b (diff)
Beef up resource abstraction
Diffstat (limited to 'app/node_modules/okresource/index.js')
-rw-r--r--app/node_modules/okresource/index.js103
1 files changed, 58 insertions, 45 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index b7dd4b6..9f0c9d4 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -1,60 +1,73 @@
-var OKRestEndpoint = require('okrest');
+var Q = require('q');
/**
- * Creates an OKResource types
- * Takes a resource name and a spec defining the resources attributes.
+ * OKResource!
+ * TODO Would be nicer to compose this with an existing resource
+ * module, but haven't found a good fit. Should keep an eye on
+ * 'resource' by bigcompany for an update.
*/
-function createResourceClass(options) {
+function OKResource(options) {
+ if (!(this instanceof OKResource)) return new OKResource(options);
options = options || {};
if (!options.type)
throw new Error('No resource type provided to OKResource')
if (!options.schema)
throw new Error('No schema provided to OKResource');
+ if (!options.db)
+ throw new Error('No DB provided to OKResource');
var type = options.type;
- var schema = options.schema;
- // All resources have the same default CRUD endpoint
- var viewClass = options.endpoint || OKRestEndpoint;
- // Id determines specific resource referenced.
- // Defaults to set of all resources of this type.
- var id = options.id || '*';
- if (viewClass !== OKRestEndpoint && !(viewClass instanceof OKRestEndpoint))
- throw new Error('Resource view not descendent of OKRestEndpoint');
+ this._db = options.db;
+ this._schema = options.schema;
+ Object.defineProperty(this, 'type', {
+ value: type,
+ writable: false
+ });
+}
- /**
- * OKResource!
- */
- function OKResource(options) {
- if (!(this instanceof OKResource)) return new OKResource(options);
- if (!options.db)
- throw new Error('No DB provided to OKResource');
- options = options || {};
- this._db = options.db;
- this._schema = schema;
- Object.defineProperty(this, 'type', {
- value: type,
- writable: false
- });
- }
+/**
+ * Throws an error if data does not conform to schema
+ */
+OKResource.prototype.assertValid = function(data) {
+ this._schema.assertValid(data);
+};
- /**
- * Returns the resource's CRUD view
- * This allows us to specify custom views per resource if need be
- */
- OKResource.prototype.view = function(options) {
- return viewClass(this, options);
- };
+OKResource.prototype.all = function() {
+ return this._db.getAll(this.type);
+};
- OKResource.prototype.assertValid = function(data) {
- this._schema.assertValid(data);
- }
+OKResource.prototype.create = function(data) {
+ return this._db.create(this.type, data);
+};
- // Expose the resource type on the class constructor
- Object.defineProperty(OKResource, 'type', {
- value: type,
- writable: false
- });
+OKResource.prototype.destroy = function(data) {
+ return this._db.remove(this.type, data.id, data);
+};
- return OKResource;
-}
+OKResource.prototype.find = function(query) {
+ return this._db.find(this.type, query);
+};
+
+OKResource.prototype.get = function(id) {
+ return this._db.get(this.type, id);
+};
+
+OKResource.prototype.update = function(data) {
+ data = data || {};
+ return this._db.put(this.type, data.id, data);
+};
+
+OKResource.prototype.updateOrCreate = function(data) {
+ data = data || {};
+ var type = this.type;
+ var db = this._db;
+ 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);
+ });
+};
-module.exports = createResourceClass;
+module.exports = OKResource;