diff --git a/entity/lib/ops/hydrate.js b/entity/lib/ops/hydrate.js index 9419709..b7c6176 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 @@ -62,8 +63,13 @@ 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)) { - ops[c.model_type] = factory_model_list(app, content_ids, c.model_type, depth); + 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.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); } } }); @@ -78,7 +84,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']; @@ -97,16 +103,21 @@ var factory_model_list = function (app, content_ids, model_type, depth) { var ops = _.map(results, function (m) { + //we only want to recurse active records return function (cbh) { - if (!m) { + debug('m.attrs are:', m.attrs); + + if (!m || !m.attrs.active) { return cbh(null, null); } app_plugin.hydrate(m, { - depth: depth - 1 + depth: depth - 1, + types: types }, cbh); }; + }); return async.parallel(ops, cb); diff --git a/model/index.js b/model/index.js index 69fd825..39edd96 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'; @@ -208,51 +208,35 @@ 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; - - 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; + self.db = db_connection.use(couch_options.database); + + 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 +}); diff --git a/model/lib/model.js b/model/lib/model.js index f4a6ea0..30011ed 100644 --- a/model/lib/model.js +++ b/model/lib/model.js @@ -39,7 +39,7 @@ var Model = module.exports = BasePlugin.extend({ _.each(this.history, function (row) { if (!timestamp || timestamp > row.updated_date) { - record = _.extend(record || {}, row); + record = _.merge(record || {}, row); } }); diff --git a/model/lib/omit_nulls.js b/model/lib/omit_nulls.js index 79dc82e..5baa3c2 100644 --- a/model/lib/omit_nulls.js +++ b/model/lib/omit_nulls.js @@ -1,7 +1,13 @@ var _ = require('lodash'); -module.exports = function(obj) { - return _.omit(obj || {}, function(value) { - return value === null; +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; + } + }); }; diff --git a/package.json b/package.json index 2db2763..22d7114 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphiti", - "version": "1.2.1", + "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" @@ -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",