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
node | ❌ | public data | ❌ | you can only read public data without token |
node | ✔ | private data | ✔ | you can read and modify private data with token |
import Notabase from 'notabase'
let nb = new Notabase()
let nb = new Notabase({
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.
browser | ❌ | public data | ❌ | just read public data without authCode |
browser | ✔ | private data | ✔ | you can read and modify private data with authCode |
import Notabase from 'notabase'
let nb = new Notabase({
proxy: {
url: "cloudflare worker url",
authCode: "nobody knows it but you"
}
})
Browser Extension
import Notabase from 'notabase'
let nb = new Notabase()
withCredentials
Fetch Data
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 |
let songs = await nb.fetch("https://www.notion.so/2628769120ad41d998ec068d6e2eb410?v=e8e69ac68a8d483792c54541e4d8ba72")
let db = await nb.fetchAll({
songs: "https://www.notion.so/2628769120ad41d998ec068d6e2eb410?v=e8e69ac68a8d483792c54541e4d8ba72",
albums: "https://www.notion.so/15f1759f38a34fedaa79262812b707f0?v=b385656739214101b2b8a159092a52e8",
artists: "https://www.notion.so/31b8544ffb034964b1aa56bfa78497c1?v=1d9cbfcd279d4534964acdd374c9824e"
})
Query
Data in Notion table will be mapped to JavaScript Array
let allSongs = db.songs.rows
let song = allSongs[0]
console.log(`${song.tile}`)
console.log(`${song.artist[0].Name}`)
let aSong = allSongs.find(song=> song.title === "Bad Guy")
let songByArtistName = allSongs.filter(song=> song.artist[0].name === "someone")
songByArtistName = db.artists.rows.filter(a=> a.name === "someone").songs
Write
let aSong = allSongs.find(song=> song.title === "Bad Guy")
aSong.title = "new title"
Create
let newRow = collection.addRow()
newRow.Tags = ["tag1"]
collection.addRow({title:"",Tags:["tag1"]})
Delete
aSong.delete()
Update Table Schema
collection.schema.Tags.options.push({
id:nb.genId(),
value: "new tag",
color: "pink"
})
collection.updateSchema()
Todos
collection
row
all