notion_ruby_mapping 2.0.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -6
  3. data/exe/notionErDiagram.rb +30 -24
  4. data/lib/notion_ruby_mapping/blocks/base.rb +32 -4
  5. data/lib/notion_ruby_mapping/blocks/block.rb +0 -1
  6. data/lib/notion_ruby_mapping/blocks/column_block.rb +7 -2
  7. data/lib/notion_ruby_mapping/blocks/data_source.rb +167 -0
  8. data/lib/notion_ruby_mapping/blocks/database.rb +41 -21
  9. data/lib/notion_ruby_mapping/blocks/file_base_block.rb +3 -3
  10. data/lib/notion_ruby_mapping/blocks/list.rb +5 -5
  11. data/lib/notion_ruby_mapping/controllers/mermaid.rb +13 -13
  12. data/lib/notion_ruby_mapping/controllers/{mermaid_database.rb → mermaid_data_source.rb} +39 -39
  13. data/lib/notion_ruby_mapping/controllers/notion_cache.rb +42 -7
  14. data/lib/notion_ruby_mapping/controllers/property_cache.rb +26 -4
  15. data/lib/notion_ruby_mapping/objects/file_upload_object.rb +1 -1
  16. data/lib/notion_ruby_mapping/properties/checkbox_property.rb +1 -1
  17. data/lib/notion_ruby_mapping/properties/created_by_property.rb +2 -2
  18. data/lib/notion_ruby_mapping/properties/created_time_property.rb +1 -1
  19. data/lib/notion_ruby_mapping/properties/date_property.rb +1 -1
  20. data/lib/notion_ruby_mapping/properties/files_property.rb +2 -2
  21. data/lib/notion_ruby_mapping/properties/formula_property.rb +4 -4
  22. data/lib/notion_ruby_mapping/properties/last_edited_by_property.rb +2 -2
  23. data/lib/notion_ruby_mapping/properties/last_edited_time_property.rb +1 -1
  24. data/lib/notion_ruby_mapping/properties/multi_select_property.rb +4 -4
  25. data/lib/notion_ruby_mapping/properties/number_property.rb +4 -4
  26. data/lib/notion_ruby_mapping/properties/people_property.rb +2 -2
  27. data/lib/notion_ruby_mapping/properties/phone_number_property.rb +1 -1
  28. data/lib/notion_ruby_mapping/properties/property.rb +23 -5
  29. data/lib/notion_ruby_mapping/properties/relation_property.rb +26 -14
  30. data/lib/notion_ruby_mapping/properties/rich_text_property.rb +1 -1
  31. data/lib/notion_ruby_mapping/properties/rollup_property.rb +7 -7
  32. data/lib/notion_ruby_mapping/properties/select_property.rb +5 -5
  33. data/lib/notion_ruby_mapping/properties/text_property.rb +1 -1
  34. data/lib/notion_ruby_mapping/properties/title_property.rb +1 -1
  35. data/lib/notion_ruby_mapping/properties/unique_id_property.rb +1 -1
  36. data/lib/notion_ruby_mapping/properties/url_property.rb +1 -1
  37. data/lib/notion_ruby_mapping/version.rb +2 -2
  38. data/lib/notion_ruby_mapping.rb +3 -3
  39. data/tools/an +4 -4
  40. metadata +3 -9
  41. data/exe/erdToNotionDb.rb +0 -56
  42. data/exe/notionSitemap.rb +0 -131
  43. data/exe/notionTimeRecorder.rb +0 -284
  44. data/lib/notion_ruby_mapping/blocks/template_block.rb +0 -34
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 472fc98d3f7d94d134deced4cbbfb36810d0d2ea47c03922eee72e43e8c8b4fe
4
- data.tar.gz: 227bd9482a3c65879114de1c980be9de0153fd7d4e79c39f012f1b59206a043e
3
+ metadata.gz: 2939f0091b98158d3e7f65b47ecc9b8a1e5d87c9e0546ac0b44ae76c03b3c97a
4
+ data.tar.gz: 20ef7923ab54c9528a672c059ebac70d40c2f377156ec1f1789737bcd517bf13
5
5
  SHA512:
