summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/node_modules/okresource/index.js')
-rw-r--r--app/node_modules/okresource/index.js125
1 files changed, 54 insertions, 71 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
index 0e8498f..c3f9adb 100644
--- a/app/node_modules/okresource/index.js
+++ b/app/node_modules/okresource/index.js
@@ -20,17 +20,6 @@ function OKResource(options) {
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(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
- }, spec.id && 'id');
- if (!idField)
- throw new Error('Bad schema: no ID field');
var type = options.type;
this._db = options.db;
@@ -51,12 +40,6 @@ function OKResource(options) {
enumerable: true
});
- Object.defineProperty(this, 'idField', {
- value: idField,
- writable: false,
- enumerable: true
- });
-
// Whether this resource represents a specific data point
// or a whole class of data
Object.defineProperty(this, 'bound', {
@@ -74,19 +57,22 @@ OKResource.prototype.assertValid = function(data) {
};
OKResource.prototype.all = function() {
- return this._db.getAll(this.type);
+ return this._db.all(this.type);
};
-OKResource.prototype.create = function(data) {
+OKResource.prototype.getID = function(data) {
data = data || {};
+ return data[this._schema.idField];
+};
+
+OKResource.prototype.create = function(data) {
var type = this.type;
var db = this._db;
- var id = data[this.idField];
return Q.promise(function(resolve, reject) {
- if (!id) {
- reject(new Error('Data does not contain ID property'));
+ if (!data) {
+ reject(new Error('No data provided'));
} else {
- db.create(type, data).then(resolve).fail(reject);
+ db.insert(type, data).then(resolve).fail(reject);
}
});
};
@@ -94,13 +80,11 @@ OKResource.prototype.create = function(data) {
OKResource.prototype.destroy = function(id) {
var db = this._db;
var type = this.type;
- var query = {};
- query[this.idField] = id;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('No ID given'));
+ reject(new Error('No ID provided'));
} else {
- db.remove(type, query).then(resolve).fail(reject);
+ db.remove(type, id).then(resolve).fail(reject);
}
});
};
@@ -111,7 +95,7 @@ OKResource.prototype.find = function(query) {
var type = this.type;
return Q.promise(function(resolve, reject) {
if (!query) {
- throw new Error('No query given');
+ throw new Error('No query provided');
} else {
db.find(type, query).then(resolve).fail(reject);
}
@@ -121,53 +105,65 @@ OKResource.prototype.find = function(query) {
OKResource.prototype.get = function(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');
+ throw new Error('No ID provided');
} 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).fail(reject);
+ db.get(type, id).then(resolve).fail(reject);
}
});
};
OKResource.prototype.update = function(id, data) {
- data = data || {};
var db = this._db;
var type = this.type;
- var idField = this.idField;
return Q.promise(function(resolve, reject) {
if (!id) {
reject(new Error('No resource ID provided'));
+ } else if (!data) {
+ reject(new Error('No data provided'));
} else {
- var query = {};
- query[idField] = id;
- db.put(type, query, data).then(resolve).fail(reject);;
+ db.update(type, id, data).then(resolve).fail(reject);;
}
});
};
+OKResource.prototype.updateBatch = function(ids, datas) {
+ var self = this;
+ var db = this._db;
+ var type = this.type;
+ return Q.promise(function(resolve, reject) {
+ if (!ids || !ids.length || !datas || !datas.length ||
+ ids.length !== datas.length) {
+ reject(new Error('Bad input'));
+ } else {
+ db.updateBatch(type, ids, datas).then(resolve).fail(reject);
+ }
+ });
+}
+
+
+/**
+ * Get all documents in collection sorted by property,
+ * optionally in descending order
+ */
+OKResource.prototype.sortBy = function(prop, descend) {
+ return this._db.sortBy(this.type, prop, descend);
+};
+
OKResource.prototype.updateOrCreate = function(id, data) {
data = data || {};
var type = this.type;
var db = this._db;
- var idField = this.idField;
- var query = {};
- query[idField] = id;
return Q.promise(function(resolve, reject) {
if (!id) {
- reject(new Error('No resource ID provided'));
+ reject(new Error('No ID provided'));
} else {
- db.get(type, query).then(function(persisted) {
+ db.get(type, id).then(function(persisted) {
if (persisted) {
- db.put(type, query, data).then(resolve).fail(reject);
+ db.update(type, id, data).then(resolve).fail(reject);
} else {
- db.create(type, data).then(resolve).fail(reject);
+ db.insert(type, data).then(resolve).fail(reject);
}
}).fail(reject);
}
@@ -196,11 +192,15 @@ function OKResourceInstance(resource, options) {
// configuration, but may not actually be present
// in the database and need custom logic to handle this.
var staticData = cloneDeep(options.static);
- var id = staticData[resource.idField];
+ var id = resource.getID(staticData);
if (!id)
throw new Error(
'Cannot create static OKResourceInstance without an ID field');
+ this.getID = function() {
+ return id;
+ };
+
/**
* Ensure that static data is provided on get
*/
@@ -209,9 +209,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, cloneDeep(staticData)));
+ resolve(assign(data, cloneDeep(staticData)));
} else {
- resolve(assign({}, cloneDeep(staticData)));
+ resolve(cloneDeep(staticData));
}
}).fail(reject);
});
@@ -286,14 +286,9 @@ function OKResourceInstance(resource, options) {
});
Object.defineProperty(this, 'spec', {
- value: resource.spec,
- writable: false,
- enumerable: true
- });
-
- Object.defineProperty(this, 'id', {
- value: id,
- writable: false,
+ get: function() {
+ return resource.spec
+ },
enumerable: true
});
@@ -303,23 +298,11 @@ function OKResourceInstance(resource, options) {
enumerable: true
});
- Object.defineProperty(this, 'idField', {
- value: resource.idField,
- writable: false,
- enumerable: true
- });
-
Object.defineProperty(this, 'bound', {
value: true,
writable: false,
enumerable: true
});
-
- Object.defineProperty(this, 'class', {
- value: resource,
- writable: false,
- enumerable: true
- });
}
module.exports = OKResource;