diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-03 13:12:34 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 15:27:52 -0400 |
| commit | 35bdb78a6701b919157b0dafa75ff39904197d49 (patch) | |
| tree | 9d16a52117b078e152e2a39abec84e2b294fee03 | |
| parent | 8ea01b6737259f7380a1295d538456ebc4d1c30f (diff) | |
Expose schema as part of resource API
| -rw-r--r-- | app/node_modules/okresource/.index.js | 102 | ||||
| -rw-r--r-- | app/node_modules/okresource/index.js | 8 |
2 files changed, 108 insertions, 2 deletions
diff --git a/app/node_modules/okresource/.index.js b/app/node_modules/okresource/.index.js new file mode 100644 index 0000000..8664fc0 --- /dev/null +++ b/app/node_modules/okresource/.index.js @@ -0,0 +1,102 @@ +var resource = require('resource'); +var Q = require('q'); + +/** + * 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 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; + this._db = options.db; + this._schema = options.schema; + // this._resource = resource.define(type, {}, schema.getMschema()); + // this._resource.persist(dbConfig); + 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); +}; + +OKResource.prototype.all = function() { + return this._db.getAll(this.type); +}; + +OKResource.prototype.create = function(data) { + return this._db.create(this.type, data); +}; + +OKResource.prototype.destroy = function(data) { + return this._db.remove(this.type, data.id, data); +}; + +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); + }); +}; + +// /** +// * Proxy CRUD methods to underlying resource module +// * additionally wrapping them in a promise +// */ +// [ 'all', 'create', 'destroy', 'find', +// 'get', 'update', 'updateOrCreate' ].forEach(function(method) { +// OKResource.prototype[method] = function() { +// var resource = this._resource; +// var args = [].slice.call(arguments); + +// return Q.promise(function(resolve, reject) { +// args.push(callback); +// resource[method].apply(resource, args); + +// function callback() { +// var args = [].slice.call(arguments); +// var error = args.shift(); +// if (err) +// reject.call(null, err); +// else +// resolve.apply(null, args); +// } +// }); +// }; +// }); + +module.exports = OKResource; diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js index 9f0c9d4..055a24a 100644 --- a/app/node_modules/okresource/index.js +++ b/app/node_modules/okresource/index.js @@ -17,7 +17,11 @@ function OKResource(options) { throw new Error('No DB provided to OKResource'); var type = options.type; this._db = options.db; - this._schema = options.schema; + // Define properties which are part of the API + Object.defineProperty(this, 'schema', { + value: schema, + writable: false + }); Object.defineProperty(this, 'type', { value: type, writable: false @@ -28,7 +32,7 @@ function OKResource(options) { * Throws an error if data does not conform to schema */ OKResource.prototype.assertValid = function(data) { - this._schema.assertValid(data); + this.schema.assertValid(data); }; OKResource.prototype.all = function() { |
