Notabase (WIP)
API Wrapper For Notion's Database
Getting Started
Installing
yarn add notabase
Use
You can use notabse in browser, browser extension, and node. But there is a difference when initializing the notabase instance.
Node
Env | Token | Read | Write | Note |
---|---|---|---|---|
node | ❌ | public data | ❌ | you can only read public data without token |
node | ✔ | private data | ✔ | you can read and modify private data with token |
// node env// just Read public datalet nb = // CRUD supportlet nb = token: "token_v2 from cookies"
Browser
If you want to use Notabase in your web pages, you need to handle CORS. You can solve this problem with a cloudflare worker.Generate a cloudflare-worker using the code from src/cf-worker.js
In the browser environment, we don't use token
directly, instead we use authcode
to handle identity checks. You need to set an authcode
that only you know in your cf-worker
, and then we use it when initializing the notabase instance.
Env | authCode | Read | Write | Note |
---|---|---|---|---|
browser | ❌ | public data | ❌ | just read public data without authCode |
browser | ✔ | private data | ✔ | you can read and modify private data with authCode |
// browser env// without authCode Read public data// with authCode CRUD supportlet nb = proxy: url: "cloudflare worker url" authCode: "nobody knows it but you"
Browser Extension
// browser extension env// CRUD support without setting anythinglet nb =
Fetch Data
Method | Note |
---|---|
fetch | fetch data from one table, but you can't query relation data |
fetchAll | fetch data from multiple tables, you can query relation data if relation table has been fetched |
// fetch one tablelet songs = await nb// fetch all tables about music// get my music datalet db = await nb
Query
Data in Notion table will be mapped to JavaScript Array
// get all songslet allSongs = dbsongsrows// get song by indexlet song = allSongs0// get one song's title (base props)console// get artist's name of the song (related props)console // a song maybe has two or more artists// search song by title in song's tablelet aSong = allSongs// search all song by artist's name in song's tablelet songByArtistName = allSongs// search all song by artist's name in artist's tablesongByArtistName = dbartistsrowssongs
Write
let aSong = allSongsaSongtitle = "new title"
Create
// create then modifylet newRow = collection// if tag1 is not exists, it will be auto created. newRowTags = "tag1" // create with valuecollection
Delete
// delete row aSong
Update Table Schema
// changecollectionschemaTagsoptions // commitcollection
Todos
collection
- collection.addRow({title:"",Tags:["tag1"]}) // add new row
- updateSchema // update schema
row
- row.delete() // delete a row
all
- Rewrite in TypeScript