Class: NotionRubyMapping::Database

Inherits:
Base
  • Object
show all
Defined in:
lib/notion_ruby_mapping/blocks/database.rb

Overview

Notion database

Instance Attribute Summary

Attributes inherited from Base

#id, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #append_block_children, #assert_parent_children_pair, #assign_property, #block?, #children, #create_child_breadcrumb, create_from_json, #created_time, #database?, #description, dry_run_script, #icon, #initialize, #json_properties, #last_edited_time, #new_record?, #page?, #properties, #reload, #restore_from_json, #save, #set_icon, #update_json

Constructor Details

This class inherits a constructor from NotionRubyMapping::Base

Class Method Details

.find(id, dry_run: false) ⇒ NotionRubyMapping::Database, String



11
12
13
14
15
16
17
18
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 11

def self.find(id, dry_run: false)
  nc = NotionCache.instance
  if dry_run
    dry_run_script :get, nc.database_path(id)
  else
    nc.database id
  end
end

Instance Method Details

#add_property(klass, title) {|prop| ... } ⇒ NotionRubyMapping::Property



24
25
26
27
28
29
30
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 24

def add_property(klass, title)
  prop = assign_property klass, title
  yield prop if block_given?
  @payload.merge_property prop.property_schema_json
  prop.clear_will_update
  prop
end

#build_child_page(*assign) {|page, pp| ... } ⇒ NotionRubyMapping::Base



35
36
37
38
39
40
41
42
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 35

def build_child_page(*assign)
  assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
  page = Page.new assign: assign, parent: {"database_id" => @id}
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page
end

#create_child_page(*assign, dry_run: false) {|page, pp| ... } ⇒ NotionRubyMapping::Base



47
48
49
50
51
52
53
54
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 47

def create_child_page(*assign, dry_run: false)
  assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
  page = Page.new assign: assign, parent: {"database_id" => @id}
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page.save dry_run: dry_run
end

#database_titleNotionRubyMapping::RichTextArray



58
59
60
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 58

def database_title
  @database_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
end

#property_schema_jsonHash

Returns created json for property schemas (for create database).

Returns:

  • (Hash)

    created json for property schemas (for create database)



63
64
65
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 63

def property_schema_json
  @payload.property_schema_json @property_cache, database_title
end

#property_values_jsonHash

Returns created json for property values.

Returns:

  • (Hash)

    created json for property values



68
69
70
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 68

def property_values_json
  @payload.property_values_json @property_cache, database_title
end

#query_database(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String



75
76
77
78
79
80
81
82
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 75

def query_database(query = Query.new, dry_run: false)
  if dry_run
    Base.dry_run_script :post, @nc.query_database_path(@id), query.query_json
  else
    response = @nc.database_query @id, query
    List.new json: response, database: self, query: query
  end
end

#remove_properties(*property_names) ⇒ Array<NotionRubyMapping::Property>



87
88
89
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 87

def remove_properties(*property_names)
  property_names.map { |n| properties[n] }.each(&:remove)
end

#rename_property(old_property_name, new_property_name) ⇒ NotionRubyMapping::Database

Parameters:

  • old_property_name (String)
  • new_property_name (String)

Returns:



94
95
96
97
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 94

def rename_property(old_property_name, new_property_name)
  properties[old_property_name].new_name = new_property_name
  self
end

#update_property_schema_jsonHash

Returns created json for property schemas (for update database).

Returns:

  • (Hash)

    created json for property schemas (for update database)



100
101
102
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 100

def update_property_schema_json
  @payload.update_property_schema_json @property_cache, database_title
end