diff options
Diffstat (limited to 'app/node_modules/okresource/index.js')
| -rw-r--r-- | app/node_modules/okresource/index.js | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js index 94d8cfb..5a643de 100644 --- a/app/node_modules/okresource/index.js +++ b/app/node_modules/okresource/index.js @@ -1,4 +1,5 @@ var assign = require('object-assign'); +var cloneDeep = require('cloneDeep'); var Q = require('q'); /** @@ -18,15 +19,16 @@ function OKResource(options) { throw new Error('No DB provided to OKResource'); 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(schema.spec).reduce(function(idField, prop) { - var spec = schema.spec[prop]; - if (spec.id) + 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 - }, schema.spec.id && 'id'); + }, spec.id && 'id'); if (!idField) throw new Error('Bad schema: no ID field'); @@ -37,8 +39,9 @@ function OKResource(options) { // Define properties which are part of the API Object.defineProperty(this, 'spec', { - value: schema.spec, - writable: false, + get: function() { + return schema.spec; + }, enumerable: true }); @@ -190,7 +193,7 @@ function OKResourceInstance(resource, options) { // conceptually at all times since they are derived from app // configuration, but may not actually be present // in the database and need custom logic to handle this. - var staticData = assign({}, options.static); + var staticData = cloneDeep(options.static); var id = staticData[resource.idField]; if (!id) throw new Error( @@ -204,9 +207,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, staticData)); + resolve(assign({}, data, cloneDeep(staticData))); } else { - resolve(assign({}, staticData)); + resolve(assign({}, cloneDeep(staticData))); } }).fail(reject); }); |
