Skip to content

Commit

Permalink
Add simple querying
Browse files Browse the repository at this point in the history
  • Loading branch information
osilviotti committed Dec 29, 2020
1 parent 9ce0589 commit 6196f53
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
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{
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) });
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(
"Could not initialize Firebase. Please check `credential` property in gatsby-config.js"
);
report.warn(e);
return;
}
Expand All @@ -28,8 +24,8 @@ exports.sourceNodes = async (
const { createNode } = actions;

const promises = types.map(
async ({ collection, type, map = node => node }) => {
const snapshot = await db.collection(collection).get();
async ({ collection, type, map = (node) => node, query }) => {
const snapshot = await queryDb(db, collection, query);
for (let doc of snapshot.docs) {
const contentDigest = getDigest(doc.id);
createNode(
Expand All @@ -52,4 +48,15 @@ exports.sourceNodes = async (
await Promise.all(promises);

return;
};
};

function queryDb(db, collection, query) {
let dbQuery = db.collection(collection);

if (query?.where) {
const { field, comparator, value } = query.where;
dbQuery = dbQuery.where(field, comparator, value);
}

return dbQuery.get();
}

0 comments on commit 6196f53

Please sign in to comment.