Skip to content

Commit

Permalink
feat: add support for database paging
Browse files Browse the repository at this point in the history
notion-markdown-cms would previously fail when trying to query pages
from a database with more than 100 entries because paging was not
implemented.
  • Loading branch information
JohannesRudolph committed Oct 17, 2022
1 parent 3513b3d commit 3c95e7c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.10.1",
"version": "0.11.0",
"name": "@meshcloud/notion-markdown-cms",
"engines": {
"node": ">=14"
Expand Down
2 changes: 1 addition & 1 deletion src/ChildDatabaseRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ export class ChildDatabaseRenderer {
page_size: 100,
});

return allPages.results;
return allPages;
}
}
36 changes: 24 additions & 12 deletions src/NotionApiFacade.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {
APIErrorCode, APIResponseError, Client, RequestTimeoutError, UnknownHTTPResponseError
} from '@notionhq/client';
import { DatabasesQueryParameters } from '@notionhq/client/build/src/api-endpoints';
APIErrorCode,
APIResponseError,
Client,
RequestTimeoutError,
UnknownHTTPResponseError,
} from "@notionhq/client";
import { DatabasesQueryParameters } from "@notionhq/client/build/src/api-endpoints";

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

Expand Down Expand Up @@ -34,17 +38,25 @@ export class NotionApiFacade {
}

async queryDatabase(query: DatabasesQueryParameters) {
const result = await this.withRetry(
async () => await this.client.databases.query(query)
); // todo: paging
const results = [];

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

return result;
do {
const response = await this.withRetry(
async () =>
await this.client.databases.query({
...query,
start_cursor: next_cursor || undefined,
})
);

results.push(...response.results);

next_cursor = response.next_cursor;
} while (next_cursor);

return results;
}

async retrievePage(pageId: string) {
Expand Down

0 comments on commit 3c95e7c

Please sign in to comment.