Firebase - Admin.firestore vs functions.firestore - firebase

I'm newbie on cloud function. I have some confusions.
Is there any difference between admin.firestore and
functions.firestore?
Is admin.database for real-time database?
So if cloud functions are basically written as JavaScript or
TypeScript in Node.js environment, then I can write them with
JavaScript. But documentation made me confused about that. Because
whenever I read the documentation, it gives things different. For
example,
Above code to get a document, it uses document('some/doc').
But, above code it uses doc('doc') to achieve the same functionality. If all of them come form firestore, why do both differ each other?
Can someone help me to understand these questions? Thank you for all your supports.

functions.firestore: is used to set up a trigger. You can set up a listener on a document path so that whenever an event happens on that document (create/update/delete etc.), this function will be executed. See how to create one here.
admin.firestore: is used to create a reference to a firestore database. You can perform various operations on firestore collection/documents using admin sdk through that reference. See documentation.

Related

How to use Firebase built-in features instead of ORM?

I started using Firebase for Android app with Kotlin. So the question is how to use Firebase built-in features instead of adding ORM? I would like to work with classes and objects, instead of dealing with get, set, onSuccessListener and other firebase methods.. So I can create some semi-ORM by myself, by dividing everything to adapter and models. However is there simpler solution? I assume there should be some built in features in Firebase?
E. g. we can query documents with Firebase functions and map them to classes as per official manual:
val docRef = db.collection("cities").document("BJ")
docRef.get().addOnSuccessListener { documentSnapshot ->
val city = documentSnapshot.toObject<City>()
}
To make controller loose coupled from the database, we can create classes AbstractModel and CityModel and Queries and FirebaseAdapter so finally we can do something like
val city = Cities("BJ")
instead of lengthy code above.
Eventually I see that problem is we need to create our own ORM for using Firebase for every project. Or is there simpler solution? Do some existing ORMs work with Firebase? I did some research, however Stackoverflow policy prohibits to post trademarks into the question.

Optimising network connections of firebase cloud function

Firebase documentation recommends including code snippet given at (https://firebase.google.com/docs/functions/networking#https_requests) to optimize the networking, but few details are missing. Like
How exactly does this help?
Are we supposed to call the function defined as per of recommendation
or include this snippet deploy?
Any documentation around this would be of great help.
this is an example showing you how you would make this request, the key part in this example is the agent field which by nature isn't normally managed within your app. By injecting a reference to it manually, you are able to micro-manage it and it's events directly.
As for the second question, it really depends on your cloud function needs - some users set it in a global object that they manage with all cloud functions but it's on a by-use case basis. but ultimately isn't required.
You can read more about HTTP.Agent's and their usage below:
https://nodejs.org/api/http.html
https://www.tabnine.com/code/javascript/functions/http/Agent
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent

Flutter: How to count children in Firebase?

We have a data model designed in the Firebase for realtime questions and answers. However, we are unable to get realtime counts for a quiz.
On researching we are able to see a method addValueEventListener() which refers the DataSnaphot object with the getChildrenCount()
One of the reference links for the above info is: android firebase - get childrens count
However, we are unable to find these methods in the Flutter package for Firebase. The link to the package that we are using: https://pub.dartlang.org/packages/firebase_database
Please advise.Thanks.
It indeed looks like the child count method is not implement in the FlutterFire library version of DataSnapshot. I'd recommend filing a feature request for it here.
The fastest way to get the feature added would probably to create a PR for it yourself, but that would require you to dig into how the FlutterFire library works under the hood.
Alternatively you can listen to the onChildAdded event, and count the children in your client-side Flutter code. It won't be significantly less efficient, since the regular SDKs also need to download all child nodes to be able to return their count.

Does Datastore support bulk updates?

I've combed through GC DataStore documentation, but I haven't found anything useful around bulk updating documents. Does anyone have best practice around bulk updates in DataStore? Is that even a valid use case for DataStore? (I'm thinking something along the lines of MongoDB Bulk.find.update or db.Collection.bulkWrite)
You can use the projects.commit API call to write multiple changes at once. Most client libraries have a way to do this, for example in python use put_multi.
In C# the DatastoreDb.Insert method has an overload for adding multiple entities at once.
If this is a one-off, consider using gcloud's import function.

Cloud Functions suitability for Custom Tags

I wonder if Google Cloud Functions are a good solution to the following:
For our apps we like to use Google Tag Manager, just like we do for our sites.
Thing is : on the website GTM, we can implement custom tags easily by using javascript.
In an app that's not possible , it would be possible to call a function that's already compiled inside the app.
But that gives a lot of problems with flexibility, for instance: if an app is already used by many users, we need to deploy a new version if we want to implement a new custom tag to a third party.
I was thinking of Google Cloud Functions as an answer, where by using Javascript, we could implement new tags (in the form of api calls) without redeploying.
Question is : how can you call such function without redeploying? And is this a suitable way of doing this.
Unfortunately, this is not possible without planning it ahead of time and it would likely result in a high LOE instead of just adding the tag and releasing a new version of the app.
You would need to setup your app to be able to detect new tags returned by the cloud function since you're trying to add tags without releasing a new version of the app.
You would then need to come up with a way of tying that tag to a particular view or location within the app. In addition, you would then need to have the right code in the right places to make sure that tag is used properly.
Cloud Storage and JavaScript instances might offer a solution:
I'm going to use iOS as an example, you could potentially use JavaScriptCore.framework to implement this dynamic capability within the app. You could store new JavaScript functions in cloud storage. Then you could use a cloud function to fetch these from Cloud Storage, or you can access Cloud Storage directly from an SDK or URL; you do not even need a cloud function to perform this task.
You would then evaluate these scripts within the app:
JSContext *context = [JSContext new];
NSString *jsFunctionText =
#"var isValidNumber = function(phone) {"
" var myTag = 'new custom tag';"
" /* use JS Tag Manager SDK to deliver tag information."
"}";
[context evaluateScript:jsFunctionText];
This only solves part of your problem though, since you need to be able to add context to where the function should be pulled from and called. So again, you need to anticipate your future needs and could potentially over engineer the solution.
I know it's not a solid answer, but it is not a simple problem to solve.

Resources