summaryrefslogtreecommitdiff
path: root/app/node_modules/okresource
diff options
context:
space:
mode:
Diffstat (limited to 'app/node_modules/okresource')
-rw-r--r--app/node_modules/okresource/index.js64
-rw-r--r--app/node_modules/okresource/package.json15
2 files changed, 79 insertions, 0 deletions
diff --git a/app/node_modules/okresource/index.js b/app/node_modules/okresource/index.js
new file mode 100644
index 0000000..c46e7f4
--- /dev/null
+++ b/app/node_modules/okresource/index.js
@@ -0,0 +1,64 @@
+var Q = require('q');
+var joi = require('joi');
+var OKRestEndpoint = require('okrest');
+
+/**
+ * Creates an OKResource types
+ * Takes a resource name and a spec defining the resources attributes.
+ */
+function createResourceClass(name, spec, options) {
+ options = options || {};
+ spec = spec || {};
+ // 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 || '*';
+ // The meta resource is a special case
+ var meta = options.meta === undefined ? false : options.meta;
+ if (viewClass !== OKRestEndpoint && !(viewClass instanceof OKRestEndpoint))
+ throw new Error('Resource view not descendent of OKRestEndpoint');
+
+ /**
+ * OKResource!
+ */
+ function OKResource(options) {
+ if (!(this instanceof OKResource)) return new OKResource(options);
+ options = options || {};
+ this.name = name;
+ if (!name)
+ throw new Error('No resource type provided to resource!')
+ var db = this._db = options.db;
+ if (!db)
+ throw new Error('No DB provided to resource!');
+ this._validator = compileValidator(spec);
+ }
+
+ /**
+ * 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.get = function() {
+ // return this._query();
+ // };
+
+ // Expose the resource type on the class constructor
+ OKResource.type = name;
+
+ return OKResource;
+}
+
+/**
+ * Compiles our schema spec into a schema validator function
+ */
+function compileValidator(spec) {
+ // Skip validation for now
+ var schema = joi.any();
+ return schema.validate.bind(schema);
+}
+
+module.exports = createResourceClass;
diff --git a/app/node_modules/okresource/package.json b/app/node_modules/okresource/package.json
new file mode 100644
index 0000000..12fcd26
--- /dev/null
+++ b/app/node_modules/okresource/package.json
@@ -0,0 +1,15 @@
+{
+ "name": "okresource",
+ "version": "1.0.0",
+ "description": "really good",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "author": "OKFocus",
+ "license": "None",
+ "dependencies": {
+ "joi": "^6.0.8",
+ "q": "^1.2.0"
+ }
+}