@@ -87,7 +87,7 @@ type PageData struct {
8787
8888func ParsePage (page * notionapi.Page ) (PageUID , * PageData ) {
8989 var pageUID PageUID
90- pageID := GetPageID (page .ID )
90+ pageID := GetStandardID (page .ID )
9191 data := make (map [string ]string )
9292 for name , property := range page .Properties {
9393 data [name ] = ParseProperty (property )
@@ -165,6 +165,22 @@ func (n *Notion) WithConfig(pageID, databaseID string) *Notion {
165165}
166166
167167func (n * Notion ) Init () error {
168+ if n .DatabaseID == "" && n .PageID == "" {
169+ return fmt .Errorf ("both database_id and page_id are empty" )
170+ }
171+ // 1. 未填写database_id则创建数据库
172+ if n .DatabaseID == "" && n .PageID != "" {
173+ db , err := n .CreateDB ()
174+ if err != nil {
175+ return fmt .Errorf ("create database failed: %v" , err )
176+ }
177+ n .DatabaseID = notionapi .DatabaseID (GetStandardID (db .ID ))
178+
179+ log .Printf ("Create Database: %d, Please add the database_id in the config file\n " , n .DatabaseID )
180+ log .Printf ("Visited Link: %s" , fmt .Sprintf ("https://www.notion.so/%s" , n .DatabaseID ))
181+ }
182+
183+ // 2. 创建完成database_id则添加记录
168184 if n .DatabaseID != "" {
169185 records , err := n .Query ()
170186 if err != nil {
@@ -237,10 +253,36 @@ func (n *Notion) InsertOrUpdate(record *Record) error {
237253 return n .Insert (record )
238254}
239255
256+ var EmptySelect = notionapi.Select {Options : make ([]notionapi.Option , 0 )}
257+
258+ func (n * Notion ) CreateDB () (db * notionapi.Database , err error ) {
259+ ctx := context .Background ()
260+
261+ dbReq := & notionapi.DatabaseCreateRequest {
262+ Parent : notionapi.Parent {
263+ Type : "page_id" ,
264+ PageID : n .PageID ,
265+ },
266+ Title : []notionapi.RichText {{Text : & notionapi.Text {Content : "Leetcode" }}},
267+ Properties : notionapi.PropertyConfigs {
268+ "Link" : & notionapi.URLPropertyConfig {Type : "url" },
269+ "Tags" : & notionapi.MultiSelectPropertyConfig {Type : "multi_select" , MultiSelect : EmptySelect },
270+ "Solved" : & notionapi.SelectPropertyConfig {Type : "select" , Select : EmptySelect },
271+ "Difficulty" : & notionapi.SelectPropertyConfig {Type : "select" , Select : EmptySelect },
272+ "ID" : & notionapi.RichTextPropertyConfig {Type : "rich_text" },
273+ "_id" : & notionapi.RichTextPropertyConfig {Type : "rich_text" },
274+ "Name" : & notionapi.TitlePropertyConfig {Type : "title" },
275+ },
276+ IsInline : true , // show database inline in the parent page
277+ }
278+
279+ return n .client .Database .Create (ctx , dbReq )
280+ }
281+
240282func GetPageSig (v map [string ]string ) string {
241283 return fmt .Sprintf ("%s_%s_%s_%s_%s_%s" , v ["Difficulty" ], v ["ID" ], v ["Link" ], v ["Name" ], v ["Solved" ], v ["Tags" ])
242284}
243285
244- func GetPageID ( objectID notionapi. ObjectID ) string {
245- return strings .ReplaceAll (objectID .String (), "-" , "" )
286+ func GetStandardID ( stringer fmt. Stringer ) string {
287+ return strings .ReplaceAll (stringer .String (), "-" , "" )
246288}
0 commit comments