class Kagaribi::Collection
#=> { name: “sue445”, url: “github.com/sue445” }
collection.get(“sue445”)
#=> document is stored in a key named “users/sue445” to Firestore collection
collection.set(“sue445”, name: “sue445”, url: “github.com/sue445”)
collection = UsersCollection.new
end
end
super(“users”)
def initialize
class UsersCollection < Kagaribi::Collection
# or
collection = Kagaribi.collection(“users”)
require “kagaribi”
@example
Manage a Firestore collection
def self.sanitize_key(key)
-
(String)
-
Parameters:
-
key
(String
) --
def self.sanitize_key(key) key.tr("/", "-") end
def delete(doc_key)
-
doc_key
(String
) --
def delete(doc_key) with_retry("Kagaribi::Collection#delete") do ref = firestore.doc(full_doc_key(doc_key)) ref.delete end end
def exists?(doc_key)
-
(Boolean)
-
Parameters:
-
doc_key
(String
) --
def exists?(doc_key) with_retry("Kagaribi::Collection#exists?") do ref = firestore.doc(full_doc_key(doc_key)) snap = ref.get snap&.exists? end end
def firestore
-
(Google::Cloud::Firestore)
-
def firestore @firestore ||= Google::Cloud::Firestore.new(database_id: database_id) end
def full_doc_key(doc_key)
-
(String)
-
Parameters:
-
doc_key
(String
) --
def full_doc_key(doc_key) "#{sanitized_collection_name}/#{sanitize_key(doc_key)}" end
def get(doc_key)
-
(Hash
- return empty Hash if document isn't found)
Parameters:
-
doc_key
(String
) --
def get(doc_key) with_retry("Kagaribi::Collection#get") do ref = firestore.doc(full_doc_key(doc_key)) snap = ref.get snap&.data || {} end end
def initialize(collection_name, database_id: nil, logger: nil)
-
logger
(Logger, nil
) -- default is `STDOUT` Logger -
database_id
(String, nil
) -- Identifier for a Firestore database. If not present, the default database of the project is used. -
collection_name
(String
) --
def initialize(collection_name, database_id: nil, logger: nil) @collection_name = collection_name @database_id = database_id @logger = if logger logger else Logger.new($stdout) end end
def retryable_error?(error)
-
(Boolean)
-
Parameters:
-
error
(StandardError
) --
def retryable_error?(error) case error when RuntimeError # e.g. # Could not load the default credentials. Browse to # https://developers.google.com/accounts/docs/application-default-credentials # for more information return true if error.message.include?("Could not load the default credentials.") # e.g. # Your credentials were not found. To set up Application Default # Credentials for your environment, see # https://cloud.google.com/docs/authentication/external/set-up-adc return true if error.message.include?("Your credentials were not found.") return false end true end
def sanitize_key(key)
-
(String)
-
Parameters:
-
key
(String
) --
def sanitize_key(key) Collection.sanitize_key(key) end
def sanitized_collection_name
-
(String)
-
def sanitized_collection_name sanitize_key(@collection_name) end
def set(doc_key, data)
-
data
(Hash
) -- -
doc_key
(String
) --
Other tags:
- Note: - If a document with the same key exists, it is overwritten.
def set(doc_key, data) with_retry("Kagaribi::Collection#set") do ref = firestore.doc(full_doc_key(doc_key)) ref.set(data) end end
def update(doc_key, data)
-
data
(Hash
) -- -
doc_key
(String
) --
Other tags:
- Note: - If a document with the same key exists, it is merged.
def update(doc_key, data) with_retry("Kagaribi::Collection#update") do ref = firestore.doc(full_doc_key(doc_key)) ref.update(data) end end
def with_retry(label)
- Yield: -
Parameters:
-
label
(String
) --
def with_retry(label) yield rescue TypeError, GRPC::Unavailable, RuntimeError, Signet::AuthorizationError, Google::Cloud::UnauthenticatedError => error raise error unless retryable_error?(error) retry_count ||= 0 retry_count += 1 raise error if retry_count > MAX_RETRY_COUNT logger.warn "[#{label}] collection_name=#{@collection_name}, retry_count=#{retry_count}, error=#{error}" sleep 1 retry end