Skip to content
21 changes: 16 additions & 5 deletions entity/lib/ops/hydrate.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
}
}
});
Expand All @@ -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'];

Expand All @@ -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);
Expand Down
66 changes: 25 additions & 41 deletions model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
}
}
});
});
2 changes: 1 addition & 1 deletion model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
12 changes: 9 additions & 3 deletions model/lib/omit_nulls.js
Original file line number Diff line number Diff line change
@@ -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;
}

});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"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"
},
"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",
Expand Down