-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.js
executable file
·55 lines (46 loc) · 1.23 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
const report = require('gatsby-cli/lib/reporter');
const firebase = require('firebase-admin');
const crypto = require('crypto');
const getDigest = id =>
crypto
.createHash('md5')
.update(id)
.digest('hex');
exports.sourceNodes = async (
{ actions },
{ types, credential }
) => {
try{
if (firebase.apps || !firebase.apps.length) {
firebase.initializeApp({ credential: firebase.credential.cert(credential) });
}
} catch (e) {
report.warn('Could not initialize Firebase. Please check `credential` property in gatsby-config.js');
report.warn(e);
return;
}
const db = firebase.firestore();
const { createNode } = actions;
const promises = types.map(
async ({ collection, type, map = node => node }) => {
const snapshot = await db.collection(collection).get();
for (let doc of snapshot.docs) {
const contentDigest = getDigest(doc.id);
createNode(
Object.assign({}, map(doc.data()), {
id: doc.id,
parent: null,
children: [],
internal: {
type,
contentDigest,
},
})
);
Promise.resolve();
}
}
);
await Promise.all(promises);
return;
};