diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-03 12:39:01 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-03 12:39:20 -0400 |
| commit | 7d15bc8a49121dd85171d7837c4f6a97a7e096dc (patch) | |
| tree | 88f06e2957cd4f1d0b91dd3cd2ebe0b41ac7401e | |
| parent | 0c4f6ad96c2ee1b1c948dd227e291ff73023b95b (diff) | |
Beef up resource abstraction
| -rw-r--r-- | app/index.js | 15 | ||||
| -rw-r--r-- | app/node_modules/okresource/index.js | 103 | ||||
| -rw-r--r-- | app/node_modules/okresource/package.json | 6 |
3 files changed, 72 insertions, 52 deletions
diff --git a/app/index.js b/app/index.js index 29dcfd0..6d057d3 100644 --- a/app/index.js +++ b/app/index.js @@ -56,19 +56,22 @@ OKCMS.prototype._createSchemas = function(schemaConfig) { OKCMS.prototype._createResources = function(resourceConfig, db, schemaCache) { resourceConfig = resourceConfig || {}; - return resourceConfig.map(function(config) { + var typeCache = {}; + return resourceConfig.reduce(function(cache, config) { var type = config.type; var schema = schemaCache[type]; if (!schema) throw new Error('Resource config references nonexistent schema'); - // If no id is provided, default to "select all" - var id = config.id || '*'; - return OKResource({ + // If we already created resource class, just skip + if (cache[type]) + return cache; + cache[type] = OKResource({ type: type, - id: id, + db: db, schema: schema }) - }); + return cache; + }, {}); }; OKCMS.prototype._createViews = function(viewConfig, db, 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; diff --git a/app/node_modules/okresource/package.json b/app/node_modules/okresource/package.json index 43824ed..9462887 100644 --- a/app/node_modules/okresource/package.json +++ b/app/node_modules/okresource/package.json @@ -7,5 +7,9 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "OKFocus", - "license": "None" + "license": "None", + "dependencies": { + "q": "^1.2.0", + "resource": "^0.5.6" + } } |
