Class: NotionRubyMapping::Database

Inherits:
Base
  • Object
show all
Defined in:
lib/notion_ruby_mapping/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

#[], #assign_property, #children, create_from_json, #created_time, #database?, dry_run_script, #icon, #initialize, #json_properties, #last_edited_time, #new_record?, #page?, #properties, #reload, #restore_from_json, #save, #set_icon, #title, #update_json

Constructor Details

This class inherits a constructor from NotionRubyMapping::Base

Class Method Details

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

Parameters:

  • id (String)

Returns:



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

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

Parameters:

  • klass (Class)
  • title (String)

Yields:

  • (prop)

Returns:



22
23
24
25
26
27
28
# File 'lib/notion_ruby_mapping/database.rb', line 22

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

#create_child_page(*assign) ⇒ NotionRubyMapping::Base

Parameters:

  • assign (Array<Property, Class, String>)

Returns:



32
33
34
# File 'lib/notion_ruby_mapping/database.rb', line 32

def create_child_page(*assign)
  Page.new assign: assign, parent: {"database_id" => @id}
end

#database_titleNotionRubyMapping::RichTextArray



37
38
39
# File 'lib/notion_ruby_mapping/database.rb', line 37

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

#property_schema_jsonObject



41
42
43
# File 'lib/notion_ruby_mapping/database.rb', line 41

def property_schema_json
  @payload.property_schema_json @property_cache, database_title
end

#property_values_jsonHash

Returns created json for update database.

Returns:

  • (Hash)

    created json for update database



46
47
48
# File 'lib/notion_ruby_mapping/database.rb', line 46

def property_values_json
  @payload.property_values_json @property_cache, database_title
end

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

Parameters:

Returns:



52
53
54
55
56
57
58
59
# File 'lib/notion_ruby_mapping/database.rb', line 52

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(*names) ⇒ Array<NotionRubyMapping::Property>

Parameters:

  • names (Array)

Returns:



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

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

#update_property_schema_jsonHash

Returns created json for update database.

Returns:

  • (Hash)

    created json for update database



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

def update_property_schema_json
  @payload.update_property_schema_json @property_cache, database_title
end