Module: Notion::Api::Endpoints::Databases

Included in:
Notion::Api::Endpoints
Defined in:
lib/notion/api/endpoints/databases.rb

Instance Method Summary collapse

Instance Method Details

#create_database(options = {}) ⇒ Object

Creates a new database in the specified page.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :parent (Object)

    Parent of the database, which is always going to be a page.

  • :title (Object)

    Title of this database.

  • :properties (Object)

    Property schema of database. The keys are the names of properties as they appear in Notion and the values are property schema objects. Property Schema Object is a metadata that controls how a database property behaves, e.g. {}. Each database must have exactly one database property schema object of type “title”.



62
63
64
65
66
67
# File 'lib/notion/api/endpoints/databases.rb', line 62

def create_database(options = {})
  throw ArgumentError.new('Required arguments :parent.page_id missing') if options.dig(:parent, :page_id).nil?
  throw ArgumentError.new('Required arguments :title missing') if options.dig(:title).nil?
  throw ArgumentError.new('Required arguments :properties missing') if options.dig(:properties).nil?
  post('databases', options)
end

#database(options = {}) ⇒ Object

Retrieves a Database object using the ID specified in the request.

Returns a 404 HTTP response if the database doesn’t exist, or if the bot doesn’t have access to the database. Returns a 429 HTTP response if the request exceeds Notion’s Request limits.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :database_id (id)

    Database to get info on.



100
101
102
103
# File 'lib/notion/api/endpoints/databases.rb', line 100

def database(options = {})
  throw ArgumentError.new('Required arguments :database_id missing') if options[:database_id].nil?
  get("databases/#{options[:database_id]}")
end

#database_query(options = {}) ⇒ Object

Gets a paginated array of Page object s contained in the requested database, filtered and ordered according to the filter and sort objects provided in the request.

Filters are similar to the filters provided in the Notion UI. Filters operate on database properties and can be combined. If no filter is provided, all the pages in the database will be returned with pagination.

Sorts are similar to the sorts provided in the Notion UI. Sorts operate on database properties and can be combined. The order of the sorts in the request matter, with earlier sorts taking precedence over later ones.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :database_id (id)

    Database to query.

  • :filter (Object)

    When supplied, limits which pages are returned based on the provided criteria.

  • :sorts ([Object])

    When supplied, sorts the results based on the provided criteria.

  • :start_cursor (UUID)

    Paginate through collections of data by setting the cursor parameter to a start_cursor attribute returned by a previous request’s next_cursor. Default value fetches the first “page” of the collection. See pagination for more detail.

  • :page_size (integer)

    The number of items from the full list desired in the response. Maximum: 100



36
37
38
39
40
41
42
43
44
45
# File 'lib/notion/api/endpoints/databases.rb', line 36

def database_query(options = {})
  throw ArgumentError.new('Required arguments :database_id missing') if options[:database_id].nil?
  if block_given?
    Pagination::Cursor.new(self, :database_query, options).each do |page|
      yield page
    end
  else
    post("databases/#{options[:database_id]}/query", options.except(:database_id))
  end
end

#update_database(options = {}) ⇒ Object

Updates an existing database as specified by the parameters.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :database_id (id)

    Database to update.

  • :title (Object)

    Title of database as it appears in Notion. An array of rich text objects. If omitted, the database title will remain unchanged.

  • :properties (Object)

    Updates to the property schema of a database. If updating an existing property, the keys are the names or IDs of the properties as they appear in Notion and the values are property schema objects. If adding a new property, the key is the name of the database property and the value is a property schema object.



86
87
88
89
# File 'lib/notion/api/endpoints/databases.rb', line 86

def update_database(options = {})
  throw ArgumentError.new('Required arguments :database_id missing') if options.dig(:database_id).nil?
  patch("databases/#{options[:database_id]}", options.except(:database_id))
end