From 7b71cd1bde2879556a4dae2735a25f191289d601 Mon Sep 17 00:00:00 2001 From: Aiko Mastboom Date: Fri, 3 Jul 2015 17:35:37 +0200 Subject: [PATCH] allow for different PK between databases --- lib/data.js | 16 ++++++++-------- lib/importer.js | 10 ++++++---- lib/mongoData.js | 3 +++ lib/preview.js | 7 +++++-- lib/rethinkData.js | 3 +++ lib/routes.js | 9 ++++++--- lib/shareHandlers.js | 2 -- lib/updateShareDocument.js | 2 +- server.js | 4 ++++ 9 files changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/data.js b/lib/data.js index 82aa4ab..7b0e4d5 100644 --- a/lib/data.js +++ b/lib/data.js @@ -66,12 +66,12 @@ module.exports = function (config, dataBase, updateShare) { }; if (result.hasOwnProperty(options.attribute) && result[options.attribute].guid) { - attribute_options.query = { - id: result[options.attribute].guid - }; + attribute_options.query = {}; + attribute_options.query[config.database.pk] = result[options.attribute].guid; + } else { attribute_options.query = { - parent: result.id, + parent: result[config.database.pk], name: result.name + '.' + options.attribute }; @@ -85,16 +85,16 @@ module.exports = function (config, dataBase, updateShare) { } var updateContent = true; if (result[options.attribute]) { - if (attribute_result.id === String(result[options.attribute].guid)) { + if (attribute_result[config.database.pk] === String(result[options.attribute].guid)) { updateContent = false; } else { - result[options.attribute].guid = attribute_result.id; + result[options.attribute].guid = attribute_result[config.database.pk]; } } else { - result[options.attribute] = {guid: attribute_result.id}; + result[options.attribute] = {guid: attribute_result[config.database.pk]}; } - attribute_result.parent = result.id; + attribute_result.parent = result[config.database.pk]; attribute_result.name = result.name + '.' + options.attribute; attribute_result[options.attribute] = data; if (options.operation) { diff --git a/lib/importer.js b/lib/importer.js index 659d7ad..f2522dd 100644 --- a/lib/importer.js +++ b/lib/importer.js @@ -57,7 +57,8 @@ module.exports = function (config, mongoInstance, helpers, markers) { config.error && config.error('ERR importer.importer ensureContent', err); return cb && cb(err); } - leftover.replacement.query = {id: parent_result.id}; + leftover.replacement.query = {}; + 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) { @@ -103,8 +104,8 @@ module.exports = function (config, mongoInstance, helpers, markers) { config.error && config.error('ERR importer.replaceMarkers JSON.parse(remainder)', remainder, error); return callback && callback(error); } - if (data.id) { - delete data.id; + if (data[config.database.pk]) { + delete data[config.database.pk]; } _.extend(parent_result, data); context.update = true; @@ -120,7 +121,8 @@ module.exports = function (config, mongoInstance, helpers, markers) { }); }); } - context.query = {id: parent_result.id}; + context.query = {}; + context.query[config.database.pk] = parent_result[config.database.pk]; return mongoInstance.setMongoAttribute(remainder, context, function savedAttribute(err) { if (err) { config.error && config.error('ERR2 importer.importer setMongoAttribute', err); diff --git a/lib/mongoData.js b/lib/mongoData.js index 5a91a06..9550dc5 100644 --- a/lib/mongoData.js +++ b/lib/mongoData.js @@ -4,6 +4,9 @@ var _ = require('underscore'); module.exports = function (config, db) { + + // config.database.pk = '_id' + /* * options: * collection (mandatory) diff --git a/lib/preview.js b/lib/preview.js index 0bb627f..1729da4 100644 --- a/lib/preview.js +++ b/lib/preview.js @@ -157,8 +157,10 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { var attribute_context = { collection: context.collection, attribute: key, - query: {id: context_result.id} + query: {} }; + attribute_context.query[config.database.pk] = context_result[config.database.pk]; + return mongoDataInstance.getMongoAttribute(attribute_context, function cacheTemplateKey(err, template_key_result) { if (err) { config.error && config.error('ERR handlebar.registerHelper getMongoAttribute', err); @@ -180,7 +182,8 @@ module.exports = function (config, mongoDataInstance, helpers, markers) { return callback && callback(err); } config.debug && config.debug('// handle markers on rendered template'); - context.query = {id: context_result.id}; + context.query = {}; + context.query[config.database.pk] = context_result[config.database.pk]; return getPreviewHTML(rendered, context, function handlePreviewResult(err, preview_html) { if (err) { config.error && config.error('ERR template_tag getPreviewHTML', err); diff --git a/lib/rethinkData.js b/lib/rethinkData.js index 000e89f..f322eaa 100644 --- a/lib/rethinkData.js +++ b/lib/rethinkData.js @@ -2,6 +2,9 @@ var _ = require('underscore'); module.exports = function (config, r, connection) { + + // config.database.pk = 'id' + /* * options: * collection (mandatory) diff --git a/lib/routes.js b/lib/routes.js index ebe81e0..a896247 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -14,8 +14,9 @@ module.exports = function (app, handlers, markers, config) { collection: req.params.collection, attribute: req.params.attribute, ext: req.params.ext, - query: {id: req.params.guid} + query: {} }; + options.query[config.database.pk] = req.params.guid; handlers.getAttribute(options, responder(config, options, res, next) ); @@ -29,8 +30,9 @@ module.exports = function (app, handlers, markers, config) { var options = { collection: req.params.collection, ext: req.params.ext, - query: {id: req.params.guid} + query: {} }; + options.query[config.database.pk] = req.params.guid; handlers.getContent(options, responder(config, options, res, next) ); @@ -103,9 +105,10 @@ module.exports = function (app, handlers, markers, config) { collection: options.collection, name: options.name, attribute: attribute, - query: {id: result.id}, + query: {}, req: options.req }; + options.query[config.database.pk] = result[config.database.pk]; config.debug && config.debug('getPreviewContent content', attribute_value); handlers.getPreviewHTML(attribute_value, diff --git a/lib/shareHandlers.js b/lib/shareHandlers.js index 6de2c1a..06cb4de 100644 --- a/lib/shareHandlers.js +++ b/lib/shareHandlers.js @@ -71,7 +71,6 @@ module.exports = function (config, model, mongoDataInstance) { attribute: null }; if (splitId.length === 4) { -// options.query = {_id: splitId[2]}; options.query = {name: splitId[2]}; options.attribute = splitId[3]; mongoDataInstance.getMongoAttribute(options, handleMongoGetResult(options)); @@ -185,7 +184,6 @@ module.exports = function (config, model, mongoDataInstance) { }; var attribute = false; if (splitId.length === 4) { -// options.query = {_id: splitId[2]}; options.query = {name: splitId[2]}; options.attribute = splitId[3]; attribute = true; diff --git a/lib/updateShareDocument.js b/lib/updateShareDocument.js index e162c43..a4dd76e 100644 --- a/lib/updateShareDocument.js +++ b/lib/updateShareDocument.js @@ -75,7 +75,7 @@ module.exports = function (config, shareModel) { }); } else if (doc.type.name === 'json') { _.forEach(keys, function (key) { - if (key !== 'id') { + if (key !== config.database.pk) { var op = { p: [key] }; diff --git a/server.js b/server.js index 2ee0aaf..136eef8 100644 --- a/server.js +++ b/server.js @@ -93,6 +93,10 @@ var config = { db: 'Prototyper' } }, + database: { + // pk: '_id' // mongo + pk: 'id' // rethink + }, share: { sockjs: { prefix: '',