gatsby-source-notion-database
Advanced tools
Comparing version 0.5.5 to 0.5.6
@@ -9,26 +9,42 @@ const Notabase = require("notabase") | ||
const { createNode } = actions | ||
const { configTable, dbMap, settings } = options | ||
const { configTable, sourceConfig } = options | ||
let cacheSettings = {} | ||
let config | ||
let _dbMap = {} | ||
let cacheTable = [] | ||
if (configTable) { | ||
let config = await nb.fetch(configTable) | ||
config.rows.filter(i => i).map(c => { | ||
let configCollection = await nb.fetch(configTable) | ||
config = configCollection.rows | ||
} else if (sourceConfig) { | ||
config = sourceConfig | ||
} | ||
config.filter(i => i).map(c => { | ||
if (!c.cacheType === "dynamic") { | ||
_dbMap[c.name] = c.table | ||
if (c.isHtmlCache) { | ||
cacheSettings[c.name] = 'html' | ||
} | ||
}) | ||
} else if (dbMap) { | ||
_dbMap = dbMap | ||
} else { | ||
cacheTable.push(c.name) | ||
} | ||
}) | ||
const nodeContent = JSON.stringify(config) | ||
const nodeMeta = { | ||
id: createNodeId(nodeContent), | ||
parent: null, | ||
children: [], | ||
internal: { | ||
type: 'SourceConfig', | ||
mediaType: `text/html`, | ||
content: nodeContent, | ||
contentDigest: createContentDigest(SourceConfig) | ||
}, | ||
} | ||
if (settings) { | ||
cacheSettings = settings | ||
} | ||
const node = Object.assign({}, SourceConfig, nodeMeta) | ||
createNode(node) | ||
let db = await nb.fetchAll(_dbMap) | ||
await Promise.all(Object.entries(db).map(async (i) => { | ||
let [tableName, collection] = i | ||
await genApiData(nb, collection, tableName, 'id', createNode, createNodeId, createContentDigest, cacheSettings) | ||
await genApiData(nb, collection, tableName, 'id', createNode, createNodeId, createContentDigest, cacheTable) | ||
})) | ||
return | ||
} |
{ | ||
"name": "gatsby-source-notion-database", | ||
"version": "0.5.5", | ||
"version": "0.5.6", | ||
"description": "Load data from Notion's database", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,3 +44,3 @@ const getPageHtml = require('./getPageHtml') | ||
async function genApiData(nb, collection, tableName, key, createNode, createNodeId, createContentDigest, settings) { | ||
async function genApiData(nb, collection, tableName, key, createNode, createNodeId, createContentDigest, cacheTable) { | ||
console.log(`🌈fetch data from notion: ${tableName}`) | ||
@@ -70,19 +70,17 @@ let props = collection.props.filter(i => !(i === '_raw')) | ||
if (settings.hasOwnProperty(tableName)) { | ||
if (settings[tableName] === 'html') { | ||
let cachedData = getCachedData(data) | ||
if (cachedData) { | ||
console.log(`get html from cache: ${tableName} - ${itemData.id}`) | ||
data.html = cachedData.html | ||
} else { | ||
console.log(`get html from notion: ${tableName} - ${itemData.id}`) | ||
let url = `https://notion.so/${itemData.id.split('-').join('')}` | ||
try { | ||
let html = await getPageHtml(url) | ||
data.html = html | ||
updateCacheData(data) | ||
} catch (error) { | ||
data.html = `fetch error` | ||
console.log(`failed to fetch html of ${tableName} - ${itemData.id}`) | ||
} | ||
if (cacheTable.includes(tableName)) { | ||
let cachedData = getCachedData(data) | ||
if (cachedData) { | ||
console.log(`get html from cache: ${tableName} - ${itemData.id}`) | ||
data.html = cachedData.html | ||
} else { | ||
console.log(`get html from notion: ${tableName} - ${itemData.id}`) | ||
let url = `https://notion.so/${itemData.id.split('-').join('')}` | ||
try { | ||
let html = await getPageHtml(url) | ||
data.html = html | ||
updateCacheData(data) | ||
} catch (error) { | ||
data.html = `fetch error` | ||
console.log(`failed to fetch html of ${tableName} - ${itemData.id}`) | ||
} | ||
@@ -89,0 +87,0 @@ } |
23527
196