-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
177 lines (156 loc) · 3.75 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { notion, logger } from "./config.js";
import { newDatabase } from "./index.js";
/**
* Inspiration https://stackoverflow.com/a/31729267
*
* @param {Object} dict
* @param {String} key
*/
const removeKeyInDict = (dict, key) => {
for (const prop in dict) {
if (prop === key) {
delete dict[prop];
} else if (typeof dict[prop] === "object") {
removeKeyInDict(dict[prop], key);
}
}
};
/**
*
* @param {*} dbTitle
* @param {*} properties
* @returns
*/
// export const createDb = async (dbTitle, properties) => {
// try {
// const { id } = await notion.databases.create({
// parent: {
// type: 'page_id',
// page_id: "b8595b75-abd1-4cad-8dfe-f935a8ef57cb"
// },
// title: [
// {
// type: "text",
// text: {
// content: dbTitle,
// link: null
// }
// }
// ],
// properties: properties,
// });
// return id;
// } catch (error) {
// logger.debug(error);
// throw new Error(`Error retrieving database ${error.message}`);
// }
// };
/**
*
* @param {String} dbId
* @param {Object} properties
* @returns
*/
export const updateDbProperties = async (id, properties) => {
try {
const response = await notion.databases.update({
database_id: id,
properties: properties,
});
return { ...response };
} catch (error) {
logger.debug("updateDbProperties", error);
throw new Error(`Error updating database ${error.message}`);
}
};
/**
*
* @param {String} dbId
* @param {Object} properties
* @returns
*/
export const createPage = async (dbId, page) => {
try {
const response = await notion.pages.create({
parent: {
database_id: dbId,
},
properties: page.properties,
icon: page.icon,
cover: page.cover,
// children: page.children
});
return { ...response };
} catch (error) {
logger.debug("createPage", error);
throw new Error(`Error creating page ${error.message}`);
}
};
/**
*
* @param {String} id
* @returns {Object}
*/
const getDatabaseById = async (id) => {
try {
const response = await notion.databases.retrieve({
database_id: id,
});
return { ...response };
} catch (error) {
logger.debug("getDatabaseById", error);
throw new Error(`Error retrieving database ${error.message}`);
}
};
/**
* Gets pages from the database.
*
* @returns {Promise<Array<{ source_db: string, cover: string, icon: string, properties: object}>>}
*/
export const getPagesFromDatabase = async (databaseId, databaseTitle) => {
const pages = [];
let cursor = undefined;
while (true) {
const { results, next_cursor } = await notion.databases.query({
database_id: databaseId,
start_cursor: cursor,
});
pages.push(...results);
if (!next_cursor) {
break;
}
cursor = next_cursor;
}
logger.debug(
"getPagesFromDatabase",
`${pages.length} pages successfully fetched.`
);
return pages.map((page) => {
for (const entry of Object.keys(page.properties)) {
removeKeyInDict(page.properties[entry], "id");
}
const pageData = {
// source_db: databaseTitle,
children: page.children,
cover: page.cover,
icon: page.icon,
properties: page.properties,
};
newDatabase["pages"].push(pageData);
return pageData;
});
};
/**
*
* @param {String} id
* @returns
*/
export const extractDatabaseProperties = async (id) => {
const { title, properties } = await getDatabaseById(id);
logger.info("extractDatabaseProperties", properties);
for (const entry of Object.keys(properties)) {
removeKeyInDict(properties[entry], "id");
newDatabase["properties"][entry] = properties[entry];
}
return title[0].plain_text;
};