🧪

erdToNotionDb sample

This script is currently in progress.
💡
This command assumes that the enviroment variable NOTION_API_KEY contains your integration key. Please set this environment variable before execution.
# bash, zsh export NOTION_API_KEY=secret_.......... # fish set -gx NOTION_API_KEY secret_.......
environment setting
 

How to use

erdToNotionDb.rb creates Notion databases from ER diagrams in mermaid code block. At first, you invite your integration to your pages
  • a page including the mermaid code block,
  • a page for creating or updating databases.
After inviting, please type command as follows.
# Pattern1: use id erdToNotionDb.rb code_block_id page_id --inline Example: erdToNotionDb.rb e6e0e92965f046e7b383fac6dea4590f 87e5e52a6b9f46abbdeebcb3c902a516 --inline # Pattern2: use url (needs "") erdToNotionDb.rb "https://www.notion.so/hkob/erdToNotionDb-sample-87e5e52a6b9f46abbdeebcb3c902a516#e6e0e92965f046e7b383fac6dea4590f" "https://www.notion.so/hkob/erdToNotionDb-sample-87e5e52a6b9f46abbdeebcb3c902a516" --inline

Format of ER diagram for creating Notion database

1. Database title

  • The first line is erDiagram
  • In Notion databases, titles often include non-ASCII characters such as spaces, symbols, and CJK characters. To ensure accuracy, please use keywords for Mermaid database titles.
  • Instead, enter the Database title in the comment field of the attribute.
  • The title property is required in the Notion database. Please make sure to provide one.
 
If you run the script in this state, it will be like this.
erDiagram db1 { Database title "Task database" title Name }
⚒️
Created database
notion imagenotion image

2. Update database

  • If there is already corresponding database on the page, it will be updated.
  • The title can only exist once in the database, so it will change if it is rewritten.
  • When creating databases in Notion, property names may include non-ASCII characters such as spaces, symbols, and CJK characters. To ensure accuracy, please use keywords for Mermaid attribute names and enter the property name in the comment field of the attribute.
    • erDiagram db1 { Database title "Task database" title p0 "Task name" }
      ⚒️
      Updated title
      notion imagenotion image

3. Add other properties (without relation and rollup)

  • You can add properties other than the title. The following types are set without any options.
    • checkbox
    • created_by
    • created_time
    • date
    • email
    • files
    • last_edited_by
    • last_edited_time
    • people
    • phone_number
    • rich_text
    • url
    • status
  • The following properties can be specified with an option. The option is written after the |.
    • formula|formula_expression
      • ex1: formula … formula without expression
      • ex2: formula|now()
      • ex3: prop(@Progress@) … Please use @ instead.
    • multi_select|selection_name1|selection_name2…
      • ex1: multi_select … multi_select without selections
      • ex2: multi_select|SEL1|SEL2|SEL3
    • number|format
      • ex1: number … number with "number"
      • ex2: number|percent
    • select|selection_name1|selection_name2…
      • ex1: select … multi_select without selections
      • ex2: select|SEL1|SEL2|SEL3
      erDiagram db1 { Database title "Task database" title p0 "Task name" status Status date Date }
      ⚒️
      Add date and status properties
      notion imagenotion image

4. Add a second database and establish a relationship between them.

  • The page limit of the database cannot be set with the API, so any symbol is fine.
  • When the name of the relation is separated by a |, a bidirectional relation is created.
    • erDiagram db1 }o--|| Project : "project|tasks" db1 { Database title "Task database" title p0 "Task name" status Status date Date } Project { title p0 "Project name" }
      ⚒️
      Created bidirectional relation
      notion imagenotion image

5. Add rollup property

Finally, I will explain how to create a roll-up. In the case of a roll-up, the comment field should be written in the order of "property name | relation name | roll-up name | function". The available functions are as follows.
average, checked, count, count_per_group, count_values, date_range, earliest_date, empty, latest_date, max, median, min, not_empty, percent_checked, percent_empty, percent_not_empty, percent_per_group, percent_unchecked, range, show_original, show_unique, sum, unchecked, unique,
erDiagram db1 }o--|| Project : "project|tasks" db1 { Database title "Task database" title p0 "Task name" status Status date Date } Project { title p0 "Project name" rollup p1 "Progress|tasks|Status|percent_per_group" }
⚒️
Created database
notion imagenotion image
 

Limitation

  1. You cannot set the Status property through the API, so please convert it to another property first and then convert it back to Status.
  1. Settings with dependencies cannot be created at once. We have made it possible to create Relations with Rollup at the same time as setting. We plan to update the script taking into account the dependencies. → I released erdToNotionDb to correspond to dependencies. If there were dependencies, it would block and try to generate in the next pass.
 

Mermaid sample 1

erDiagram db1 }o--|| Project : "project|tasks" db1 { Database title "Task database" title p0 "Task name" checkbox Done date Date rollup p1 "Project progress|project|_Progress|max" } Project { title p0 "Project name" rollup p1 "Progress|tasks|Done|percent_checked" formula p2 "_Progress|prop(@Progress@)" }
 

Generated tables 1

Task database
Task name
Date
Done
Project progress
project
Project
Project name
Progress
_Progress
tasks

Mermaid sample 2

erDiagram db1 }o--|| db2 : "プロジェクト|タスク" db1 { Database title "タスク" title p0 "タスク名" checkbox p1 "完了" date p2 "日付" rollup p1 "プロジェクト完了率|プロジェクト|_完了率|max" } db2 { Database title "プロジェクト" title p0 "プロジェクト名" rollup p1 "完了率|タスク|完了|percent_checked" formula p2 "_完了率|prop(@完了率@)" }

Generated tables 2

タスク
タスク名
プロジェクト完了率
完了
プロジェクト
日付
プロジェクト
プロジェクト名
タスク
完了率
_完了率