class Google::Cloud::Firestore::Service

@private Represents the gRPC Firestore service, including all the API methods.

Attributes

credentials[RW]
host[RW]
project[RW]
timeout[RW]

Public Class Methods

new(project, credentials, host: nil, timeout: nil) click to toggle source

Creates a new Service instance.

# File lib/google/cloud/firestore/service.rb, line 36
def initialize project, credentials, host: nil, timeout: nil
  @project = project
  @credentials = credentials
  @host = host
  @timeout = timeout
end

Public Instance Methods

begin_transaction(transaction_opt) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 131
def begin_transaction transaction_opt
  firestore.begin_transaction(
    {
      database: database_path,
      options:  transaction_opt
    },
    call_options(parent: database_path)
  )
end
commit(writes, transaction: nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 141
def commit writes, transaction: nil
  commit_req = {
    database: database_path,
    writes:   writes
  }
  commit_req[:transaction] = transaction if transaction

  firestore.commit commit_req, call_options(parent: database_path)
end
database_path(project_id: project, database_id: "(default)") click to toggle source
# File lib/google/cloud/firestore/service.rb, line 161
def database_path project_id: project, database_id: "(default)"
  # Originally used V1::FirestoreClient.database_root_path until it was removed in #5405.
  "projects/#{project_id}/databases/#{database_id}"
end
documents_path(project_id: project, database_id: "(default)") click to toggle source
# File lib/google/cloud/firestore/service.rb, line 166
def documents_path project_id: project, database_id: "(default)"
  # Originally used V1::FirestoreClient.document_root_path until it was removed in #5405.
  "projects/#{project_id}/databases/#{database_id}/documents"
end
firestore() click to toggle source
# File lib/google/cloud/firestore/service.rb, line 43
def firestore
  @firestore ||= \
    V1::Firestore::Client.new do |config|
      config.credentials = credentials if credentials
      config.timeout = timeout if timeout
      config.endpoint = host if host
      config.lib_name = "gccl"
      config.lib_version = Google::Cloud::Firestore::VERSION
      config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}/databases/(default)" }
    end
end
get_documents(document_paths, mask: nil, transaction: nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 55
def get_documents document_paths, mask: nil, transaction: nil
  batch_get_req = {
    database:  database_path,
    documents: document_paths,
    mask:      document_mask(mask)
  }
  if transaction.is_a? String
    batch_get_req[:transaction] = transaction
  elsif transaction
    batch_get_req[:new_transaction] = transaction
  end

  firestore.batch_get_documents batch_get_req, call_options(parent: database_path)
end
inspect() click to toggle source
# File lib/google/cloud/firestore/service.rb, line 171
def inspect
  "#{self.class}(#{@project})"
end
list_collections(parent, token: nil, max: nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 88
def list_collections parent, token: nil, max: nil
  firestore.list_collection_ids(
    {
      parent:     parent,
      page_size:  max,
      page_token: token
    },
    call_options(parent: database_path)
  )
end
list_documents(parent, collection_id, token: nil, max: nil) click to toggle source

Returns a list of DocumentReferences that are directly nested under the given collection. Fetches all documents from the server, but provides an empty field mask to avoid unnecessary data transfer. Sets the showMissing flag to true to support full document traversal. If there are too many documents, recommendation will be not to call this method.

# File lib/google/cloud/firestore/service.rb, line 77
def list_documents parent, collection_id, token: nil, max: nil
  mask = { field_paths: [] }
  paged_enum = firestore.list_documents parent:        parent,
                                        collection_id: collection_id,
                                        page_size:     max,
                                        page_token:    token,
                                        mask:          mask,
                                        show_missing:  true
  paged_enum.response
end
listen(enum) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 127
def listen enum
  firestore.listen enum, call_options(parent: database_path)
end
partition_query(parent, query_grpc, partition_count, token: nil, max: nil) click to toggle source

Returns Google::Cloud::Firestore::V1::PartitionQueryResponse

# File lib/google/cloud/firestore/service.rb, line 101
def partition_query parent, query_grpc, partition_count, token: nil, max: nil
  request = Google::Cloud::Firestore::V1::PartitionQueryRequest.new(
    parent: parent,
    structured_query: query_grpc,
    partition_count: partition_count,
    page_token: token,
    page_size: max
  )
  paged_enum = firestore.partition_query request
  paged_enum.response
end
rollback(transaction) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 151
def rollback transaction
  firestore.rollback(
    {
      database:    database_path,
      transaction: transaction
    },
    call_options(parent: database_path)
  )
end
run_query(path, query_grpc, transaction: nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 113
def run_query path, query_grpc, transaction: nil
  run_query_req = {
    parent:           path,
    structured_query: query_grpc
  }
  if transaction.is_a? String
    run_query_req[:transaction] = transaction
  elsif transaction
    run_query_req[:new_transaction] = transaction
  end

  firestore.run_query run_query_req, call_options(parent: database_path)
end

Protected Instance Methods

call_options(parent: nil, token: nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 182
def call_options parent: nil, token: nil
  Gapic::CallOptions.new(**{
    metadata:   default_headers(parent),
    page_token: token
  }.delete_if { |_, v| v.nil? })
end
default_headers(parent = nil) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 177
def default_headers parent = nil
  parent ||= database_path
  { "google-cloud-resource-prefix" => parent }
end
document_mask(mask) click to toggle source
# File lib/google/cloud/firestore/service.rb, line 189
def document_mask mask
  return nil if mask.nil?

  mask = Array(mask).map(&:to_s).reject(&:nil?).reject(&:empty?)
  return nil if mask.empty?

  Google::Cloud::Firestore::V1::DocumentMask.new field_paths: mask
end