Class: Kagaribi::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/kagaribi/collection.rb

Overview

Manage a Firestore collection

Examples:

require "kagaribi"

collection = Kagaribi.collection("users")

# or

class UsersCollection < Kagaribi::Collection
  def initialize
    super("users")
  end
end
collection = UsersCollection.new

collection.set("sue445", name: "sue445", url: "https://github.com/sue445")
#=> document is stored in a key named "users/sue445" to Firestore collection

collection.get("sue445")
#=> { name: "sue445", url: "https://github.com/sue445" }

Constant Summary collapse

MAX_RETRY_COUNT =
5

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection_name, database_id: nil, logger: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • collection_name (String)
  • database_id (String, nil) (defaults to: nil)

    Identifier for a Firestore database. If not present, the default database of the project is used.

  • logger (Logger, nil) (defaults to: nil)

    default is STDOUT Logger



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kagaribi/collection.rb', line 43

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

Instance Attribute Details

#collection_nameString (readonly)

Returns:

  • (String)


30
31
32
# File 'lib/kagaribi/collection.rb', line 30

def collection_name
  @collection_name
end

#database_idString? (readonly)

Returns:

  • (String, nil)


34
35
36
# File 'lib/kagaribi/collection.rb', line 34

def database_id
  @database_id
end

#loggerLogger (readonly)

Returns:

  • (Logger)


38
39
40
# File 'lib/kagaribi/collection.rb', line 38

def logger
  @logger
end

Class Method Details

.sanitize_key(key) ⇒ String

Parameters:

  • key (String)

Returns:

  • (String)


104
105
106
# File 'lib/kagaribi/collection.rb', line 104

def self.sanitize_key(key)
  key.tr("/", "-")
end

Instance Method Details

#delete(doc_key) ⇒ Object

Delete document in collection

Parameters:

  • doc_key (String)


97
98
99
100
# File 'lib/kagaribi/collection.rb', line 97

def delete(doc_key)
  ref = firestore.doc(full_doc_key(doc_key))
  ref.delete
end

#exists?(doc_key) ⇒ Boolean

Whether document is exists in collection

Parameters:

  • doc_key (String)

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/kagaribi/collection.rb', line 87

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

#get(doc_key) ⇒ Hash<Symbol,Object>

Get document from collection

Parameters:

  • doc_key (String)

Returns:

  • (Hash<Symbol,Object>)

    return empty Hash if document isn’t found



76
77
78
79
80
81
82
# File 'lib/kagaribi/collection.rb', line 76

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

#set(doc_key, data) ⇒ Object

Note:

If a document with the same key exists, it is overwritten.

Save a document to collection

Parameters:

  • doc_key (String)
  • data (Hash)


59
60
61
62
# File 'lib/kagaribi/collection.rb', line 59

def set(doc_key, data)
  ref = firestore.doc(full_doc_key(doc_key))
  ref.set(data)
end

#update(doc_key, data) ⇒ Object

Note:

If a document with the same key exists, it is merged.

Update a document that has already been saved

Parameters:

  • doc_key (String)
  • data (Hash)


68
69
70
71
# File 'lib/kagaribi/collection.rb', line 68

def update(doc_key, data)
  ref = firestore.doc(full_doc_key(doc_key))
  ref.update(data)
end