Skip to content

Commit 3c95e7c

Browse files
feat: add support for database paging
notion-markdown-cms would previously fail when trying to query pages from a database with more than 100 entries because paging was not implemented.
1 parent 3513b3d commit 3c95e7c

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.10.1",
2+
"version": "0.11.0",
33
"name": "@meshcloud/notion-markdown-cms",
44
"engines": {
55
"node": ">=14"

src/ChildDatabaseRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,6 @@ export class ChildDatabaseRenderer {
106106
page_size: 100,
107107
});
108108

109-
return allPages.results;
109+
return allPages;
110110
}
111111
}

src/NotionApiFacade.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {
2-
APIErrorCode, APIResponseError, Client, RequestTimeoutError, UnknownHTTPResponseError
3-
} from '@notionhq/client';
4-
import { DatabasesQueryParameters } from '@notionhq/client/build/src/api-endpoints';
2+
APIErrorCode,
3+
APIResponseError,
4+
Client,
5+
RequestTimeoutError,
6+
UnknownHTTPResponseError,
7+
} from "@notionhq/client";
8+
import { DatabasesQueryParameters } from "@notionhq/client/build/src/api-endpoints";
59

610
const debug = require("debug")("notion-api");
711

@@ -34,17 +38,25 @@ export class NotionApiFacade {
3438
}
3539

3640
async queryDatabase(query: DatabasesQueryParameters) {
37-
const result = await this.withRetry(
38-
async () => await this.client.databases.query(query)
39-
); // todo: paging
41+
const results = [];
4042

41-
if (result.next_cursor) {
42-
throw new Error(
43-
`Paging not implemented, db ${query.database_id} has more than 100 entries`
44-
);
45-
}
43+
let next_cursor: string | null = null;
4644

47-
return result;
45+
do {
46+
const response = await this.withRetry(
47+
async () =>
48+
await this.client.databases.query({
49+
...query,
50+
start_cursor: next_cursor || undefined,
51+
})
52+
);
53+
54+
results.push(...response.results);
55+
56+
next_cursor = response.next_cursor;
57+
} while (next_cursor);
58+
59+
return results;
4860
}
4961

5062
async retrievePage(pageId: string) {

0 commit comments

Comments
 (0)