Skip to content

Commit 822de7f

Browse files
committed
Initial database > dataSource conversion
1 parent 5ee1e8a commit 822de7f

File tree

8 files changed

+107
-60
lines changed

8 files changed

+107
-60
lines changed

components/notion/actions/common/base-page-builder.mjs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { markdownToBlocks } from "@tryfabric/martian";
22
import {
3-
NOTION_DATABASE_META,
3+
NOTION_DATA_SOURCE_META,
44
NOTION_PAGE_META,
55
} from "../../common/notion-meta-properties.mjs";
66
import NOTION_META from "../../common/notion-meta-selection.mjs";
@@ -10,7 +10,7 @@ export default {
1010
methods: {
1111
/**
1212
* Creates additional props for page properties and the selected block children
13-
* @param properties - The selected (database) properties from the page obtained from Notion
13+
* @param properties - The selected (data source) properties from the page obtained from Notion
1414
* @param meta - The selected meta properties
1515
* @param blocks - The selected block children from the workflow UI
1616
* @returns additional props
@@ -102,14 +102,14 @@ export default {
102102
return notionProperties;
103103
},
104104
/**
105-
* Builds page meta properties (parent, icon, cover, archived) from a parent database
105+
* Builds page meta properties (parent, icon, cover, archived) from a parent data source
106106
* Uses the property label as its type to be able to select in notion-meta-properties.mjs
107107
* @param properties - list of Notion page properties inputted by the user
108108
* @returns the meta properties in Notion format inputted by the user
109109
*/
110-
_buildNotionDatabaseMeta(properties = []) {
110+
_buildNotionDataSourceMeta(properties = []) {
111111
properties.forEach((property) => property.type = property.label);
112-
return this._convertPropertiesToNotion(properties, NOTION_DATABASE_META);
112+
return this._convertPropertiesToNotion(properties, NOTION_DATA_SOURCE_META);
113113
},
114114
/**
115115
* Builds page meta properties (parent, icon, cover, archived) from a parent page
@@ -122,21 +122,21 @@ export default {
122122
return this._convertPropertiesToNotion(properties, NOTION_PAGE_META);
123123
},
124124
/**
125-
* Builds page properties from a parent database/page
125+
* Builds page properties from a parent data source/page
126126
* @param properties - list of Notion page properties inputted by the user
127127
* @returns the properties in Notion format inputted by the user
128128
*/
129129
_buildNotionPageProperties(properties = []) {
130130
return this._convertPropertiesToNotion(properties, NOTION_PAGE_PROPERTIES);
131131
},
132132
/**
133-
* Builds the page meta inputted by the user in Notion format from a parent database
134-
* @param parentDatabase - the parent database that contains the meta properties
133+
* Builds the page meta inputted by the user in Notion format from a parent data source
134+
* @param parentDataSource - the parent data source that contains the meta properties
135135
* @returns the meta properties in Notion format
136136
*/
137-
buildDatabaseMeta(parentDatabase) {
138-
const filteredMeta = this._filterProps(parentDatabase);
139-
return this._buildNotionDatabaseMeta(filteredMeta);
137+
buildDataSourceMeta(parentDataSource) {
138+
const filteredMeta = this._filterProps(parentDataSource);
139+
return this._buildNotionDataSourceMeta(filteredMeta);
140140
},
141141
/**
142142
* Builds the page meta inputted by the user in Notion format from a parent page
@@ -210,13 +210,13 @@ export default {
210210
},
211211
};
212212
},
213-
childDatabaseToLink(block) {
213+
childDataSourceToLink(block) {
214214
return {
215215
object: "block",
216216
type: "link_to_page",
217217
link_to_page: {
218-
type: "database_id",
219-
database_id: block.id,
218+
type: "data_source_id",
219+
data_source_id: block.id,
220220
},
221221
};
222222
},
@@ -236,9 +236,9 @@ export default {
236236
if (child.type === "child_page") {
237237
// convert child pages to links
238238
children[i] = this.childPageToLink(child);
239-
} else if (child.type === "child_database") {
240-
// convert child databases to links
241-
children[i] = this.childDatabaseToLink(child);
239+
} else if (child.type === "child_data_source") {
240+
// convert child data sources to links
241+
children[i] = this.childDataSourceToLink(child);
242242
} else {
243243
if (this.notValid(child, c)) {
244244
children[i] = undefined;

components/notion/actions/create-database/create-database.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export default {
66
...base,
77
key: "notion-create-database",
88
name: "Create Database",
9-
description: "Create a database. [See the documentation](https://developers.notion.com/reference/create-a-database)",
10-
version: "0.0.1",
9+
description: "Create a database and its initial data source. [See the documentation](https://developers.notion.com/reference/database-create)",
10+
version: "0.1.0",
1111
type: "action",
1212
props: {
1313
notion,
@@ -45,7 +45,9 @@ export default {
4545
},
4646
},
4747
],
48-
properties: utils.parseObject(this.properties),
48+
initial_data_source: {
49+
properties: utils.parseObject(this.properties),
50+
},
4951
});
5052

5153
$.export("$summary", `Successfully created database with ID ${response.id}`);

components/notion/actions/create-page-from-database/create-page-from-database.mjs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
key: "notion-create-page-from-database",
99
name: "Create Page from Database",
1010
description: "Create a page from a database. [See the documentation](https://developers.notion.com/reference/post-page)",
11-
version: "0.2.3",
11+
version: "1.0.0",
1212
type: "action",
1313
props: {
1414
notion,
@@ -20,6 +20,15 @@ export default {
2020
label: "Parent Database ID",
2121
description: "Select a parent database or provide a database ID",
2222
},
23+
dataSource: {
24+
propDefinition: [
25+
notion,
26+
"dataSourceId",
27+
({ parent }) => ({
28+
databaseId: parent,
29+
}),
30+
],
31+
},
2332
Name: {
2433
type: "string",
2534
label: "Name",
@@ -60,14 +69,14 @@ export default {
6069
methods: {
6170
...base.methods,
6271
/**
63-
* Builds a page from a parent database
64-
* @param parentDatabase - the parent database
72+
* Builds a page from a parent data source
73+
* @param parentDataSource - the parent data source
6574
* @returns the constructed page in Notion format
6675
*/
67-
buildPage(parentDatabase) {
68-
const meta = this.buildDatabaseMeta(parentDatabase);
76+
buildPage(parentDataSource) {
77+
const meta = this.buildDataSourceMeta(parentDataSource);
6978
this.properties = utils.parseObject(this.properties);
70-
const properties = this.buildPageProperties(parentDatabase.properties);
79+
const properties = this.buildPageProperties(parentDataSource.properties);
7180
const children = this.createBlocks(this.pageContent);
7281
return {
7382
...meta,
@@ -78,7 +87,7 @@ export default {
7887
},
7988
async run({ $ }) {
8089
const MAX_BLOCKS = 100;
81-
const parentPage = await this.notion.retrieveDatabase(this.parent);
90+
const parentPage = await this.notion.retrieveDataSource(this.dataSource);
8291
const {
8392
children, ...page
8493
} = this.buildPage(parentPage);

components/notion/actions/query-database/query-database.mjs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import notion from "../../notion.app.mjs";
33

44
export default {
55
key: "notion-query-database",
6-
name: "Query Database",
7-
description: "Query a database with a specified filter. [See the documentation](https://developers.notion.com/reference/post-database-query)",
8-
version: "0.1.0",
6+
name: "Query Data Source",
7+
description: "Query a data source with a specified filter. [See the documentation](https://developers.notion.com/reference/query-a-data-source)",
8+
version: "1.0.0",
99
type: "action",
1010
props: {
1111
notion,
@@ -15,14 +15,23 @@ export default {
1515
"databaseId",
1616
],
1717
},
18+
dataSourceId: {
19+
propDefinition: [
20+
notion,
21+
"dataSourceId",
22+
({ databaseId }) => ({
23+
databaseId,
24+
}),
25+
],
26+
},
1827
filter: {
1928
label: "Filter (query)",
20-
description: "The filter to apply, as a JSON-stringified object. [See the documentation for available filters](https://developers.notion.com/reference/post-database-query-filter). Example: `{ \"property\": \"Name\", \"title\": { \"contains\": \"title to search for\" } }`",
29+
description: "The filter to apply, as a JSON-stringified object. [See the documentation for available filters](https://developers.notion.com/reference/filter-data-source-entries). Example: `{ \"property\": \"Name\", \"title\": { \"contains\": \"title to search for\" } }`",
2130
type: "string",
2231
},
2332
sorts: {
2433
label: "Sorts",
25-
description: "The sort order for the query. [See the documentation for available sorts](https://developers.notion.com/reference/post-database-query-sort). Example: `[ { \"property\": \"Name\", \"direction\": \"ascending\" } ]`",
34+
description: "The sort order for the query. [See the documentation for available sorts](https://developers.notion.com/reference/sort-data-source-entries). Example: `[ { \"property\": \"Name\", \"direction\": \"ascending\" } ]`",
2635
type: "string[]",
2736
},
2837
},
@@ -31,7 +40,7 @@ export default {
3140
filter, sorts,
3241
} = this;
3342

34-
const response = await this.notion.queryDatabase(this.databaseId, {
43+
const response = await this.notion.queryDataSource(this.dataSourceId, {
3544
filter: utils.parseStringToJSON(filter),
3645
sorts: utils.parseObject(sorts),
3746
});

components/notion/actions/search/search.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import common from "../common/base-search.mjs";
33
export default {
44
...common,
55
key: "notion-search",
6-
name: "Find Pages or Databases",
7-
description: "Searches for a page or database. [See the documentation](https://developers.notion.com/reference/post-search)",
8-
version: "0.0.9",
6+
name: "Find Pages or Data Sources",
7+
description: "Searches for a page or data source. [See the documentation](https://developers.notion.com/reference/post-search)",
8+
version: "0.1.0",
99
type: "action",
1010
props: {
1111
...common.props,

components/notion/common/notion-meta-properties.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import utils from "./utils.mjs";
55
*
66
* convertToNotion: converts the prop values to send to the Notion API
77
*/
8-
export const NOTION_DATABASE_META = {
8+
export const NOTION_DATA_SOURCE_META = {
99
parent: {
1010
convertToNotion: (property) => ({
11-
type: "database_id",
11+
type: "data_source_id",
1212
database_id: property.value,
1313
}),
1414
},
@@ -42,7 +42,7 @@ export const NOTION_DATABASE_META = {
4242
* Implementation for Page Meta in Notion - https://developers.notion.com/reference/page
4343
*/
4444
export const NOTION_PAGE_META = {
45-
...NOTION_DATABASE_META,
45+
...NOTION_DATA_SOURCE_META,
4646
parent: {
4747
convertToNotion: (property) => ({
4848
type: "page_id",
@@ -52,6 +52,6 @@ export const NOTION_PAGE_META = {
5252
};
5353

5454
export default {
55-
NOTION_DATABASE_META,
55+
NOTION_DATA_SOURCE_META,
5656
NOTION_PAGE_META,
5757
};

components/notion/notion.app.mjs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ export default {
1919
return this._buildPaginatedOptions(options, response.next_cursor);
2020
},
2121
},
22+
dataSourceId: {
23+
type: "string",
24+
label: "Data Source ID",
25+
description: "Select a data source from the database or provide a data source ID",
26+
async options({ databaseId }) {
27+
this._checkOptionsContext(databaseId, "Database ID");
28+
const dataSources = await this.getDataSourcesForDatabase(databaseId);
29+
return dataSources?.map((dataSource) => ({
30+
label: dataSource.name,
31+
value: dataSource.id,
32+
})) || [];
33+
},
34+
},
2235
pageId: {
2336
type: "string",
2437
label: "Page ID",
@@ -41,13 +54,13 @@ export default {
4154
pageIdInDatabase: {
4255
type: "string",
4356
label: "Page ID",
44-
description: "Search for a page from the database or provide a page ID",
57+
description: "Search for a page from the data source or provide a page ID",
4558
useQuery: true,
4659
async options({
47-
query, prevContext, databaseId,
60+
query, prevContext, dataSourceId,
4861
}) {
49-
this._checkOptionsContext(databaseId, "Database ID");
50-
const response = await this.queryDatabase(databaseId, {
62+
this._checkOptionsContext(dataSourceId, "Data Source ID");
63+
const response = await this.queryDataSource(dataSourceId, {
5164
query,
5265
start_cursor: prevContext.nextPageParameters ?? undefined,
5366
});
@@ -65,8 +78,8 @@ export default {
6578

6679
const parentType = response.parent.type;
6780
try {
68-
const { properties } = parentType === "database_id"
69-
? await this.retrieveDatabase(response.parent.database_id)
81+
const { properties } = parentType === "data_source_id"
82+
? await this.retrieveDataSource(response.parent.data_source_id).properties
7083
: response;
7184

7285
const propEntries = Object.entries(properties);
@@ -195,12 +208,12 @@ export default {
195208
},
196209
filter: {
197210
type: "string",
198-
label: "Page or Database",
199-
description: "Whether to search for pages or databases",
211+
label: "Page or Data Source",
212+
description: "Whether to search for pages or data sources.",
200213
optional: true,
201214
options: [
202215
"page",
203-
"database",
216+
"data_source",
204217
],
205218
},
206219
pageContent: {
@@ -219,7 +232,7 @@ export default {
219232
_getNotionClient() {
220233
return new notion.Client({
221234
auth: this.$auth.oauth_access_token,
222-
notionVersion: "2022-02-22",
235+
notionVersion: "2025-09-03",
223236
});
224237
},
225238
_extractDatabaseTitleOptions(databases) {
@@ -292,6 +305,23 @@ export default {
292305
async updateDatabase(database) {
293306
return this._getNotionClient().databases.update(database);
294307
},
308+
async queryDataSource(dataSourceId, params = {}) {
309+
return this._getNotionClient().request({
310+
method: "POST",
311+
path: `data_sources/${dataSourceId}/query`,
312+
body: params,
313+
});
314+
},
315+
async retrieveDataSource(dataSourceId) {
316+
return this._getNotionClient().request({
317+
method: "GET",
318+
path: `data_sources/${dataSourceId}`,
319+
});
320+
},
321+
async getDataSourcesForDatabase(databaseId) {
322+
const database = await this.retrieveDatabase(databaseId);
323+
return database.data_sources || [];
324+
},
295325
async createFileUpload(file) {
296326
return this._getNotionClient().fileUploads.create(file);
297327
},
@@ -342,22 +372,22 @@ export default {
342372
});
343373
},
344374
/**
345-
* This generator function scans the pages in a database yields each page
375+
* This generator function scans the pages in a data source and yields each page
346376
* separately.
347377
*
348-
* @param {string} databaseId - The database containing the pages to scan
378+
* @param {string} dataSourceId - The data source containing the pages to scan
349379
* @param {object} [opts] - Options to customize the operation
350380
* @yield {object} The next page
351381
*/
352-
async *getPages(databaseId, opts = {}) {
382+
async *getPages(dataSourceId, opts = {}) {
353383
let cursor;
354384

355385
do {
356386
const params = {
357387
...opts,
358388
start_cursor: cursor,
359389
};
360-
const response = await this.queryDatabase(databaseId, params);
390+
const response = await this.queryDataSource(dataSourceId, params);
361391
const {
362392
results: pages,
363393
next_cursor: nextCursor,

0 commit comments

Comments
 (0)