diff options
| -rw-r--r-- | app/node_modules/okdb/index.js | 48 | ||||
| -rw-r--r-- | app/node_modules/okquery/index.js | 3 | ||||
| -rw-r--r-- | app/node_modules/okresource/index.js | 110 | ||||
| -rw-r--r-- | examples/index.js | 10 |
4 files changed, 143 insertions, 28 deletions
diff --git a/app/node_modules/okdb/index.js b/app/node_modules/okdb/index.js index 6c01df0..3089411 100644 --- a/app/node_modules/okdb/index.js +++ b/app/node_modules/okdb/index.js @@ -37,19 +37,51 @@ function FSDB(options) { this._db = low(filename); } -FSDB.prototype._resolve = function(data, success) { - success = typeof success === 'undefined' ? true : success; +FSDB.prototype._resolve = function(data, error) { return Q.Promise(function resolvePromise(resolve, reject) { - if (success) + if (error) { + reject(error); + } else { resolve(data); - else - reject(data); + } }); }; -FSDB.prototype.get = function(collection, id) { - var data = this._db(collection).get(id); - return this._resolve(data); +FSDB.prototype.get = function(collection, query) { + var data = this._db(collection).find(query); + if (!query) { + return this._resolve(null, new Error('No query given')); + } else { + return this._resolve(data); + } +}; + +FSDB.prototype.put = function(collection, query, data) { + data = data || {}; + if (!query) { + return this._resolve(null, new Error('No query given')); + } else if (this._db(collection).find(query)) { + var updated = this._db(collection) + .chain() + .find(query) + .assign(data) + .value(); + return this._resolve(updated); + } else { + return this._resolve(null, new Error('Cannot update nonexistent entry')); + } +}; + +FSDB.prototype.create = function(collection, id, data) { + throw new Error('Not implemented!'); +}; + +FSDB.prototype.remove = function(collection, id, data) { + throw new Error('Not implemented!'); +}; + +FSDB.prototype.find = function(collection, id, data) { + throw new Error('Not implemented!'); }; FSDB.prototype.getMeta = function() { diff --git a/app/node_modules/okquery/index.js b/app/node_modules/okquery/index.js index c28971f..a64f0f2 100644 --- a/app/node_modules/okquery/index.js +++ b/app/node_modules/okquery/index.js @@ -37,8 +37,9 @@ function createQuery(resource, query, options) { } else { query = querySingle(resource, query); } - if (options.default) + if (options.default) { query = withDefault(query, options.default); + } return query; } 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); + } }); }; diff --git a/examples/index.js b/examples/index.js index 7e0c6f9..c4fd3be 100644 --- a/examples/index.js +++ b/examples/index.js @@ -2,15 +2,15 @@ var okcms = require('..'); var app = okcms.createApp({ - root: 'www', + root: 'public', schemas: { page: { - title: {type: 'string'}, + title: {type: 'string', id: true}, body: {type: 'string'} }, project: { - title: {type: 'string'}, + title: {type: 'string', id: true}, index: {type: 'integer'}, category: {type: 'enum'}, body: {type: 'string'}, @@ -20,8 +20,8 @@ var app = okcms.createApp({ }, resources: [ - { type: 'page', id: 'about' }, - { type: 'page', id: 'contact' }, + { type: 'page', data: {title: 'about'}}, + { type: 'page', data: {title: 'contact'}}, { type: 'project' }, ], |
