1 | "use strict";
|
2 |
|
3 | const _ = require(`lodash`);
|
4 |
|
5 | const {
|
6 | store
|
7 | } = require(`../redux`);
|
8 |
|
9 | const backend = process.env.GATSBY_DB_NODES || `redux`;
|
10 | let nodesDb;
|
11 | let runQuery;
|
12 |
|
13 | switch (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 |
|
28 | module.exports = Object.assign({}, nodesDb, {
|
29 | runQuery,
|
30 | backend
|
31 | });
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | module.exports.loadNodeContent = node => {
|
40 | if (_.isString(node.internal.content)) {
|
41 | return Promise.resolve(node.internal.content);
|
42 | } else {
|
43 | return new Promise(resolve => {
|
44 |
|
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 |
|
57 | resolve(content);
|
58 | });
|
59 | });
|
60 | }
|
61 | };
|
62 |
|
\ | No newline at end of file |