Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tomphill committed Sep 21, 2019
1 parent f9e9b5c commit cb71cb3
Show file tree
Hide file tree
Showing 7 changed files with 6,714 additions and 1 deletion.
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
76 changes: 75 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
# gatsby-firesource
# gatsby-firesource [![npm version](https://badge.fury.io/js/gatsby-firesource.svg)](https://badge.fury.io/js/gatsby-firesource)

Gatsby source plugin for building websites using the Firestore as a data source.

# Usage
1. Get a private key for your Firebase project.
2. Put that private key somewhere in your Gatsby project.
3. `$ npm i gatsby-firesource`
4. Configure `gatsby-config.js`

```javascript
module.exports = {
plugins: [
{
resolve: 'gatsby-firesource',
options: {
credential: require("./firebase.json"),
types: [
{
type: 'Book',
collection: 'books',
map: doc => ({
title: doc.title,
isbn: doc.isbn,
author___NODE: doc.author.id,
}),
},
{
type: 'Author',
collection: 'authors',
map: doc => ({
name: doc.name,
country: doc.country,
books___NODE: doc.books.map(book => book.id),
}),
},
],
},
},
],
};

```

5. To query
```graphql
{
allBooks {
edges {
node {
title
isbn
author {
name
}
}
}
}
}
```

# Configurations
Key|Description
---|---
credential|Require your private key here
types| Array of types, which require the following 3 keys
type|The type of the collection, which will be used in GraphQL queries. Eg, when `type = Book`, the GraphQL types are named `book` and `allBook`
collection|The name of the collections in Firestore. Nested collections are **not** tested.
map|A function to map your data in Firestore to Gatsby nodes, utilize the undocumented `___NODE` to link between nodes

# Disclaimer
This project is created solely to suit our requirements, no maintenance/warranty are provided. Feel free to send in pull requests.

# Acknowledgement
[gatsby-source-firestore](https://github.com/taessina/gatsby-source-firestore)
55 changes: 55 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// no-op-lo-op
Loading

0 comments on commit cb71cb3

Please sign in to comment.