From 5abbdf600396144fbbe32b97e83beaabf6ed5c39 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 21:40:38 -0400 Subject: Support autoincrement and custom ID field in OKSchema --- app/node_modules/okschema/index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'app/node_modules/okschema/index.js') diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 4b215d1..1528eab 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -69,8 +69,9 @@ function OKSchema(spec) { if (!spec) throw new Error('No spec provided to OKSchema'); spec = cloneDeep(spec); + var specKeys = Object.keys(spec); // Cache the mschema version of our spec - this._mschemaSpec = Object.keys(spec).reduce(function(cache, prop) { + this._mschemaSpec = specKeys.reduce(function(cache, prop) { // If custom type, return its parent spec var type = spec[prop].type; if (types[type]) { @@ -82,12 +83,45 @@ function OKSchema(spec) { return cache; }, {}); + // Find ID field + var idField; + specKeys.every(function(prop) { + if (prop === 'id' || spec[prop].id) { + idField = prop; + return false; + } else { + return true; + } + }); + + // Register autoincrement fields + // NOTE Does not work for nested fields + var autoIncrementFields = specKeys.reduce(function(arr, prop) { + if (spec[prop] === 'autoincrement') { + arr.push(prop); + } + return arr; + }, []); + Object.defineProperty(this, 'spec', { get: function() { return cloneDeep(spec); }, enumerable: true }); + + Object.defineProperty(this, 'idField', { + value: idField, + writable: true, + enumerable: true + }); + + Object.defineProperty(this, 'autoIncrementFields',{ + get: function() { + return cloneDeep(autoIncrementFields); + }, + enumerable: true + }); } OKSchema.prototype.assertValid = function(data) { -- cgit v1.2.3-70-g09d2 From fd0d777ee81219577ef9416fab7f985920c3ae29 Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 22:35:24 -0400 Subject: Fix autoincrement algo bug --- app/node_modules/okdb/index.js | 8 +++++--- app/node_modules/okdb/package.json | 1 + app/node_modules/okschema/index.js | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) (limited to 'app/node_modules/okschema/index.js') diff --git a/app/node_modules/okdb/index.js b/app/node_modules/okdb/index.js index c9dca99..7639ec6 100644 --- a/app/node_modules/okdb/index.js +++ b/app/node_modules/okdb/index.js @@ -1,8 +1,10 @@ -var Q = require('q'); +var assign = require('object-assign') var cloneDeep = require('lodash.clonedeep'); var isobject = require('lodash.isobject'); var format = require('util').format; var low = require('lowdb'); +var Q = require('q'); + low.mixin(low.mixin(require('underscore-db'))); /** @@ -146,10 +148,10 @@ FSDB.prototype.getMeta = function() { */ function autoincrement(wrapper, schema, data) { return schema.autoIncrementFields.reduce(function(data, field) { - var last = wrapper.chain().sort(field).last().value(); + var last = wrapper.chain().sort(field).first().value(); var index = last ? last[field] : -1; var incremented = {}; - incremented[field] = index + 1; + incremented[field] = (parseInt(index) + 1) % Number.MAX_SAFE_INT; return assign(data, incremented); }, data); } diff --git a/app/node_modules/okdb/package.json b/app/node_modules/okdb/package.json index a6d13ff..5184cb6 100644 --- a/app/node_modules/okdb/package.json +++ b/app/node_modules/okdb/package.json @@ -12,6 +12,7 @@ "lodash.clonedeep": "^3.0.0", "lodash.isobject": "^3.0.1", "lowdb": "^0.7.2", + "object-assign": "^2.0.0", "q": "^1.2.0", "underscore-db": "^0.8.1" } diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 1528eab..4633e7a 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -97,7 +97,8 @@ function OKSchema(spec) { // Register autoincrement fields // NOTE Does not work for nested fields var autoIncrementFields = specKeys.reduce(function(arr, prop) { - if (spec[prop] === 'autoincrement') { + var specProp = spec[prop]; + if (specProp.autoincrement) { arr.push(prop); } return arr; -- cgit v1.2.3-70-g09d2 From 1eff07c9c2ca5b61a28a1037a586d25c3791d67b Mon Sep 17 00:00:00 2001 From: Sean Fridman Date: Fri, 10 Apr 2015 22:37:33 -0400 Subject: Add autoincrementing index to resources --- app/index.js | 2 ++ app/node_modules/okschema/index.js | 5 +++++ 2 files changed, 7 insertions(+) (limited to 'app/node_modules/okschema/index.js') diff --git a/app/index.js b/app/index.js index fc38b0a..e462b48 100644 --- a/app/index.js +++ b/app/index.js @@ -122,6 +122,8 @@ OKCMS.prototype._createSchemas = function(schemaConfig) { schemaConfig = schemaConfig || {}; return Object.keys(schemaConfig).reduce(function(cache, key) { var spec = schemaConfig[key]; + // All resources have an autoincrementing index so we can order them suckas + spec.__index = {type: 'meta', autoincrement: true}; cache[key] = OKSchema(spec); return cache; }, {}); diff --git a/app/node_modules/okschema/index.js b/app/node_modules/okschema/index.js index 4633e7a..d53ed7b 100644 --- a/app/node_modules/okschema/index.js +++ b/app/node_modules/okschema/index.js @@ -56,6 +56,11 @@ var types = { }]; } } + }, + // Special type for resource meta information + 'meta': { + parent: 'string', + assertValid: function(spec, value) {} } } -- cgit v1.2.3-70-g09d2