From 0d5f29cbb7b1ea4a2ce06cacbeace9f1af5bd4bf Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Sat, 20 Apr 2013 17:59:42 +0200 Subject: [PATCH] added ensureContent for importer --- mongodata.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/mongodata.js b/mongodata.js index c27d08a..3d881c6 100644 --- a/mongodata.js +++ b/mongodata.js @@ -16,6 +16,9 @@ module.exports = function (config) { config.errors && console.log('ERR2 getMongoContent', err); return callback(err); } + if (!options.query){ + return callback(new Error('Data not found ' + options.collection + '/ missing query')); + } if (options.query._id && !(options.query._id instanceof Object)) { try { options.query._id = new ObjectID.createFromHexString(options.query._id); @@ -38,6 +41,28 @@ module.exports = function (config) { }); } + function ensureContent(options, callback) { + if (!options.query){ + options.query = { + name: options.name + } + } + getMongoContent(options, function (err, result) { + if(err) { + if (/Data not found*/.test(err.message)) { + setMongoContent({name:options.name},options, function (err, result) { + if(err) { + return callback(err); + } + return callback(null,result); + }) + } else { + return callback(err); + } + } + return callback(null,result); + }) + } function getMongoAttribute(options, callback) { config.debug && console.log('getMongoAttribute options', options); return getMongoContent(options, function (err, result) { @@ -217,7 +242,8 @@ module.exports = function (config) { getMongoAttribute: getMongoAttribute, getMongoContent: getMongoContent, setMongoAttribute: setMongoAttribute, - setMongoContent: setMongoContent + setMongoContent: setMongoContent, + ensureContent: ensureContent }; };