This time, I will summarize the code that issues a query when retrieving data from Firestore in Ruby and the code that adds data.
A NoSQL cloud database provided by Firebase.
For the time being, I think it's okay if you know the following three terms in Firestore.
A general frame for storing data (documents). If you click on Cloud Firestore in the sidebar of the Firebase console, the collection will be on the far left of the screen. Data (documents) related to it are stored in one collection.
Data stored in the collection. The data is managed by a unique document ID, and there is a field associated with that document ID.
Data associated with one document. It is possible to link multiple documents to one document.
The above nested structure is the Firestore data model. So, "getting the values from the collection to the document and in the fields" is the act of getting the Firestore data. I get the impression that you have to take quite a few steps.
The methods described below need to be able to use Firestore methods.
Instead of using ActiveRecord, which is an ORM of mysql, it works with a gem for accessing data in the fiestore.
There is a gem that allows you to use the firestore
gem 'google-cloud-firestore'
Is used.
firebase.rb
class Firesbase
require 'google/cloud'
class_attribute :connecting
self.connecting = Google::Cloud.new(Project name created in the console).firestore
end
First, determine from which collection you want to get the data. If you use the collection method and specify the collection name created on the Firebase console as an argument, you can use the method to get data for that collection.
collection.rb
hogehfuges_colleciton = connecting.collection 'hogefugas'
It seems that the following three are used as the main methods for acquisition.
document
get_by_document.rb
hoge = hoge_collection.document(Document ID).get #Google::Cloud::Firestore::DocumentSnapshot
fuge = fuge_collection.document(Document ID).get.data # Hash
By taking the document ID as an argument, document gets the data associated with the document ID. If you use the get method, you can get it with an object called Google :: Cloud :: Firestore :: DocumentSnapshot, but since you can not handle the data of the field inside as it is, use the data method. If you use the data method, it will make a hash with the field associated with the acquired document as a key, and you can easily access the data by the hash operation as shown below.
data.rb
fuge[:fugefuge_name]
where where gets the one that meets the condition. In the case of where, if you use the get method, the object type will be Enumerator, so you need to use loop processing or a method that includes loop processing in the Ruby standard library (for example, map). You can use the data method on the data retrieved in a loop so that you can access the data.
get_by_where.rb
snap = hogefugas_collection.where(conditions).get #Enumerator
snap.map do |hoge|
{
"hoge_name" => hoge.data[:hoge_name],
"update_at" => hoge.data[:update_at]
}
end
order order specifies a field as an argument and gets the document in which the field exists. If order also goes through the get method, the object type will be Enumrator, so loop it before accessing the data. The code below uses the document_id method instead of the data method. This allows you to get the document ID of the document, not the field.
get_by_oder.rb
hoges = hogefugas_collection.order('updatedAt').get.map { |hoge| hoge.document_id }
Use the set method. Create data by using the field you want to save as the hash key. If you use it as an argument of the set method, the document ID will be automatically generated in the specified collection and saved in the Firestore.
set.rb
def save(id, hoge)
huge = {
id: id,
hogehoge: hoge,
update_at: DateTime.now.strftime("%Y-%m-%d-%H:%M")
}
collection.doc.set(huge)
end
Use the update method. You can update the data by specifying the document ID in the collection you want to update and using that field as an argument of the update method.
update.rb
def edit(id, hoge)
document_id = collection.where("id", "=", id).get.first.document_id
collection.document(document_id).update(
hogehoge: hoge,
update_at: DateTime.now.strftime("%Y-%m-%d-%H:%M")
)
end
Recommended Posts