찬찬의 Notion API 정복기 20 - API 집중분석 - Create a database

소찬 (Chan)·2022년 11월 29일
0
post-thumbnail

Reference API URL : Create a database

생성하려면 보니 첫 parameter 값이 parent 입니다.

  • parent (json) required
    A page parent

parent 값을 pageparent, 즉, page가 부모인 곳에서 생성이 가능합니다. database가 부모인 곳에서의 생성은 지원하지 않습니다.
page_id를 알려면, search 를 활용하시면 됩니다.
filter={"property": "object", "value": "page"}로 설정해두고 search 를 호출해오시면 API Bot 이 권한있는 page 목록이 보일겁니다.

pages = notion.search(filter={"property": "object", "value": "page"}

여기서 추가할 page_id를 가져올 겁니다.

 'results': [{'archived': False,
              ... ,
              'icon': {'emoji': '🍕', 'type': 'emoji'},
              'id': '1f217cc1-225f-4580-8e22-cd7eb8db3c5e',
              ... ,
              'object': 'page',
              'parent': {'database_id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
                         'type': 'database_id'},

보면 'parent''database_id'가 있는데, 저희가 필요한 값은 이 'page''id' 값입니다. 위의 목록상 첫번째([0]) 항목의 ['id'] 값을 가져오시면 됩니다.

page_id = pages['results'][0]['id']
parent_id = {'type': 'page_id', 'page_id': page_id}
  • title (array)
    Title of database as it appears in Notion. An array of rich text objects.
    'title' 값도 설정해둡니다. array 이므로 list 형태로 입력해주면 되겠죠.
title_value = {'type': 'text', 'text': {'content': '피자 토핑 목록', 'link': None}}

피자 토핑이니까 icon도 넣어줍니다. optional 이므로 필수는 아닙니다.

icon_value = {'emoji': '🍕', 'type': 'emoji'}
  • properties (json) required
    Property schema of database. The keys are the names of properties as they appear in Notion and the values are property schema objects.
    properties 역시 필수 값이네요.

Property Schema Object 문서 참조하면 어떤 형식으로 작성하는지 설명이 나와있다고 되어있네요.
문서 보면서 작성해도 좋지만, 제일 좋은 예시는 기존에 작성된 databaseproperties를 참조하는 것입니다.

그럼 기존의 properties의 구성을 확인해볼까요?

{'archived': False,
 ... ,
 'icon': {'emoji': '🍕', 'type': 'emoji'},
 'id': '021eb424-7a41-47ae-9252-f67d4f6e6b05',
 ... ,
 'properties': {'상태': {'id': '%3EQQn',
                        'name': '상태',
                        'select': {'options': [{'color': 'green',
                                                'id': '6da478a3-5307-4d77-a47a-934a86ddda48',
                                                'name': '★★★★★'},
                                               {'color': 'brown',
                                                'id': '588ecfe9-05b0-40c2-aa32-3f26ee0b5281',
                                                'name': '★★★★'},
                                               {'color': 'purple',
                                                'id': '8e6092bc-4dcf-42fc-ae54-5e6bf1586c48',
                                                'name': '★★★'},
                                               {'color': 'orange',
                                                'id': 'ed26ef07-df97-4ced-9275-9aa0c71d5bb1',
                                                'name': '★★'},
                                               {'color': 'gray',
                                                'id': '632758dd-d74c-455c-8a3b-0d063282a3a7',
                                                'name': '★'}]},
                                    'type': 'select'}, ...

위의 구조를 참조해서 작성을 해봅니다.
select 형태의 property는 다음과 같이 작성하면 되겠죠.

select_property = {'평점': {'name': '평점',
                   'select': {'options': [{'color': 'green', 'name': '★★★'},
                                          {'color': 'purple', 'name': '★★'},
                                          {'color': 'gray', 'name': '★'}]
                             },
                   'type': 'select'}
                   }

property 하나만 추가할 순 있으나, 하나 더 추가해보겠습니다.
'rich_text' 형태 예시를 가져와 봤습니다.

'코멘트': {'id': '%5C%3D%7B%5E',
         'name': '코멘트',
         'rich_text': {},
         'type': 'rich_text'},

property'메모'라는 이름의 property'rich_text' 형태로 설정하고 싶을 때 다음과 같이 입력해주면 됩니다.

rich_text_property = {'메모': {'name': '메모',
                              'type': 'rich_text',
                              'rich_text': {}
                              }
                      }

이제 create를 호출해보...기전에 뭔가 하나 빠졌습니다. property 에는 key 값이 존재합니다. key 값은 title로 되어있습니다. title이 설정되어 있지 않을 경우, notion_client.errors.APIResponseError: Title is not provided 오류가 발생합니다.

'맛집명': {'id': 'title',
         'name': '맛집명',
         'title': {},
         'type': 'title'}

그럼 위 형태로 title_property를 입력합니다.

title_property = {'맛집명': {'id': 'title',
                           'name': '맛집명',
                           'title': {},
                           'type': 'title'
                           }
                 }

propertiesjson이므로 dictonary 형태로 묶어줍니다.

properties_value = {}
properties_value.update(select_property)
properties_value.update(rich_text_property)
properties_value.update(title_property)

이제 create 하여 database를 생성해줍니다.

notion.databases.create(parent=parent_id, title=title_value, icon=icon_value, properties=properties_value)

최상단에 데이터베이스를 만드는게 가능할까요?

파라메터 값은, parent = {'type': 'workspace', 'workspace': True}로 주시면 됩니다. (Workspace parent 문서 참조)
다만 안타깝게도 body failed validation: body.parent.page_id should be defined, instead was 'undefined' 오류가 나오는 걸 보니, 아직은 최상단(workspace)에서 database를 생성할 수 없는 걸로 파악이 됩니다.

profile
QA Specialist

0개의 댓글