-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
executable file
·72 lines (64 loc) · 1.73 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const report = require('gatsby-cli/lib/reporter');
const firebaseAdmin = require('firebase-admin');
const firebaseWeb = require('firebase');
require('firebase/firestore');
const applyConditions = (collection, conditions = []) => {
if (!conditions.length) {
return collection;
}
return conditions.reduce(
(coll, condition) => coll.where.apply(coll, condition),
collection
);
};
exports.sourceNodes = async (
{ actions, createContentDigest },
{ types, credential, config }
) => {
let firebase = firebaseWeb;
if (credential) {
firebase = firebaseAdmin;
}
try {
if (credential) {
firebase.initializeApp({
credential: firebase.credential.cert(credential),
});
} else {
firebase.initializeApp(config);
}
} catch (e) {
report.warn(
'Could not initialize Firebase. Please check `credential` property in gatsby-config.js or supply a valid config object'
);
report.warn(e);
return;
}
const db = firebase.firestore();
const { createNode } = actions;
const promises = types.map(
async ({ collection, type, map = node => node, conditions }) => {
const conditionedCollection = applyConditions(
db.collection(collection),
conditions
);
const snapshot = await conditionedCollection.get();
for (let doc of snapshot.docs) {
const nodeData = map(doc.data());
const nodeMeta = {
id: doc.id,
parent: null,
children: [],
internal: {
type,
contentDigest: createContentDigest(nodeData),
},
};
createNode(Object.assign({}, nodeData, nodeMeta));
Promise.resolve();
}
}
);
await Promise.all(promises);
return;
};