Skip to content

✨ Add support for multiple databases #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ Integration token.
The identifier of the database you want to get pages from. The integration identified by provided
token must have access to the database with given id.

`nodeTypeName` [string][optional]

Defines the name of the entity to be queried. For instance, a `nodeTypeName` of value `notionTeamMember` will allow creating queries named `allNotionTeamMember(...)`. Defaults to the string `"Notion"`.

This is particularly useful in case you want having multiple Notion databases.

`propsToFrontmatter` [boolean][defaults to **true**]

Put Notion page props to Markdown frontmatter. If you set this to **false**, you will need to query `notion` to get page props.
Expand Down Expand Up @@ -253,13 +259,42 @@ exports.onCreateNode = async ({ node, actions: { createNode }, createNodeId, get

- Due to the fact that Notion API only appeared recently, and it is still in beta, some blocks are
marked "unsupported". Among others, images cannot be fetched for now
- Currently, `gatsby-source-notion-api` can only work with one provided database. In further
releases, all databases reachable by the Integration will be available for querying
- ~~Currently, `gatsby-source-notion-api` can only work with one provided database. In further
releases, all databases reachable by the Integration will be available for querying~~ Querying multiple databases is now supported! Please read the doc below.
- ~~Nested blocks are currently skipped. E.g. if a list item has a nested sublist, it's contents will
be omitted. This will be fixed in the nearest releases~~ Nested blocks are supported as of `0.4.0`!
- ~~Only raw content is available. Raw meaning whatever Notion returns. Further releases will aim at
providing a more convenient data format apart from the raw one~~ `0.3.0` features support for **archived**, **createdAt**, **updatedAt**, **properties** and **title**.

## Querying multiple databases

In order to query multiple databases, you can add multiple `gatsby-source-notion-api` in your project’s `gatsby-config.js` **and** set different `nodeTypeName` values. For instance:

```javascript
plugins: [
{
resolve: `gatsby-source-notion-api`,
options: {
token: `$INTEGRATION_TOKEN`,
databaseId: `$DATABASE_ID`,
nodeTypeName: "notionTeamMember",
propsToFrontmatter: true,
lowerTitleLevel: true,
},
},
{
resolve: `gatsby-source-notion-api`,
options: {
token: `$INTEGRATION_TOKEN`,
databaseId: `$DATABASE_ID`,
nodeTypeName: "notionTask",
propsToFrontmatter: true,
lowerTitleLevel: true,
},
},
],
```

## 🎉 You did it

Thanks for reaching to the end of the readme!
9 changes: 5 additions & 4 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ const { getNotionPageProperties } = require("./src/transformers/get-page-propert
const { getNotionPageTitle } = require("./src/transformers/get-page-title")
const YAML = require("yaml")

const NOTION_NODE_TYPE = "Notion"
const DEFAULT_NOTION_NODE_TYPE = "Notion"

exports.sourceNodes = async (
{ actions, createContentDigest, createNodeId, reporter, cache },
{ token, databaseId, propsToFrontmatter = true, lowerTitleLevel = true },
{ token, databaseId, nodeTypeName = null, propsToFrontmatter = true, lowerTitleLevel = true },
) => {
const pages = await getPages({ token, databaseId }, reporter, cache)

pages.forEach((page) => {
const title = getNotionPageTitle(page)
const properties = getNotionPageProperties(page)
const notionNodeType = nodeTypeName ?? DEFAULT_NOTION_NODE_TYPE;
let markdown = notionBlockToMarkdown(page, lowerTitleLevel)

if (propsToFrontmatter) {
Expand All @@ -30,7 +31,7 @@ exports.sourceNodes = async (
}

actions.createNode({
id: createNodeId(`${NOTION_NODE_TYPE}-${page.id}`),
id: createNodeId(`${notionNodeType}-${page.id}`),
title,
properties,
archived: page.archived,
Expand All @@ -42,7 +43,7 @@ exports.sourceNodes = async (
parent: null,
children: [],
internal: {
type: NOTION_NODE_TYPE,
type: notionNodeType,
mediaType: "text/markdown",
content: markdown,
contentDigest: createContentDigest(page),
Expand Down