Skip to content

Commit

Permalink
✨ Add Notion plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
julien040 committed Jul 9, 2024
1 parent 9fef0c0 commit 68564b5
Show file tree
Hide file tree
Showing 18 changed files with 1,726 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugins/notion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
devManifest.json
*.out
*.log

dist/
36 changes: 36 additions & 0 deletions plugins/notion/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com

# The lines below are called `modelines`. See `:help modeline`
# Feel free to remove those if you don't want/need to use them.
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 2

before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy

builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- windows
- darwin

goarch:
- amd64
- arm64

archives:
- format: binary

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
4 changes: 4 additions & 0 deletions plugins/notion/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# LICENSE

Copyright © 2024 Julien CAGNIART

68 changes: 68 additions & 0 deletions plugins/notion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Notion plugin

This plugin allows you to interact with Notion databases. You can read/insert/update/delete records in a database.

## Installation

You need [Anyquery](https://github.com/julien040/anyquery) to run this plugin.

Then, install the plugin with the following command:

```bash
anyquery install notion
```

At some point, you will be asked to provide your Notion API key. You can find it by creating an integration.

### Find your Notion API key

1. Go to [Notion's My Integrations page](https://www.notion.so/my-integrations).
2. Click on the `+ New integration` button.

![Home of Notion integrations](https://github.com/julien040/anyquery/blob/main/plugins/notion/images/creator-profile.png)
3. Fill in the form with the following information:
1. Name: Whatever you want
2. Associated workspace: The workspace you want the plugin to have access to
3. Type: Internal

![A form to create a new integration](https://github.com/julien040/anyquery/blob/main/plugins/notion/images/form-integration.png)
4. Click on the `Save` button and on `Configure integration settings`.

![alt text](https://github.com/julien040/anyquery/blob/main/plugins/notion/images/success.png)
5. Copy the `token` and paste it when asked by the plugin.

![alt text](https://github.com/julien040/anyquery/blob/main/plugins/notion/images/token.png)

### Finding the database ID

Once you have your API key, you need to find the database ID of the database you want to interact with. You can find it in the URL of the database. For example, if the URL of the database is `https://www.notion.so/myworkspace/My-Database-1234567890abcdef1234567890abcdef`, the database ID is `1234567890abcdef1234567890abcdef`.

## Usage

The plugin supports all the basic SQL operations. Here are some examples:

```sql
SELECT * FROM notion_database;

SELECT * FROM notion_database WHERE name = 'Michael';

INSERT INTO notion_database (name, age) VALUES ('Michael', 25);

UPDATE notion_database SET age = 26 WHERE name = 'Michael';

DELETE FROM notion_database WHERE name = 'Michael';
```

## Known limitations

- Rollup and UniqueID properties are not supported.
- Due to the nature of formulas, a column can have different types depending on the row. This can lead to unexpected results when filtering records.
- Because SQLite does not support arrays, the plugin will return a JSON representation of the array. For example, `["a", "b", "c"]` will be returned as `'["a", "b", "c"]'`. <br>
You can then use the [JSON operator](https://www.sqlite.org/json1.html#the_and_operators) like in PostgreSQL to query the data. For example, `SELECT "Files & media" ->> '$[0]' FROM notion_database;` will return the first element of the array.
- You cannot create/update files, formulas, or rollup properties. You cannot update the cover and icon properties of a page.
- `DELETE FROM` operations only trash the record. You can restore it from the Notion interface.
- Because SQLite does not have a `BOOLEAN` type, the plugin will return `0` for `false` and `1` for `true`.
- Dates are returned as strings in the format `YYYY-MM-DDTHH:MM:SSZ`(RFC3339). If an end date is specified, it will be returned as a string in the format `YYYY-MM-DDTHH:MM:SSZ/YYYY-MM-DDTHH:MM:SSZ`. <br>
When inserting/updating a date, you can specify the date as YYYY-MM-DD, DD/MM/YYYY, RFC3339, or a Unix timestamp. If you want to specify a time, you can use the format `YYYY-MM-DDTHH:MM:SSZ`.
- Rate limit: Notion has a rate limit of 3 requests per second. While the plugin automatically handles retries, it may slow down the execution of your queries.
For example, if you run a query that inserts 100 records, it will take at least 33 seconds to complete. And you will read at most 300 records per second.
26 changes: 26 additions & 0 deletions plugins/notion/database_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"context"
"fmt"

"github.com/jomei/notionapi"
)

func (t *table) Delete(primaryKeys []interface{}) error {
for _, pk := range primaryKeys {
primaryKey, ok := pk.(string)
if !ok {
return fmt.Errorf("invalid page id: %v", pk)
}

_, err := t.client.Page.Update(context.Background(), notionapi.PageID(primaryKey), &notionapi.PageUpdateRequest{
Archived: true,
Properties: map[string]notionapi.Property{},
})
if err != nil {
return err
}
}
return nil
}
95 changes: 95 additions & 0 deletions plugins/notion/database_insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package main

import (
"context"
"log"
"net/url"

"github.com/jomei/notionapi"
)

func (t *table) Insert(rows [][]interface{}) error {
for _, row := range rows {
pageRequest := &notionapi.PageCreateRequest{
Properties: map[string]notionapi.Property{},
Parent: notionapi.Parent{
Type: notionapi.ParentTypeDatabaseID,
DatabaseID: notionapi.DatabaseID(t.database.ID),
},
}

for i, colName := range t.columns {
// Skip the system columns
if colName == "_page_id" || colName == "_page_url" ||
colName == "_created_time" || colName == "_last_edited_time" {
continue
}

if colName == "_icon_url" || colName == "_cover_url" {
// Check if the value is an URL
if i < len(row) {
value, ok := row[i].(string)
if !ok {
log.Printf("Invalid icon URL: %+v", row[i])
continue
}
parsed, err := url.Parse(value)
if err != nil {
log.Printf("Invalid icon URL: %+v", row[i])
continue
}
if parsed.Scheme == "" || parsed.Host == "" {
log.Printf("Invalid icon URL: %+v", row[i])
continue
}
if colName == "_icon_url" {
pageRequest.Icon = &notionapi.Icon{
Type: notionapi.FileTypeExternal,
External: &notionapi.FileObject{
URL: value,
},
}
} else if colName == "_cover_url" {
pageRequest.Cover = &notionapi.Image{
Type: notionapi.FileTypeExternal,
External: &notionapi.FileObject{
URL: value,
},
}
}

}
}

// Get the property of the column
prop, ok := t.database.Properties[colName]
if !ok {
continue
}

// Get the value of the column
var value interface{}
if i < len(row) {
value = row[i]
} else {
value = nil
}

// Convert the value to a Notion property
propValue := marshal(value, prop)
if propValue == nil {
continue
}
pageRequest.Properties[colName] = propValue

}
// Create the page
_, err := t.client.Page.Create(context.Background(), pageRequest)
if err != nil {
return err
}

}

return nil
}
Loading

0 comments on commit 68564b5

Please sign in to comment.