|
|
||
|---|---|---|
| src | ||
| .env.example | ||
| .gitignore | ||
| package.json | ||
| pnpm-lock.yaml | ||
| README.md | ||
notion-database
Fetch pages (entries) from database and map/transform the pages to a type.
Notion
Notion key
How to obtain new API key and connect it to database/page/etc.:
- Go to
https://www.notion.so/profile/integrations - Click 'New integration'
- Name the integration
- Select workspace and type
- Save new integration
- Continue to integration settings
- Copy the key and keep it safe!
- Modify capabilities as needed
- Go to your workspace to the page/database
- Click the three dots (top right corner) and select connections
- Add your new integration to grant access to the page/database to the integration
Database ID
Go to Notion GUI on web, to the database, copy the hash/ID.
Example:
The URL is https://www.notion.so/mareshq/1eb611d502b1807bbc5df523a3acc322?v=1eb611d502b180218d5b000c62cd80a8, the database ID is 1eb611d502b1807bbc5df523a3acc322 and its located in the path, the segment after /mareshq/<database-id>. Aka https://www.notion.so/<workspace-slug>/<database-id>.
Working with the Notion SDK (@notionhq/client)
The JavaScript SDK is only a simple wrapper on top of Notion's HTTP API, therefore only generic types are available and I find it tricky to work with types.
Instead of trying to figure out the types with the type definition, go to the API documentation (see Notion Developer portal)and work with the schema from there. It was much easier.
Also type casting response pages is tricky, just do mapping. But you will have to do a lot of null checks, without them you will pray that everything works. Until it breaks. This proof-of-concept does not check for null, but you should!