From 7c8239bbcdc5a87834d7b967580d965e18eb026d Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Thu, 28 May 2015 01:57:38 -0500 Subject: [PATCH 01/16] lazy insert method of checking for db existence. --- model/index.js | 63 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/model/index.js b/model/index.js index 69fd825..3e53ba5 100644 --- a/model/index.js +++ b/model/index.js @@ -56,7 +56,7 @@ module.exports = BasePlugin.extend({ // generate the list of properties for this model. this.properties = {}; - + _.each(this.schema.properties, function (spec, name) { var ptype = 'text'; @@ -209,50 +209,33 @@ module.exports = BasePlugin.extend({ var db_connection = create_couch_connection(couch_options); var self = this; - if(ensure_db){ - var setup_ops = [ - function (cb) { - // check if db exists.. create if not. - db_connection.db.list(function (err, body) { - - if (err) { - err.message = 'db.list(): ' + err.message; - cb(err); - return; - } - // body is an array - var db = _.find(body, function (db) { - return db === couch_options.database; - }); - if (db) { - self.db = db_connection.use(couch_options.database); - cb(); - return; + if (ensure_db) { + // trying to ensure the design docs - if it fails create db. + ensure_design_documents(self, function (err, data) { + + if (err) { + db_connection.db.create(couch_options.database, function (err, body) { + + //If the error is that the database exists already then we are okay + if (err && err.error != 'file_exists') { + // return error if db create fail + err.stack = 'db.create(): Database (' + couch_options.database + ') not found and could not be created. \n' + err.stack; + done(err); + } else { + //we've now created the db so ensure dds & done + ensure_design_documents(self, done); } - db_connection.db.create(couch_options.database, function (err, body) { - //If the error is that the database exists already then we are okay - if (err && err.error != 'file_exists') { - // return error if db create fail - - err.stack = 'db.create(): Database (' + couch_options.database + ') not found and could not be created. \n' + err.stack; - cb(err); - } else { - self.db = db_connection.use(couch_options.database); - cb(); - } - }); }); - }, - - ensure_design_documents.bind(null, self) - ]; + } else { + //good to go, callback + done(); + } + }); - async.series(setup_ops, done); - } - else{ + } else { self.db = db_connection.use(couch_options.database); done(null); } } -}); \ No newline at end of file +}); From af7f89bde8c47eb7950149ae1229b24677fafb6e Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Thu, 28 May 2015 02:02:02 -0500 Subject: [PATCH 02/16] v1.2.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2db2763..3c6c500 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.1", + "version": "1.2.2", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 73c68643ffc67af1fa9bb2c3b670dc1fc19b721b Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Thu, 28 May 2015 02:14:41 -0500 Subject: [PATCH 03/16] v1.2.3 --- model/index.js | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/model/index.js b/model/index.js index 3e53ba5..39edd96 100644 --- a/model/index.js +++ b/model/index.js @@ -208,6 +208,7 @@ module.exports = BasePlugin.extend({ // NOTE: "this" is the plugin itself. no need to use the getter/setter here. Only on the interface exposed to users. var db_connection = create_couch_connection(couch_options); var self = this; + self.db = db_connection.use(couch_options.database); if (ensure_db) { // trying to ensure the design docs - if it fails create db. diff --git a/package.json b/package.json index 3c6c500..5412bd9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.2", + "version": "1.2.3", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 09c25c3be8ae435f740fc7637c1eab9af2116a69 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Thu, 28 May 2015 11:28:49 -0500 Subject: [PATCH 04/16] v1.2.4, need to omit nulls within arrays too --- model/lib/omit_nulls.js | 9 +++++++-- package.json | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/model/lib/omit_nulls.js b/model/lib/omit_nulls.js index 79dc82e..03df7f3 100644 --- a/model/lib/omit_nulls.js +++ b/model/lib/omit_nulls.js @@ -1,7 +1,12 @@ var _ = require('lodash'); -module.exports = function(obj) { - return _.omit(obj || {}, function(value) { +var omit_nulls = module.exports = function (obj) { + return _.omit(obj || {}, function (value) { + + if (value instanceof Array) { + value = omit_nulls(value); + } + return value === null; }); }; diff --git a/package.json b/package.json index 5412bd9..e22a0a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.3", + "version": "1.2.4", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 406e034d79e308cd608befda0e4e7d910bbe493b Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Thu, 28 May 2015 11:53:35 -0500 Subject: [PATCH 05/16] v1.2.5, else wrapper for non-recursive return --- model/lib/omit_nulls.js | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/model/lib/omit_nulls.js b/model/lib/omit_nulls.js index 03df7f3..5baa3c2 100644 --- a/model/lib/omit_nulls.js +++ b/model/lib/omit_nulls.js @@ -2,11 +2,12 @@ var _ = require('lodash'); var omit_nulls = module.exports = function (obj) { return _.omit(obj || {}, function (value) { - + //if this is an array - dig deeper if (value instanceof Array) { value = omit_nulls(value); + } else { + return value === null; } - return value === null; }); }; diff --git a/package.json b/package.json index e22a0a4..f296e47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.4", + "version": "1.2.5", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 0b0dc23c0f2431c14d4f8b00b49c568c4b3d8e01 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Sun, 4 Oct 2015 19:37:56 -0500 Subject: [PATCH 06/16] added the passing of types to the recursive graph hydrating. --- entity/lib/ops/hydrate.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index 9419709..c2916b0 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -63,7 +63,7 @@ module.exports = function (entity, hydrate_options, callback) { // prevent infinite recursion & ensure we only add the fn to ops 1 time. if (!ops[c.model_type] && !(entity.attrs.id === c.model_id && entity.model_type === c.model_type)) { - ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth); + ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, types); } } }); @@ -78,7 +78,7 @@ module.exports = function (entity, hydrate_options, callback) { }; // generates a function for multi-get in hydrate. -var factory_model_list = function (app, content_ids, model_type, depth) { +var factory_model_list = function (app, content_ids, model_type, depth, types) { var app_plugin = app[model_type] ? app[model_type] : app[model_type + 's']; @@ -103,7 +103,8 @@ var factory_model_list = function (app, content_ids, model_type, depth) { } app_plugin.hydrate(m, { - depth: depth - 1 + depth: depth - 1, + types: types }, cbh); }; From f6c02388cb4fca8fc6cf051589be7246f4524342 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Sun, 4 Oct 2015 19:39:42 -0500 Subject: [PATCH 07/16] v1.2.6 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f296e47..d4a39bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.5", + "version": "1.2.6", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From e1c198a4ce1bfb478967ca43f802e6b09439ee73 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Sun, 4 Oct 2015 20:41:10 -0500 Subject: [PATCH 08/16] v1.2.7 --- entity/lib/ops/hydrate.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index c2916b0..cd2fcbd 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -63,7 +63,7 @@ module.exports = function (entity, hydrate_options, callback) { // prevent infinite recursion & ensure we only add the fn to ops 1 time. if (!ops[c.model_type] && !(entity.attrs.id === c.model_id && entity.model_type === c.model_type)) { - ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, types); + ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, hydrate_types); } } }); diff --git a/package.json b/package.json index d4a39bf..5f080c7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.6", + "version": "1.2.7", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 8381c2a2a99432f4edd417b6145a364cb902c748 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Tue, 1 Mar 2016 11:29:00 -0600 Subject: [PATCH 09/16] recurse only active records, added debug statements for hydration. --- entity/lib/ops/hydrate.js | 31 ++++++++++++++++++++----------- package.json | 1 + 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index cd2fcbd..5fb8cbd 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -1,5 +1,6 @@ var async = require('async'); var _ = require('lodash'); +var debug = require('debug')('graphiti'); /* * hydrate_options.types {Array} - List of types to hydrate @@ -63,7 +64,12 @@ module.exports = function (entity, hydrate_options, callback) { // prevent infinite recursion & ensure we only add the fn to ops 1 time. if (!ops[c.model_type] && !(entity.attrs.id === c.model_id && entity.model_type === c.model_type)) { - ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, hydrate_types); + + debug("c.model_type is:", c.model_type); + debug("entity.model_type is:", entity.model_type); + debug("entity.attrs.id:", entity.attrs.id); + + ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, types); } } }); @@ -97,16 +103,19 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { var ops = _.map(results, function (m) { - return function (cbh) { - if (!m) { - return cbh(null, null); - } - - app_plugin.hydrate(m, { - depth: depth - 1, - types: types - }, cbh); - }; + //we only want to recurse active records + if (m.active) { + return function (cbh) { + if (!m) { + return cbh(null, null); + } + debug('recursively hydrating: ', model_type, _.pick(m, ["id"])); + app_plugin.hydrate(m, { + depth: depth - 1, + types: types + }, cbh); + }; + } }); diff --git a/package.json b/package.json index 5f080c7..8b63343 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "dependencies": { "JSONStream": "^0.10.0", "async": "^0.9.0", + "debug": "^2.2.0", "deep-equal": "^1.0.0", "jsonschema": "^1.0.0", "lodash": "^3.3.1", From c52b8bf40321d765678cc98c5cc260abef324167 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Tue, 1 Mar 2016 11:50:26 -0600 Subject: [PATCH 10/16] fix on entity.type, not entity.model_type. --- entity/lib/ops/hydrate.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index 5fb8cbd..7847892 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -63,10 +63,10 @@ module.exports = function (entity, hydrate_options, callback) { content_ids[c.model_type].push(c.model_id); // prevent infinite recursion & ensure we only add the fn to ops 1 time. - if (!ops[c.model_type] && !(entity.attrs.id === c.model_id && entity.model_type === c.model_type)) { + if (!ops[c.model_type] && !(entity.attrs.id === c.model_id && entity.type === c.model_type)) { debug("c.model_type is:", c.model_type); - debug("entity.model_type is:", entity.model_type); + debug("entity.type is:", entity.type); debug("entity.attrs.id:", entity.attrs.id); ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth, types); @@ -95,6 +95,7 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { return cb(new Error("content model_type is not set or model_type plugin does not exist on app. " + model_type), null); } + debug('getting content ids of ', content_ids.length); app_plugin.get(content_ids[model_type], function (err, results) { // entities get recursively hydrated, models already are. @@ -104,18 +105,19 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { var ops = _.map(results, function (m) { //we only want to recurse active records - if (m.active) { - return function (cbh) { - if (!m) { - return cbh(null, null); - } - debug('recursively hydrating: ', model_type, _.pick(m, ["id"])); - app_plugin.hydrate(m, { - depth: depth - 1, - types: types - }, cbh); - }; - } + debug('model is:', m); + + return function (cbh) { + if (!m || !m.active) { + return cbh(null, null); + } + debug('recursively hydrating: ', model_type, _.pick(m, ["id"])); + app_plugin.hydrate(m, { + depth: depth - 1, + types: types + }, cbh); + }; + }); From 29386fdc3ab5029e568e9ac26bd21cabcc3e057e Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Tue, 1 Mar 2016 11:57:26 -0600 Subject: [PATCH 11/16] have to dereference .attrs --- entity/lib/ops/hydrate.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index 7847892..883c76e 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -105,13 +105,15 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { var ops = _.map(results, function (m) { //we only want to recurse active records - debug('model is:', m); - return function (cbh) { - if (!m || !m.active) { + debug('m.attrs are:', m.attrs); + + if (!m || !m.attrs.active) { return cbh(null, null); } + debug('recursively hydrating: ', model_type, _.pick(m, ["id"])); + app_plugin.hydrate(m, { depth: depth - 1, types: types From 2977fa436b86bf5100c20268955935527e007b82 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Mon, 7 Mar 2016 07:47:25 -0600 Subject: [PATCH 12/16] removed debug statements. --- entity/lib/ops/hydrate.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index 883c76e..b7c6176 100644 --- a/entity/lib/ops/hydrate.js +++ b/entity/lib/ops/hydrate.js @@ -95,7 +95,6 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { return cb(new Error("content model_type is not set or model_type plugin does not exist on app. " + model_type), null); } - debug('getting content ids of ', content_ids.length); app_plugin.get(content_ids[model_type], function (err, results) { // entities get recursively hydrated, models already are. @@ -112,8 +111,6 @@ var factory_model_list = function (app, content_ids, model_type, depth, types) { return cbh(null, null); } - debug('recursively hydrating: ', model_type, _.pick(m, ["id"])); - app_plugin.hydrate(m, { depth: depth - 1, types: types From d6de5ba5dd69c19eac2838030f3b4d6270f2e2ec Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Mon, 7 Mar 2016 07:47:58 -0600 Subject: [PATCH 13/16] v1.2.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8b63343..80c8d4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.7", + "version": "1.2.8", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 9b76e7ad84ec9bb7e311e9af6ea8e5131ced1743 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Wed, 18 May 2016 09:18:54 -0500 Subject: [PATCH 14/16] no message --- model/lib/model.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/model/lib/model.js b/model/lib/model.js index f4a6ea0..01f9c76 100644 --- a/model/lib/model.js +++ b/model/lib/model.js @@ -39,6 +39,11 @@ var Model = module.exports = BasePlugin.extend({ _.each(this.history, function (row) { if (!timestamp || timestamp > row.updated_date) { + + if (row.hasOwnProperty("base_entity_graph") && row.base_entity_graph.length == 0) { + row = _.without(row, "base_entity_graph"); + } + record = _.extend(record || {}, row); } }); From 639f9727168bb734568d2586e367e7a9d0ce74c8 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Wed, 18 May 2016 09:19:27 -0500 Subject: [PATCH 15/16] v1.2.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 80c8d4c..5e62997 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.8", + "version": "1.2.9", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js" From 0a18bcec74f368d6a2d637812176e3efd79913f9 Mon Sep 17 00:00:00 2001 From: Michael Angstadt Date: Wed, 18 May 2016 10:31:14 -0500 Subject: [PATCH 16/16] updated based on feedback --- model/lib/model.js | 7 +------ package.json | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/model/lib/model.js b/model/lib/model.js index 01f9c76..30011ed 100644 --- a/model/lib/model.js +++ b/model/lib/model.js @@ -39,12 +39,7 @@ var Model = module.exports = BasePlugin.extend({ _.each(this.history, function (row) { if (!timestamp || timestamp > row.updated_date) { - - if (row.hasOwnProperty("base_entity_graph") && row.base_entity_graph.length == 0) { - row = _.without(row, "base_entity_graph"); - } - - record = _.extend(record || {}, row); + record = _.merge(record || {}, row); } }); diff --git a/package.json b/package.json index 5e62997..22d7114 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.9", + "version": "1.2.10", "main": "index.js", "scripts": { "test": "mocha -R spec -u tdd test/model/tests/*.js && mocha -R spec -u tdd test/entity/tests/*.js"