Skip to content

Commit

Permalink
Option to make tables separate node types (#52)
Browse files Browse the repository at this point in the history
* Option to make tables separate node types

* Revert "Option to make tables separate node types"

This reverts commit b59ffda.

* Removed package/lock from branch

* Added AirtableField node types.

* Added more info for createSeparateNodeType

* Version bump

* Check that separateNodeType option exists
  • Loading branch information
kevee authored and jbolda committed Nov 26, 2019
1 parent b6cc526 commit cc08b80
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ plugins: [
baseId: `YOUR_AIRTABLE_BASE_ID`,
tableName: `YOUR_TABLE_NAME`,
tableView: `YOUR_TABLE_VIEW_NAME`, // optional
queryName: `OPTIONAL_NAME_TO_IDENTIFY_TABLE`, // optional
queryName: `OPTIONAL_NAME_TO_IDENTIFY_TABLE`, // optionall default is false - makes all records in this table a separate node type, based on your tableView, or if not present, tableName, e.g. a table called "Fruit" would become "allAirtableFruit". Useful when pulling many airtables with similar structures or fields that have different types. See https://github.com/jbolda/gatsby-source-airtable/pull/52.
mapping: { `CASE_SENSITIVE_COLUMN_NAME`: `VALUE_FORMAT` }, // optional, e.g. "text/markdown", "fileNode"
tableLinks: [`CASE`, `SENSITIVE`, `COLUMN`, `NAMES`] // optional, for deep linking to records across tables.
},
Expand Down
15 changes: 11 additions & 4 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ exports.sourceNodes = async (
query.all(),
tableOptions.queryName,
tableOptions.defaultValues || {},
(typeof tableOptions.createSeparateNodeType !== 'undefined') ?
tableOptions.createSeparateNodeType :
false,
cleanMapping,
cleanLinks
])
Expand All @@ -119,8 +122,9 @@ exports.sourceNodes = async (
currentValue[0].map(row => {
row.queryName = currentValue[1]; // queryName from tableOptions above
row.defaultValues = currentValue[2]; // mapping from tableOptions above
row.mapping = currentValue[3]; // mapping from tableOptions above
row.tableLinks = currentValue[4]; // tableLinks from tableOptions above
row.separateNodeType = currentValue[3]; // create separate node type from tableOptions above
row.mapping = currentValue[4]; // mapping from tableOptions above
row.tableLinks = currentValue[5]; // tableLinks from tableOptions above
return row;
})
);
Expand Down Expand Up @@ -310,6 +314,9 @@ const localFileCheck = async (
};

const buildNode = (localFiles, row, cleanedKey, raw, mapping, createNodeId) => {
const nodeType = (row.separateNodeType) ?
`Airtable${cleanKey(row.queryName ? row.queryName : row._table.name)}` :
`Airtable`;
if (localFiles) {
return {
id: createNodeId(`AirtableField_${row.id}_${cleanedKey}`),
Expand All @@ -318,7 +325,7 @@ const buildNode = (localFiles, row, cleanedKey, raw, mapping, createNodeId) => {
raw: raw,
localFiles___NODE: localFiles,
internal: {
type: `AirtableField`,
type: nodeType,
mediaType: mapping,
content: typeof raw === "string" ? raw : JSON.stringify(raw),
contentDigest: crypto
Expand All @@ -334,7 +341,7 @@ const buildNode = (localFiles, row, cleanedKey, raw, mapping, createNodeId) => {
children: [],
raw: raw,
internal: {
type: `AirtableField`,
type: nodeType,
mediaType: mapping,
content: typeof raw === "string" ? raw : JSON.stringify(raw),
contentDigest: crypto
Expand Down

0 comments on commit cc08b80

Please sign in to comment.