Skip to content

Commit c2d620d

Browse files
author
Sergei Orlov
committed
💥 Make 'properties' an object
Since you are probably going to know the names of the columns in your tables, fetching data via the column names seems to make more sense. All properties except for the 'title' type are now available in Notion.properties, each having a key (column name), a value, and a type
1 parent 1a019ca commit c2d620d

File tree

1 file changed

+35
-29
lines changed

1 file changed

+35
-29
lines changed

gatsby-node.js

+35-29
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,50 @@ exports.sourceNodes = async (
1313
}
1414

1515
data.pages.forEach((page) => {
16-
const properties = Object.keys(page.properties).reduce(
17-
(acc, key) =>
18-
acc.concat([
19-
{
20-
key,
21-
...page.properties[key],
22-
},
23-
]),
24-
[],
16+
const properties = Object.keys(page.properties).reduce((acc, key) => {
17+
if (page.properties[key].type == "title") {
18+
return acc
19+
}
20+
21+
return {
22+
...acc,
23+
[key]: {
24+
key,
25+
value: page.properties[key][page.properties[key].type],
26+
type: page.properties[key].type,
27+
},
28+
}
29+
}, {})
30+
31+
const titleProperty = Object.keys(page.properties).find(
32+
(key) => page.properties[key].type == "title",
2533
)
2634

27-
const title = properties
28-
.find((property) => property.type == "title")
29-
.title.reduce((acc, chunk) => {
30-
if (chunk.type == "text") {
31-
return acc.concat(chunk.plain_text)
35+
const title = page.properties[titleProperty].title.reduce((acc, chunk) => {
36+
if (chunk.type == "text") {
37+
return acc.concat(chunk.plain_text)
38+
}
39+
40+
if (chunk.type == "mention") {
41+
if (chunk.mention.type == "user") {
42+
return acc.concat(chunk.mention.user.name)
3243
}
3344

34-
if (chunk.type == "mention") {
35-
if (chunk.mention.type == "user") {
36-
return acc.concat(chunk.mention.user.name)
45+
if (chunk.mention.type == "date") {
46+
if (chunk.mention.date.end) {
47+
return acc.concat(`${chunk.mention.date.start}${chunk.mention.date.start}`)
3748
}
3849

39-
if (chunk.mention.type == "date") {
40-
if (chunk.mention.date.end) {
41-
return acc.concat(`${chunk.mention.date.start}${chunk.mention.date.start}`)
42-
}
43-
44-
return acc.concat(chunk.mention.date.start)
45-
}
50+
return acc.concat(chunk.mention.date.start)
51+
}
4652

47-
if (chunk.mention.type == "page") {
48-
return acc.concat(chunk.plain_text)
49-
}
53+
if (chunk.mention.type == "page") {
54+
return acc.concat(chunk.plain_text)
5055
}
56+
}
5157

52-
return acc
53-
}, "")
58+
return acc
59+
}, "")
5460

5561
createNode({
5662
id: createNodeId(`${NODE_TYPE}-${page.id}`),

0 commit comments

Comments
 (0)