diff --git a/lib/data.js b/lib/data.js index 7b0e4d5..492275b 100644 --- a/lib/data.js +++ b/lib/data.js @@ -28,12 +28,12 @@ module.exports = function (config, dataBase, updateShare) { return callback && callback(null, result, col); }; - return getRethinkContent(options, function document(err, result, col) { + return dataBase.getContent(options, function document(err, result, col) { if (err) { if (/Data not found*/.test(err.message)) { var documentId = 'json:' + options.collection + ':' + options.name; var data = {name: options.name}; - return dataBase.setRethinkContent(data, options, function (err, content_result, col) { + return dataBase.setContent(data, options, function (err, content_result, col) { var keys = _.keys(data); // reset all attributes; return updateShare.document(documentId, data, keys, function updatedShareDocument() { stopEnsuring(err, content_result, col); @@ -53,11 +53,11 @@ module.exports = function (config, dataBase, updateShare) { /* options: * no_share (optional): prevent share from updating itself. */ - function setRethinkAttribute(data, options, callback) { - config.debug && config.debug('setMongoAttribute options', options); + function setAttribute(data, options, callback) { + config.debug && config.debug('setAttribute options', options); return ensureContent(options, function document(err, result, col) { if (err) { - config.error && config.error('ERR1 setMongoAttribute', err); + config.error && config.error('ERR1 setAttribute', err); return callback && callback(err); } var attribute_options = { @@ -77,10 +77,10 @@ module.exports = function (config, dataBase, updateShare) { } - config.debug && config.debug('getMongoAttribute parent found, get child and save', result, attribute_options); + config.debug && config.debug('setAttribute parent found, get child and save', result, attribute_options); return ensureContent(attribute_options, function attribute(err, attribute_result) { if (err) { - config.error && config.error('ERR2 setMongoAttribute ensureContent', err); + config.error && config.error('ERR2 setAttribute ensureContent', err); return callback && callback(err); } var updateContent = true; @@ -102,7 +102,7 @@ module.exports = function (config, dataBase, updateShare) { } return dataBase.saveData(col, attribute_result, function saved(err) { if (err) { - config.error && config.error('ERR3 setMongoAttribute', err); + config.error && config.error('ERR3 setAttribute', err); return callback && callback(err); } var documentId = 'json:' + options.collection + ':' + result.name; @@ -124,7 +124,7 @@ module.exports = function (config, dataBase, updateShare) { if (updateContent) { dataBase.updateData(col, result, function saved(err) { if (err) { - config.error && config.error('ERR3 setMongoAttribute', err); + config.error && config.error('ERR3 setAttribute', err); return callback && callback(err); } var path = [options.attribute, 'guid']; // reset just guid attribute; @@ -142,14 +142,9 @@ module.exports = function (config, dataBase, updateShare) { } return { - getMongoAttribute: dataBase.getAttribute, - getMongoContent: dataBase.getContent, - setMongoAttribute: setRethinkAttribute, - setMongoContent: dataBase.setContent, - getAttribute: dataBase.getAttribute, getContent: dataBase.getContent, - setAttribute: setRethinkAttribute, + setAttribute: setAttribute, setContent: dataBase.setContent, ensureContent: ensureContent }; diff --git a/lib/handlers.js b/lib/handlers.js index 301c075..6c6e774 100644 --- a/lib/handlers.js +++ b/lib/handlers.js @@ -1,12 +1,12 @@ 'use strict'; -module.exports = function (mongoDataInstance, previewInstance, importerInstance) { +module.exports = function (dataInstance, previewInstance, importerInstance) { function getAttribute(options, callback) { - return mongoDataInstance.getMongoAttribute(options, callback); + return dataInstance.getAttribute(options, callback); } function getContent(options, callback) { - return mongoDataInstance.getMongoContent(options, callback); + return dataInstance.getContent(options, callback); } function getPreviewHTML(html, options, callback) { diff --git a/lib/importer.js b/lib/importer.js index f2522dd..8fd9d76 100644 --- a/lib/importer.js +++ b/lib/importer.js @@ -5,7 +5,7 @@ var path = require('path'); var fs = require('fs'); -module.exports = function (config, mongoInstance, helpers, markers) { +module.exports = function (config, dataInstance, helpers, markers, updateShare) { var replaceMarkers; function handleImportMarkers(doc, options, callback) { @@ -52,7 +52,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { return cb && cb(err); } if (leftover) { - return mongoInstance.ensureContent(leftover.replacement, function parent(err, parent_result) { + return dataInstance.ensureContent(leftover.replacement, function parent(err, parent_result) { if (err) { config.error && config.error('ERR importer.importer ensureContent', err); return cb && cb(err); @@ -61,9 +61,9 @@ module.exports = function (config, mongoInstance, helpers, markers) { leftover.replacement.query[config.database.pk] = parent_result[config.database.pk]; remainder = remainder.replace(leftover.regExp, ''); - return mongoInstance.setMongoAttribute(remainder, leftover.replacement, function savedAttribute(err) { + return dataInstance.setAttribute(remainder, leftover.replacement, function savedAttribute(err) { if (err) { - config.error && config.error('ERR importer.importer setMongoAttribute', err); + config.error && config.error('ERR importer.importer setAttribute', err); return cb && cb(err); } return cb && cb(null, remainder); @@ -83,7 +83,7 @@ module.exports = function (config, mongoInstance, helpers, markers) { function importRemainder(context, result, remainder, callback) { - return mongoInstance.ensureContent(context, function parent(err, parent_result) { + return dataInstance.ensureContent(context, function parent(err, parent_result) { if (err) { config.error && config.error('ERR importer.importer ensureContent', err); return callback && callback(err); @@ -109,23 +109,23 @@ module.exports = function (config, mongoInstance, helpers, markers) { } _.extend(parent_result, data); context.update = true; - return mongoInstance.setMongoContent(parent_result, context, function (err) { + return dataInstance.setContent(parent_result, context, function (err) { if (err) { - config.error && config.error('ERR importer.importRemainder setMongoContent', err); + config.error && config.error('ERR importer.importRemainder setContent', err); return callback && callback(err); } var documentId = 'json:' + context.collection + ':' + context.name; var keys = _.keys(parent_result); // reset all attributes; - return mongoInstance.updateShareDocument(documentId, parent_result, keys, function () { + return updateShare.document(documentId, parent_result, keys, function () { return replaceWithEmptyContent(null); }); }); } context.query = {}; context.query[config.database.pk] = parent_result[config.database.pk]; - return mongoInstance.setMongoAttribute(remainder, context, function savedAttribute(err) { + return dataInstance.setAttribute(remainder, context, function savedAttribute(err) { if (err) { - config.error && config.error('ERR2 importer.importer setMongoAttribute', err); + config.error && config.error('ERR2 importer.importer setAttribute', err); return callback && callback(err); } return replaceWithEmptyContent(null); diff --git a/lib/preview.js b/lib/preview.js index 1729da4..038b0a9 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -5,7 +5,7 @@ var _ = require('underscore'); var less = require('less'); var when = require('when'); -module.exports = function (config, mongoDataInstance, helpers, markers) { +module.exports = function (config, dataInstance, helpers, markers) { var getPreviewHTML; var script_tag = markers.script_tag; @@ -91,14 +91,14 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { query: {name: parts[5]}, req: options.req }; - return mongoDataInstance.getMongoContent(context, function handleContext(err, context_result) { + return dataInstance.getContent(context, function handleContext(err, context_result) { if (err) { - config.error && config.error('ERR template_tag getMongoContent', context); + config.error && config.error('ERR template_tag getContent', context); return callback && callback(err); } - return mongoDataInstance.getMongoAttribute(template, function handleTemplate(err, template_result) { + return dataInstance.getAttribute(template, function handleTemplate(err, template_result) { if (err) { - config.error && config.error('ERR template_tag getMongoAttribute', template, err); + config.error && config.error('ERR template_tag getAttribute', template, err); return callback && callback(err); } var compiled_template = null; @@ -161,9 +161,9 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { }; attribute_context.query[config.database.pk] = context_result[config.database.pk]; - return mongoDataInstance.getMongoAttribute(attribute_context, function cacheTemplateKey(err, template_key_result) { + return dataInstance.getAttribute(attribute_context, function cacheTemplateKey(err, template_key_result) { if (err) { - config.error && config.error('ERR handlebar.registerHelper getMongoAttribute', err); + config.error && config.error('ERR handlebar.registerHelper getAttribute', err); deferred.reject(err); } var value = template_key_result[key]; @@ -214,7 +214,7 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { attribute: parts[3], query: {name: parts[2]} }; - return mongoDataInstance.getMongoAttribute(attribute, function handleMarkdownContent(err, markdown_result) { + return dataInstance.getAttribute(attribute, function handleMarkdownContent(err, markdown_result) { if (err) { return callback && callback(err); } diff --git a/lib/shareHandlers.js b/lib/shareHandlers.js index 06cb4de..ada0616 100644 --- a/lib/shareHandlers.js +++ b/lib/shareHandlers.js @@ -1,13 +1,13 @@ 'use strict'; -module.exports = function (config, model, mongoDataInstance) { +module.exports = function (config, model, dataInstance) { var timers = {}; - function handleMongoGetResult(options) { + function handleGetResult(options) { function handleResult(err, result) { var notFound = false; if (err) { if (/Data not found*/.test(err.message)) { - config.debug && config.debug('handleMongoGetResult.handleResult Document/Attribute not found, It will be created on first OT'); + config.debug && config.debug('handleGetResult.handleResult Document/Attribute not found, It will be created on first OT'); result = {}; if (options.attribute) { if (options.type === 'json') { @@ -18,12 +18,12 @@ module.exports = function (config, model, mongoDataInstance) { } notFound = true; } else { - config.error && config.error('ERR1 handleMongoGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); + config.error && config.error('ERR1 handleGetResult.handleResult Error retrieving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); } } if (result || notFound) { var operation = null; - config.debug && config.debug('handleMongoGetResult options', options, result); + config.debug && config.debug('handleGetResult options', options, result); var data = result; if (options.attribute) { data = result[options.attribute]; @@ -49,9 +49,9 @@ module.exports = function (config, model, mongoDataInstance) { } if (operation) { model.applyOp(options.documentId, operation, function appliedOp(error, version) { - config.debug && config.debug('getResult applyOp version', version); + config.debug && config.debug('handleGetResult applyOp version', version); if (error) { - config.error && config.error('ERR2 handleMongoGetResult', error); + config.error && config.error('ERR2 handleGetResult', error); } }); } @@ -73,18 +73,18 @@ module.exports = function (config, model, mongoDataInstance) { if (splitId.length === 4) { options.query = {name: splitId[2]}; options.attribute = splitId[3]; - mongoDataInstance.getMongoAttribute(options, handleMongoGetResult(options)); + dataInstance.getAttribute(options, handleGetResult(options)); } else { options.query = {name: splitId[2]}; - mongoDataInstance.getMongoContent(options, handleMongoGetResult(options)); + dataInstance.getContent(options, handleGetResult(options)); } }); - function handleMongoSetResult(options, current, callback) { + function handleSetResult(options, current, callback) { function handleResult(err, result) { if (err) { - config.error && config.error('ERR1 handleMongoSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); + config.error && config.error('ERR1 handleSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); return callback && callback(err); } config.debug && config.debug('current', current, 'result', result, 'options', options); @@ -97,9 +97,9 @@ module.exports = function (config, model, mongoDataInstance) { v: options.operation.v }; model.applyOp(options.documentId, operation, function appliedOp(error, version) { - config.debug && config.debug('setResult applyOp version', version); + config.debug && config.debug('handleSetResult applyOp version', version); if (error) { - config.error && config.error('ERR2 handleMongoSetResult', error); + config.error && config.error('ERR2 handleSetResult', error); return callback && callback(error); } return callback && callback(null, version); @@ -112,10 +112,10 @@ module.exports = function (config, model, mongoDataInstance) { return handleResult; } - function handleMongoAttributeSetResult(options, current, callback) { + function handleAttributeSetResult(options, current, callback) { function handleResult(err, result) { if (err) { - config.error && config.error('ERR1 handleMongoAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); + config.error && config.error('ERR1 handleAttributeSetResult Error while saving document ', options.collection, JSON.stringify(options.query), options.attribute || '', err); return callback && callback(err); } config.debug && config.debug('current', current, 'result', result); @@ -131,8 +131,8 @@ module.exports = function (config, model, mongoDataInstance) { var args = timers[documentId]; delete timers[documentId]; config.debug && config.debug('running timer', documentId); - mongoDataInstance.setMongoContent(args.current, args.options, - handleMongoSetResult(args.options, args.current, + dataInstance.setContent(args.current, args.options, + handleSetResult(args.options, args.current, function handleApplyOpResult(err, version) { if (err) { config.error && config.error('ERR2 applyOp', version, err); @@ -150,8 +150,8 @@ module.exports = function (config, model, mongoDataInstance) { if (args.options.type === 'json') { data = JSON.parse(args.current); } - mongoDataInstance.setMongoAttribute(data, args.options, - handleMongoAttributeSetResult(args.options, data, + dataInstance.setAttribute(data, args.options, + handleAttributeSetResult(args.options, data, function handleApplyOpResult(err, version) { if (err) { config.error && config.error('ERR1 applyOp', documentId, version, err); diff --git a/server.js b/server.js index 136eef8..3c5cf7b 100644 --- a/server.js +++ b/server.js @@ -236,7 +236,7 @@ rethink.connect(config.rethink.server, function connection_result(err, connectio config.debug && config.debug('previews initialized'); - var importerInstance = importer(config, dataInstance, helperInstance, markerInstance); + var importerInstance = importer(config, dataInstance, helperInstance, markerInstance, updateShare); config.debug && config.debug('importer initialized'); diff --git a/test/test.rethinkData.js b/test/test.rethinkData.js index 1ff74ff..a11f068 100644 --- a/test/test.rethinkData.js +++ b/test/test.rethinkData.js @@ -71,7 +71,7 @@ describe('rethinkData', function () { function testArguments(options) { return function (done) { - dataInstance.getMongoContent(options, function (err, result) { + dataInstance.getContent(options, function (err, result) { if (result === 'ok') { expect(result).to.equal('ok'); expect(err).to.not.be.ok; @@ -179,12 +179,12 @@ describe('rethinkData', function () { attribute: 'content_attribute' } ]; - var mongoDataInstance = rethinkData(config, db, shareModel); + var dataInstance = rethinkData(config, db, shareModel); var i; function testArguments(options) { return function (done) { - mongoDataInstance.getMongoAttribute(options, function (err, result) { + dataInstance.getAttribute(options, function (err, result) { if (options.collection === 'ok') { //console.log('result',result,'err',err,'coll',coll); expect(result).to.equal(ok);