Skip to content

Commit

Permalink
feat(config): custom views can be put in a special views folder in mu…
Browse files Browse the repository at this point in the history
…ltiple files
  • Loading branch information
stropitek committed Jun 1, 2018
1 parent 89e23c6 commit 3e08f77
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 43 deletions.
48 changes: 34 additions & 14 deletions src/config/db.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable import/no-dynamic-require */

'use strict';

const fs = require('fs');
const path = require('path');

const _ = require('lodash');
const hasOwn = require('has-own');

const die = require('../util/die');
Expand All @@ -21,22 +24,40 @@ if (homeDir) {
if (fs.statSync(databasePath).isDirectory()) {
let databaseConfig = {};
if (fs.existsSync(databaseConfigPath)) {
// eslint-disable-next-line import/no-dynamic-require
databaseConfig = require(databaseConfigPath);
const designDocNames = {};
databaseConfig.designDocNames = [];
if (
databaseConfig.customDesign &&
databaseConfig.customDesign.views
) {
const views = databaseConfig.customDesign.views;
for (const key in views) {
if (hasOwn(key, views)) {
if (!views[key].designDoc) {
views[key].designDoc = constants.CUSTOM_DESIGN_DOC_NAME;
}
designDocNames[key] = views[key].designDoc;
databaseConfig.customDesign = databaseConfig.customDesign || {};
databaseConfig.customDesign.views =
databaseConfig.customDesign.views || {};
const views = databaseConfig.customDesign.views;

// Get views from views folder
let viewFiles;
try {
viewFiles = fs.readdirSync(path.join(databasePath, 'views'));
} catch (e) {
viewFiles = [];
}

for (let view of viewFiles) {
const v = require(path.join(databasePath, 'views', view));
const currentKeys = Object.keys(views);
const newKeys = Object.keys(v);
const intersectionKeys = _.intersection(currentKeys, newKeys);
if (intersectionKeys.length !== 0) {
throw new Error(
`a view is defined more than once: ${intersectionKeys}`
);
}
Object.assign(views, v);
}
for (const key in views) {
if (hasOwn(key, views)) {
if (!views[key].designDoc) {
views[key].designDoc = constants.CUSTOM_DESIGN_DOC_NAME;
}
designDocNames[key] = views[key].designDoc;
}
}
databaseConfig.designDocNames = designDocNames;
Expand All @@ -53,7 +74,7 @@ if (homeDir) {
}
} catch (e) {
console.error(e.stack || e); // eslint-disable-line no-console
die(`could not read database configurations from ${homeDir}`);
die(`could not read database configurations from ${homeDir}, ${e.message}`);
}
}

Expand All @@ -66,7 +87,6 @@ function readImportConfig(databasePath, databaseConfig) {
if (fs.statSync(importPath).isDirectory()) {
let importConfig = {};
if (fs.existsSync(importConfigPath)) {
// eslint-disable-next-line import/no-dynamic-require
importConfig = require(importConfigPath);
}
if (typeof importConfig === 'function') {
Expand Down
50 changes: 21 additions & 29 deletions test/homedir/test3/config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
'use strict';

module.exports = {
customDesign: {
version: 8,
views: {
lib: {
test: ['lib.js']
},
test: {
map: function (doc) {
const libTest = require('views/lib/test');
if (doc.$type === 'entry') {
emit(doc._id, libTest.fortyTwo());
}
}
},
testCustom: {
map: function (doc) {
if (doc.$type === 'entry') {
emit(doc._id);
}
},
designDoc: 'custom'
}
},
updates: {},
filters: {
abc: function (doc) {
return doc.$type === 'log';
}
customDesign: {
version: 8,
views: {
lib: {
test: ['lib.js']
},
test: {
map: function (doc) {
const libTest = require('views/lib/test');
if (doc.$type === 'entry') {
emit(doc._id, libTest.fortyTwo());
}
}
}
},
entryUnicity: 'global'
updates: {},
filters: {
abc: function (doc) {
return doc.$type === 'log';
}
}
},
entryUnicity: 'global'
};
12 changes: 12 additions & 0 deletions test/homedir/test3/views/testCustom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

module.exports = {
testCustom: {
map: function (doc) {
if (doc.$type === 'entry') {
emit(doc._id);
}
},
designDoc: 'custom'
}
};

0 comments on commit 3e08f77

Please sign in to comment.