Giter Site home page Giter Site logo

notabase's Introduction

⚠️ Notion API 变动、不建议使用此项目。等官方 API 开放

⚠️ DO NOT use this

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
import Notabase from 'notabase'

// node env
// just Read public data
let nb = new Notabase()
// CRUD support
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.

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
import Notabase from 'notabase'

// browser env
// without authCode Read public data
// with authCode CRUD support
let nb = new Notabase({
  proxy: {
    url: "cloudflare worker url",
    authCode: "nobody knows it but you"
  }
})

Browser Extension

import Notabase from 'notabase'

// browser extension env
// CRUD support without setting anything
let nb = new Notabase()

withCredentials

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 table
let songs = await nb.fetch("https://www.notion.so/2628769120ad41d998ec068d6e2eb410?v=e8e69ac68a8d483792c54541e4d8ba72")


// fetch all tables about music
// get my music data
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

// get all songs
let allSongs = db.songs.rows
 
// get song by index
let song = allSongs[0]

// get one song's title (base props)
console.log(`${song.tile}`)

// get artist's name of the song (related props)
console.log(`${song.artist[0].Name}`) // a song maybe has two or more artists

// search song by title in song's table
let aSong = allSongs.find(song=> song.title === "Bad Guy")
// search all song by artist's name in song's table
let songByArtistName = allSongs.filter(song=> song.artist[0].name === "someone")
// search all song by artist's name in artist's table
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

// create then modify
let newRow = collection.addRow()
// if tag1 is not exists, it will be auto created. 
newRow.Tags = ["tag1"]


// create with value
collection.addRow({title:"",Tags:["tag1"]}) 

Delete

// delete row 
aSong.delete()

Update Table Schema

// change
collection.schema.Tags.options.push({
  id:nb.genId(),
  value: "new tag",
  color: "pink"
})

// commit
collection.updateSchema()

Todos

collection

  • collection.addRow({title:"",Tags:["tag1"]}) // add new row
  • updateSchema // update schema

row

  • row.delete() // delete a row

all

  • Rewrite in TypeScript

notabase's People

Contributors

mayneyao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

notabase's Issues

Crashing with empty record map

Used to work till a few days ago.

    async getPageCollectionInfo(pageId) {
        console.log(`>>>> getPageChunk:${pageId}`)
        let data = await this.reqeust.post(`/api/v3/loadPageChunk`,
            { "pageId": getFullBlockId(pageId), "limit": 50, "cursor": { "stack": [] }, "chunkNumber": 0, "verticalColumns": false }
        )
        console.log("getPageCollectionInfo -> data", data)

data prints:

{ recordMap: {}, cursor: { stack: [] } }

And then crashes on the next line:

        let collectionId = Object.entries(data.recordMap.collection)[0][0]

Is this a change in the Notion API or it's a bug from TypeScript branch updates?

setting a property does not update in Notion

I have created a simple Notion database where I am trying to update a text property.

I am the owner of the workspace, I am using the token, I can read from the actual Notion database via notabase.

But when I try to update a single property of a row as described in https://github.com/mayneyao/notabase/tree/ts#write I don't get any error and within the Node process the property is updated (reading from it gives the new value).

But for some reason the update is not propagated to Notion.
Any ideas what might be wrong?

Node.js Implementation

Hi, first of all thanks for your work with this lib, I started looking some alternatives to fetch from notion and I discover this awesome project. Unfortunately, I was trying to implement it in the backend side with nodejs, but It seems not work as a module import.

I tried with the base example in the readme, without luck.

import Notabase from 'notabase'

// node env
// just Read public data
let nb = new Notabase()

But I got this error (module problems)

node:44077) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
/Users/elihu/Documents/notion-scraping/notabase.js:1
import Notabase from 'notabase'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1063:16)
    at Module._compile (internal/modules/cjs/loader.js:1111:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

I also tried to import with "require" and assign the value to a constant but it's the same case i got the following error:

internal/modules/cjs/loader.js:1163
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/elihu/Documents/notion-scraping/node_modules/notabase/dist/index.js
require() of ES modules is not supported.
require() of /Users/elihu/Documents/notion-scraping/node_modules/notabase/dist/index.js from /Users/elihu/Documents/notion-scraping/notabase.js is an ES module file as it is a .js file whose nearest 
parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/elihu/Documents/notion-scraping/node_modules/notabase/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1163:13)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1036:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/Users/elihu/Documents/notion-scraping/notabase.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14) {
  code: 'ERR_REQUIRE_ESM'
}

I have not ideas, am I missing something?

My Nodejs version is: 13.10.1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.