UNPKG

1.44 kBJavaScriptView Raw
1"use strict";
2
3const _ = require(`lodash`);
4
5const {
6 store
7} = require(`../redux`);
8
9const backend = process.env.GATSBY_DB_NODES || `redux`;
10let nodesDb;
11let runQuery;
12
13switch (backend) {
14 case `redux`:
15 nodesDb = require(`../redux/nodes`);
16 runQuery = require(`../redux/run-sift`).runSift;
17 break;
18
19 case `loki`:
20 nodesDb = require(`./loki/nodes`);
21 runQuery = require(`./loki/nodes-query`);
22 break;
23
24 default:
25 throw new Error(`Unsupported DB nodes backend (value of env var GATSBY_DB_NODES)`);
26}
27
28module.exports = Object.assign({}, nodesDb, {
29 runQuery,
30 backend
31});
32/**
33 * Get content for a node from the plugin that created it.
34 *
35 * @param {Object} node
36 * @returns {promise}
37 */
38
39module.exports.loadNodeContent = node => {
40 if (_.isString(node.internal.content)) {
41 return Promise.resolve(node.internal.content);
42 } else {
43 return new Promise(resolve => {
44 // Load plugin's loader function
45 const plugin = store.getState().flattenedPlugins.find(plug => plug.name === node.internal.owner);
46
47 const {
48 loadNodeContent
49 } = require(plugin.resolve);
50
51 if (!loadNodeContent) {
52 throw new Error(`Could not find function loadNodeContent for plugin ${plugin.name}`);
53 }
54
55 return loadNodeContent(node).then(content => {
56 // TODO update node's content field here.
57 resolve(content);
58 });
59 });
60 }
61};
62//# sourceMappingURL=nodes.js.map
\No newline at end of file