6
- metadata.gz: 910cab62b6fddad81b690b370664eca958130809aac2ff877eac6f6a8c75e2fa74db2de0d6adc9e00f396337d4fc05002b86493e2182bc74a20c4b70cdfa9a4e
7
- data.tar.gz: ce540ca17fdbec6c5793cfa45c0d15556d328c3b28e86e942612c295cd8fdf3601bf76675c2bf6fe345da4dd7bfe9135ba3dab46ec334dfe0cdc4e7d8cc38564
6
+ metadata.gz: 49bce2621fb722fa1a3b38210b97931fec3eae6b2535a162bc658b4922a96225497eb4748f01693ecb204129332ca35ef965a7c64abfe45ee64f0cd058b08833
7
+ data.tar.gz: af85f46e2068ac9f6569d5c4ba53e803a8bec5f08a28f7555b68ebb32e42c1e2b3313416c7451b6a86851c6be5411a5c3fd6441efcac9ee73ce80cfe4164538c
data/README.md CHANGED
@@ -15,9 +15,10 @@ Development note is here.
15
15
  - [notion\_ruby\_mapping](#notion_ruby_mapping)
16
16
  - [Table of Contents](#table-of-contents)
17
17
  - [0. History of Major Changes / 主な変更履歴](#0-history-of-major-changes--主な変更履歴)
18
- - [0.1 Changes in v0.7.0](#01-changes-in-v070)
19
- - [0.2 Changes in v0.6.0](#02-changes-in-v060)
20
- - [0.3 Changes in v0.5.0](#03-changes-in-v050)
18
+ - [0.1 Changes in v3.0.0](#01-changes-in-v300)
19
+ - [0.2 Changes in v0.7.0](#02-changes-in-v070)
20
+ - [0.3 Changes in v0.6.0](#03-changes-in-v060)
21
+ - [0.4 Changes in v0.5.0](#04-changes-in-v050)
21
22
  - [1. Installation / インストール方法](#1-installation--インストール方法)
22
23
  - [2. How to use / 利用方法](#2-how-to-use--利用方法)
23
24
  - [2.1 Create a New Integration / インテグレーションの作成](#21-create-a-new-integration--インテグレーションの作成)
@@ -32,13 +33,21 @@ Development note is here.
32
33
 
33
34
  ## 0. History of Major Changes / 主な変更履歴
34
35
 
35
- ### 0.1 Changes in v0.7.0
36
+ ### 0.1 Changes in v3.0.0
37
+
38
+ NotionRubyMapping v3.0.0 now supports Notion-Version 2025-09-03.
39
+
40
+ NotionRubyMapping v3.0.0 は、Notion-Version 2025-09-03 に対応しました。
41
+ これまで Database で実行していた API は、ほとんど DataSource の API に変更になりました。
42
+ そのため、NotionRubyMapping でも新規に DataSource クラスを追加しました。
43
+
44
+ ### 0.2 Changes in v0.7.0
36
45
 
37
46
  Since the number of Japanese users has increased, we decided to include Japanese as well. In addition, we have simplified the method of setting up integration tokens.
38
47
 
39
48
  日本のユーザが増えたので、日本語も併記することにしました。また、インテグレーショントークンの設定方法を簡単にしました。
40
49
 
41
- ### 0.2 Changes in v0.6.0
50
+ ### 0.3 Changes in v0.6.0
42
51
 
43
52
  NotionRubyMapping v0.6.0 now supports Notion-Version 2022-06-28.
44
53
  ~~In 2022-06-28, property values are no longer returned when retrieving pages. NotionRubyMapping temporarily creates a Property Object and calls the retrieve a property item API when a value is needed. Therefore, users do not need to be aware of any differences, and existing scripts should work as they are.~~
@@ -46,7 +55,7 @@ NotionRubyMapping v0.6.0 now supports Notion-Version 2022-06-28.
46
55
  NotionRubyMapping v0.6.0 は、Notion-Version 2022-06-28 に対応しました。
47
56
  ~~ 2022-06-28 では、ページを取得する際にプロパティ値を返さなくなりました。NotionRubyMapping は、一時的に Property Object を作成し、値が必要なときにプロパティ項目を取得する API を呼び出します。そのため、ユーザーはこの違いを意識する必要はなく、既存のスクリプトはそのまま動作するはずです。~~
48
57
 
49
- ### 0.3 Changes in v0.5.0
58
+ ### 0.4 Changes in v0.5.0
50
59
 
51
60
  NotionRubyMapping v0.5.0 now supports block updates.
52
61
  For efficiency, subclasses are provided under Block class. As a result, they are no longer compatible with the scripts used in v0.4.0.
@@ -127,6 +136,7 @@ NotionRubyMapping.configuration { |c| c.notion_token = ENV["NOTION_API_TOKEN"] }
127
136
 
128
137
  ## 3. ChangeLog
129
138
 
139
+ - 2025/9/15 [v3.0.0] updates for Notion-Version 2025-09-03 (Add DataSource class)
130
140
  - 2025/6/29 [v2.0.1] add creating FileUploadObject with external url
131
141
  - 2025/6/26 [v2.0.0] [FIX] remove symbolize_names: true (only use String)
132
142
  - 2025/6/11 [v1.0.0] Update Faraday to 2.13.1 and Change symbolize_names: true (Broken version)
@@ -3,53 +3,59 @@
3
3
  require "notion_ruby_mapping"
4
4
  include NotionRubyMapping
5
5
 
6
- def append_database(text, db, db_titles)
7
- base_title = db_title db
8
- normalize_db_title(db, db_titles) if db_titles[db].nil?
9
- text << " #{db_titles[db]} {"
10
- text << %( Database title "#{base_title}") unless base_title == db_titles[db]
11
- db.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
- class_name = p.class.name.split("::").last.sub /Property/, ""
6
+ def append_data_source(text, ds, ds_titles)
7
+ base_title = ds_title ds
8
+ normalize_ds_title(ds, ds_titles) if ds_titles[ds].nil?
9
+ text << " #{ds_titles[ds]} {"
10
+ text << %( Database title "#{base_title}") unless base_title == ds_titles[ds]
11
+ ds.properties.reject { |p| p.is_a? RelationProperty }.each_with_index do |p, i|
12
+ class_name = p.class.name.split("::").last.sub(/Property/, "")
13
13
  text << %( #{class_name} p#{i} "#{p.name}")
14
14
  end
15
15
  text << " }\n"
16
16
  end
17
17
 
18
- def normalize_db_title(db, db_titles)
19
- base_title = db_title db
18
+ def normalize_ds_title(db, db_titles)
19
+ base_title = ds_title db
20
20
  db_titles[db] = base_title.gsub(/[\w\d\-_]+/, "").empty? ? base_title : "d#{db_titles.count}"
21
21
  end
22
22
 
23
- def db_title(db)
24
- db.database_title.full_text.gsub " ", "_"
23
+ def ds_title(db)
24
+ db.data_source_title.full_text.gsub " ", "_"
25
25
  end
26
26
 
27
27
  if ARGV.length < 2
28
- print "Usage: notionErDiagram.rb top_database_id code_block_id"
28
+ print "Usage: notionErDiagram.rb top_data_source_id code_block_id"
29
29
  exit
30
30
  end
31
- database_id, code_block_id = ARGV
31
+ data_source_id, code_block_id = ARGV
32
32
  NotionRubyMapping.configure { |c| c.notion_token = ENV["NOTION_API_KEY"] }
33
33
  block = Block.find code_block_id
34
34
  unless block.is_a? CodeBlock
35
35
  print "#{code_block_id} is not CodeBlock's id"
36
36
  exit
37
37
  end
38
- dbs = [Database.find(database_id)]
38
+ dss = [DataSource.find(data_source_id)]
39
39
  text = %w[erDiagram]
40
40
 
41
41
  finished = {}
42
- db_titles = {}
43
- until dbs.empty?
44
- db = dbs.shift
45
- finished[db] = true
46
- append_database(text, db, db_titles)
47
- db.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, i|
48
- new_db = Database.find pp.relation_database_id
49
- normalize_db_title(new_db, db_titles) if db_titles[new_db].nil?
50
- text << " #{db_titles[db]} |o--o{ #{db_titles[new_db]} : r#{i}"
51
- dbs << new_db unless finished[new_db]
42
+ ds_titles = {}
43
+ until dss.empty?
44
+ ds = dss.shift
45
+ append_data_source(text, ds, ds_titles)
46
+ ds.properties.select { |pp| pp.is_a? RelationProperty }.each_with_index do |pp, _i|
47
+ new_ds = DataSource.find pp.relation_data_source_id
48
+ normalize_ds_title(new_ds, ds_titles) if ds_titles[new_ds].nil?
49
+ if pp.dual_property?
50
+ unless finished[new_ds]
51
+ text << %( #{ds_titles[ds]} }o--o{ #{ds_titles[new_ds]} : "#{pp.name} / #{pp.synced_property_name}" )
52
+ end
53
+ else
54
+ text << %( #{ds_titles[ds]} |o--o{ #{ds_titles[new_ds]} : "#{pp.name}")
55
+ end
56
+ dss << new_ds unless finished[new_ds]
52
57
  end
58
+ finished[ds] = true
53
59
  text << ""
54
60
  end
55
61
 
@@ -33,6 +33,8 @@ module NotionRubyMapping
33
33
  case json["object"]
34
34
  when "page"
35
35
  Page.new json: json
36
+ when "data_source"
37
+ DataSource.new json: json
36
38
  when "database"
37
39
  Database.new json: json
38
40
  when "list"
@@ -52,6 +54,14 @@ module NotionRubyMapping
52
54
  end
53
55
  end
54
56
 
57
+ # @param [String] str id or URL
58
+ # @return [String] data_source_id
59
+ def self.data_source_id(str)
60
+ NotionCache.instance.hex_id str
61
+ end
62
+
63
+ # @param [String] str id or URL
64
+ # @return [String] database_id
55
65
  def self.database_id(str)
56
66
  if /^http/.match str
57
67
  /([\da-f]{32})\?/.match(str)[1]
@@ -66,7 +76,7 @@ module NotionRubyMapping
66
76
  def self.dry_run_script(method, path, json = nil)
67
77
  shell = [
68
78
  "#!/bin/sh\ncurl #{method == :get ? "" : "-X #{method.to_s.upcase}"} 'https://api.notion.com/#{path}'",
69
- " -H 'Notion-Version: 2022-06-28'",
79
+ " -H 'Notion-Version: #{NotionRubyMapping::NOTION_VERSION}'",
70
80
  " -H 'Authorization: Bearer '\"$NOTION_API_KEY\"''",
71
81
  ]
72
82
  shell << " -H 'Content-Type: application/json'" if %i[post patch].include?(method)
@@ -166,10 +176,16 @@ module NotionRubyMapping
166
176
  # @return [NotionRubyMapping::Property] generated property
167
177
  def assign_property(klass, title)
168
178
  @property_cache ||= PropertyCache.new({},
169
- base_type: database? ? "database" : "page",
179
+ base_type: if database?
180
+ "database"
181
+ else
182
+ data_source? ? "data_source" : "page"
183
+ end,
170
184
  page_id: page? ? @id : nil)
171
185
  property = if database?
172
186
  klass.new(title, will_update: new_record?, base_type: "database")
187
+ elsif data_source?
188
+ klass.new(title, will_update: new_record?, base_type: "data_source")
173
189
  else
174
190
  klass.new(title, will_update: true, base_type: "page", property_cache: @property_cache)
175
191
  end
@@ -212,6 +228,11 @@ module NotionRubyMapping
212
228
  is_a? Database
213
229
  end
214
230
 
231
+ # @return [TrueClass, FalseClass] true if Database object
232
+ def data_source?
233
+ is_a? DataSource
234
+ end
235
+
215
236
  # @return [Hash, nil] obtained Hash
216
237
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
217
238
  def icon
@@ -280,7 +301,11 @@ module NotionRubyMapping
280
301
  reload
281
302
  end
282
303
  @property_cache = PropertyCache.new json_properties,
283
- base_type: database? ? "database" : "page",
304
+ base_type: if database?
305
+ "database"
306
+ else
307
+ data_source? ? "data_source" : "page"
308
+ end,
284
309
  page_id: page? ? @id : nil
285
310
  end
286
311
  @property_cache
@@ -343,7 +368,10 @@ module NotionRubyMapping
343
368
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#62eea67af7824496820c6bb903503540
344
369
  # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e13d526bd709451e9b06fd32e8d07fcd
345
370
  def set_icon(emoji: nil, url: nil, file_upload_object: nil)
346
- @payload.set_icon(emoji: emoji, url: url, file_upload_object: file_upload_object) if page? || database?
371
+ if page? || database? || data_source?
372
+ @payload.set_icon(emoji: emoji, url: url,
373
+ file_upload_object: file_upload_object)
374
+ end
347
375
  self
348
376
  end
349
377
 
@@ -44,7 +44,6 @@ module NotionRubyMapping
44
44
  table: TableBlock,
45
45
  table_row: TableRowBlock,
46
46
  table_of_contents: TableOfContentsBlock,
47
- template: TemplateBlock,
48
47
  to_do: ToDoBlock,
49
48
  toggle: ToggleBlock,
50
49
  video: VideoBlock,
@@ -5,11 +5,16 @@ module NotionRubyMapping
5
5
  class ColumnBlock < Block
6
6
  # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
7
7
  # @see https://www.notion.so/hkob/ColumnBlock-91cb314fc6594ff6b0c77c1eae6c7fab#4b4486cdf26a474eab1c92f9c6ea8a91
8
- def initialize(sub_blocks = [], json: nil, id: nil, parent: nil)
8
+ def initialize(sub_blocks = [], json: nil, id: nil, parent: nil, width_ratio: nil)
9
9
  super(json: json, id: id, parent: parent)
10
10
  add_sub_blocks sub_blocks unless json
11
11
  @can_have_children = true
12
12
  @can_append = false
13
+ @width_ratio = if !width_ratio.nil?
14
+ width_ratio
15
+ elsif json && json["column"] && json["column"]["width_ratio"]
16
+ json["column"]["width_ratio"]
17
+ end
13
18
  end
14
19
 
15
20
  # @return [String]
@@ -21,7 +26,7 @@ module NotionRubyMapping
21
26
  # @return [Hash{String (frozen)->Hash}]
22
27
  def block_json(not_update: true)
23
28
  ans = super
24
- ans[type] = {}
29
+ ans[type] = @width_ratio ? {"width_ratio" => @width_ratio} : {}
25
30
  ans[type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
26
31
  ans
27
32
  end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # Notion data_source
5
+ class DataSource < Base
6
+ ### Public announced methods
7
+
8
+ # @param [String] id
9
+ # @param [Boolean] dry_run true if dry_run
10
+ # @return [NotionRubyMapping::DataSource, String]
11
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#58ba9190fd544432a9e2a5823d6c33b7
12
+ def self.find(id, dry_run: false)
13
+ nc = NotionCache.instance
14
+ data_source_id = Base.data_source_id id
15
+ if dry_run
16
+ dry_run_script :get, nc.data_source_path(data_source_id)
17
+ else
18
+ nc.data_source data_source_id
19
+ end
20
+ end
21
+
22
+ # @param [String] key
23
+ # @return [NotionRubyMapping::PropertyCache, Hash] obtained Page value or PropertyCache
24
+ def [](key)
25
+ get key
26
+ end
27
+
28
+ # @param [Class] klass
29
+ # @param [String] title
30
+ # @return [NotionRubyMapping::Property]
31
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#c9d24269123c444295af88b9b27074a9
32
+ def add_property(klass, title)
33
+ prop = assign_property klass, title
34
+ yield prop if block_given?
35
+ @payload.merge_property prop.property_schema_json
36
+ prop.clear_will_update
37
+ prop
38
+ end
39
+
40
+ # @param [Array<Property, Class, String>] assign
41
+ # @return [NotionRubyMapping::Base]
42
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
43
+ def build_child_page(*assign)
44
+ assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
45
+ page = Page.new assign: assign, parent: {"data_source_id" => @id}
46
+ pp = page.properties
47
+ pp.clear_will_update
48
+ yield page, pp if block_given?
49
+ page
50
+ end
51
+
52
+ # @param [Array<Property, Class, String>] assign
53
+ # @param [Boolean] dry_run true if dry_run
54
+ # @return [NotionRubyMapping::Base]
55
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
56
+ def create_child_page(*assign, dry_run: false)
57
+ assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
58
+ page = Page.new assign: assign, parent: {"data_source_id" => @id}
59
+ pp = page.properties
60
+ pp.clear_will_update
61
+ yield page, pp if block_given?
62
+ page.save dry_run: dry_run
63
+ end
64
+
65
+ # @return [NotionRubyMapping::RichTextArray]
66
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#217a7d988c404d68b270c4874a9117b4
67
+ def data_source_title
68
+ @data_source_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
69
+ end
70
+
71
+ # @return [NotionRubyMapping::RichTextArray]
72
+ def description
73
+ RichTextArray.new "description", json: self["description"]
74
+ end
75
+
76
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
77
+ def description=(text_info)
78
+ @payload.description = text_info
79
+ end
80
+
81
+ # @return [Boolean]
82
+ def is_inline
83
+ self["is_inline"]
84
+ end
85
+
86
+ # @param [Boolean] flag
87
+ def is_inline=(flag)
88
+ @payload.is_inline = flag
89
+ end
90
+
91
+ # @return [Hash] created json for property schemas (for create data_source)
92
+ def property_schema_json
93
+ @payload.property_schema_json @property_cache, data_source_title
94
+ end
95
+
96
+ # @return [Hash] created json for property values
97
+ def property_values_json
98
+ @payload.property_values_json @property_cache, data_source_title
99
+ end
100
+
101
+ # @param [NotionRubyMapping::Query] query object
102
+ # @param [Boolean] dry_run true if dry_run
103
+ # @return [NotionRubyMapping::List, String]
104
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#6bd9acf62c454f64bc555c8828057e6b
105
+ def query_data_source(query = Query.new, dry_run: false)
106
+ if dry_run
107
+ Base.dry_run_script :post, @nc.query_data_source_path(@id), query.query_json
108
+ else
109
+ response = @nc.data_source_query_request @id, query
110
+ List.new json: response, type: "data_source", value: self, query: query
111
+ end
112
+ end
113
+
114
+ # @param [Array] property_names
115
+ # @return [Array<NotionRubyMapping::Property>]
116
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#5d15354be2604141adfbf78d14d49942
117
+ def remove_properties(*property_names)
118
+ property_names.map { |n| properties[n] }.each(&:remove)
119
+ end
120
+
121
+ # @param [String] old_property_name
122
+ # @param [String] new_property_name
123
+ # @return [NotionRubyMapping::DataSource]
124
+ def rename_property(old_property_name, new_property_name)
125
+ properties[old_property_name].new_name = new_property_name
126
+ self
127
+ end
128
+
129
+ # @return [Hash] created json for property schemas (for update data_source)
130
+ def update_property_schema_json
131
+ @payload.update_property_schema_json @property_cache, data_source_title
132
+ end
133
+
134
+ protected
135
+
136
+ # @param [Boolean] dry_run true if dry_run
137
+ def create(dry_run: false)
138
+ if dry_run
139
+ dry_run_script :post, @nc.data_sources_path, :property_schema_json
140
+ else
141
+ json = @nc.create_data_source_request(property_schema_json)
142
+ @new_record = false
143
+ update_json json
144
+ end
145
+ end
146
+
147
+ # @return [Hash]
148
+ def reload_json
149
+ @nc.data_source_request @id
150
+ end
151
+
152
+ # @param [Boolean] dry_run true if dry_run
153
+ # @return [NotionRubyMapping::Base, String]
154
+ def update(dry_run: false)
155
+ if dry_run
156
+ dry_run_script :patch, @nc.data_source_path(@id), :update_property_schema_json
157
+ else
158
+ payload = update_property_schema_json
159
+ begin
160
+ update_json @nc.update_data_source_request(@id, payload)
161
+ rescue StandardError => e
162
+ raise StandardError, "#{e.message} by #{payload}"
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -37,22 +37,46 @@ module NotionRubyMapping
37
37
  prop
38
38
  end
39
39
 
40
+ # @param [String] title
41
+ # @param [Array<String, Property>] assigns
42
+ # @return [NotionRubyMapping::DataSource]
43
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#71f680d59b874930bf9f488a7cd6a49e
44
+ def build_child_data_source(title, *assigns)
45
+ ds = DataSource.new json: {"title" => [TextObject.new(title).property_values_json]},
46
+ assign: assigns, parent: {"type" => "database_id", "database_id" => @id}
47
+ yield ds, ds.properties if block_given?
48
+ ds
49
+ end
50
+
51
+ # @param [String] title
52
+ # @param [Array<String, Property>] assigns
53
+ # @param [Boolean] dry_run true if dry_run
54
+ # @return [NotionRubyMapping::DataSource, String]
55
+ # @see https://www.notion.so/hkob/Page-d359650e3ca94424af8359a24147b9a0#e3f1d21e0f724f589e48431468772eed
56
+ def create_child_data_source(title, *assigns, dry_run: false)
57
+ ds = DataSource.new json: {"title" => [TextObject.new(title).property_values_json]},
58
+ assign: assigns, parent: {"type" => "database_id", "database_id" => @id}
59
+ yield ds, ds.properties if block_given?
60
+ ds.save dry_run: dry_run
61
+ end
62
+
40
63
  # @param [Array<Property, Class, String>] assign
41
64
  # @return [NotionRubyMapping::Base]
42
- # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
65
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
43
66
  def build_child_page(*assign)
44
- assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
45
- page = Page.new assign: assign, parent: {"database_id" => @id}
46
- pp = page.properties
47
- pp.clear_will_update
48
- yield page, pp if block_given?
49
- page
67
+ unless data_sources.length == 1
68
+ raise StandardError, "Database #{id} is linked with multiple data_sources, use data_source.build_child_page"
69
+ end
70
+
71
+ data_sources.first.build_child_page(*assign) do |page, pp|
72
+ yield page, pp if block_given?
73
+ end
50
74
  end
51
75
 
52
76
  # @param [Array<Property, Class, String>] assign
53
77
  # @param [Boolean] dry_run true if dry_run
54
78
  # @return [NotionRubyMapping::Base]
55
- # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
79
+ # @see https://www.notion.so/hkob/DataSource-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
56
80
  def create_child_page(*assign, dry_run: false)
57
81
  assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
58
82
  page = Page.new assign: assign, parent: {"database_id" => @id}
@@ -68,6 +92,15 @@ module NotionRubyMapping
68
92
  @database_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
69
93
  end
70
94
 
95
+ def data_sources
96
+ return @data_sources if @data_sources
97
+
98
+ reload if @json.nil? || !@json.key?("data_sources")
99
+ @data_sources = @json["data_sources"].map do |json|
100
+ DataSource.find json["id"]
101
+ end
102
+ end
103
+
71
104
  # @return [NotionRubyMapping::RichTextArray]
72
105
  def description
73
106
  RichTextArray.new "description", json: self["description"]
@@ -98,19 +131,6 @@ module NotionRubyMapping
98
131
  @payload.property_values_json @property_cache, database_title
99
132
  end
100
133
 
101
- # @param [NotionRubyMapping::Query] query object
102
- # @param [Boolean] dry_run true if dry_run
103
- # @return [NotionRubyMapping::List, String]
104
- # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#6bd9acf62c454f64bc555c8828057e6b
105
- def query_database(query = Query.new, dry_run: false)
106
- if dry_run
107
- Base.dry_run_script :post, @nc.query_database_path(@id), query.query_json
108
- else
109
- response = @nc.database_query_request @id, query
110
- List.new json: response, type: "database", value: self, query: query
111
- end
112
- end
113
-
114
134
  # @param [Array] property_names
115
135
  # @return [Array<NotionRubyMapping::Property>]
116
136
  # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#5d15354be2604141adfbf78d14d49942
@@ -3,18 +3,18 @@
3
3
  module NotionRubyMapping
4
4
  # Notion block
5
5
  class FileBaseBlock < Block
6
- # @param [String, nil] url
6
+ # @param [String, FileUploadObject, NilClass] url or file upload object
7
7
  # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>, nil] caption
8
8
  # @see https://www.notion.so/hkob/FileBlock-08f2aa6948364d00b92beacaac9a619c#ace53d2e6ff2404f937179ae1e966e98
9
9
  # @see https://www.notion.so/hkob/ImageBlock-806b3d2a9a2c4bf5a5aca6e3fbc8a7e2#4cb790b8b5c847acab4341d55e4fa66a
10
- def initialize(url = nil, caption: [], json: nil, id: nil, parent: nil)
10
+ def initialize(url_or_fuo = nil, caption: [], json: nil, id: nil, parent: nil)
11
11
  super(json: json, id: id, parent: parent)
12
12
  if @json
13
13
  @file_object = FileObject.new json: @json[type]
14
14
  decode_block_caption
15
15
  @can_append = @file_object.external?
16
16
  else
17
- @file_object = FileObject.file_object url
17
+ @file_object = FileObject.file_object url_or_fuo
18
18
  @caption = RichTextArray.rich_text_array "caption", caption
19
19
  end
20
20
  end
@@ -13,8 +13,8 @@ module NotionRubyMapping
13
13
  case type
14
14
  when "comment_parent"
15
15
  @comment_parent = value
16
- when "database"
17
- @database = value
16
+ when "data_source"
17
+ @data_source = value
18
18
  when "parent"
19
19
  @parent = value
20
20
  when "property"
@@ -40,9 +40,9 @@ module NotionRubyMapping
40
40
  query: -> { @parent.children @query },
41
41
  create_object: ->(json) { Base.create_from_json json },
42
42
  &block
43
- elsif @database
44
- each_sub base: @database,
45
- query: -> { @nc.database_query_request @database.id, @query },
43
+ elsif @data_source
44
+ each_sub base: @data_source,
45
+ query: -> { @nc.data_source_query_request @data_source.id, @query },
46
46
  create_object: ->(json) { Base.create_from_json json },
47
47
  &block
48
48
  elsif @property
@@ -5,10 +5,10 @@ module NotionRubyMapping
5
5
  # @param [String] full_text
6
6
  def initialize(full_text)
7
7
  @lines = full_text.split "\n"
8
- @databases = Hash.new {}
8
+ @data_sources = Hash.new {}
9
9
  parse_text
10
10
  end
11
- attr_reader :lines, :databases
11
+ attr_reader :lines, :data_sources
12
12
 
13
13
  def parse_text
14
14
  return unless @lines.shift =~ /^erDiagram/
@@ -25,7 +25,7 @@ module NotionRubyMapping
25
25
  end
26
26
 
27
27
  def db_or_create(db_name)
28
- @databases[db_name] ||= MermaidDatabase.new db_name
28
+ @data_sources[db_name] ||= MermaidDataSource.new db_name
29
29
  end
30
30
 
31
31
  def append_db_with_attributes(db_name)
@@ -34,7 +34,7 @@ module NotionRubyMapping
34
34
  case @lines.shift
35
35
  when /^ *} *$/
36
36
  break
37
- when /^ *Database +title +"(.*)" *$/
37
+ when /^ *DataSource +title +"(.*)" *$/
38
38
  db.name = Regexp.last_match(1)
39
39
  when /^ *([^ ]+) +[^ ]+ +"(.*)" *$/
40
40
  db.add_property Regexp.last_match(1), Regexp.last_match(2)
@@ -44,36 +44,36 @@ module NotionRubyMapping
44
44
  end
45
45
  end
46
46
 
47
- def attach_database(db)
48
- @databases.values.select { |mdb| mdb.name == db.database_title.full_text }.first&.attach_database(db)
47
+ def attach_data_source(ds)
48
+ @data_sources.values.select { |mds| mds.name == ds.data_source_title.full_text }.first&.attach_data_source(ds)
49
49
  end
50
50
 
51
51
  def create_notion_db(target_page, inline)
52
- @databases.each_value do |mdb|
52
+ @data_sources.each_value do |mdb|
53
53
  mdb.create_notion_db(target_page, inline) unless mdb.real_db
54
54
  end
55
55
  end
56
56
 
57
57
  def update_title
58
- @databases.each_value do |mdb|
58
+ @data_sources.each_value do |mdb|
59
59
  mdb.update_title
60
60
  end
61
61
  end
62
62
 
63
- def update_databases
64
- @databases.each_value(&:update_database)
63
+ def update_data_sources
64
+ @data_sources.each_value(&:update_database)
65
65
  end
66
66
 
67
67
  def rename_reverse_name
68
- @databases.each_value(&:rename_reverse_name)
68
+ @data_sources.each_value(&:rename_reverse_name)
69
69
  end
70
70
 
71
71
  def count
72
- @databases.values.map(&:count).sum
72
+ @data_sources.values.map(&:count).sum
73
73
  end
74
74
 
75
75
  def remain
76
- @databases.values.map(&:remain).sum
76
+ @data_sources.values.map(&:remain).sum
77
77
  end
78
78
  end
79
79
  end