How do I set Firebase Cloud Function fieldMask? - firebase

My Firebase Cloud Function for my Realtime Database (NOT CloudStore) listens onWrite and provides a change object with before and after.
This documentation here states:
If fieldMask is set, then only fields that changed are present in before.
How do I set this fieldMask? And when I set this fieldMask, will the resulting before object have the JSON structure of only the changed fields?

I don't think that class you linked "ChangeJson" is supposed to be part of the public documentation. When using an onWrite trigger, you actually get a Change object, which is different. Pay attention to that instead, not ChangeJson.
Feel free to use the "Send feedback" link at the top right of any page of Firebase documentation indicate what your confusion was on that page.

Related

Is there an equivalent of a for each function on a map in Firestore Security Rules?

Users of our app can add other users as friends. Each friends list is implemented as a map where the key is the id of the users and where the value is some data related to the user. The following map is an example of how it looks like :
{'id_1' : {displayName: 'John Doe', color: 3412445}, 'id_2' : {displayName: 'Bob Alison', color: 84655467}}
We want to add rules to make data validation on the fields in the values of the map. The displayName needs to be a string and the color needs to be a number.
In the firestore rules we can check the new added friends by doing like so :
let addedFriendsKeys = newFriendsList.diff(oldFriendsList).addedKeys()
But is there a way to retrieve the values related to that list of keys?
something like :
let newFriendsList.getAll(addedFriendsKeys).forEach((p0)=>isDataValid(p0))
There are no looping operations in Firestore security rules. You will have to enumerate the keys that you want to validate.
The following answer is not mine (Credits to
Frank van Puffelen). It's coming from this post but answers perfectly to the question :
If you're asking about doing this in server-side security rules, then you've precisely hit the nail on the head: there is no ability to loop in Firebase's server-side security rules. See the reference documentation for the operations that can be performed on a List in a document. This limits what can be accomplished in security rules, and as far as I can see none of the use-cases you mention can be implemented with just security rules.
The simplest approach I can think of is by using Cloud Functions to implement the logic. You could either have the Cloud Function inspect the documents in place in the current collection, or you can have the client write to a different collection (of "pending" documents), have the Cloud Function validate the document, and move it to the actual collection.

How add the data fields that are manually added in cloud firestore as a constructor and how to call that in flutter?

i have manually added the data in cloud firestore
this how the data looks like .
Now using those fields i.e(id, location, model, equipmentCoordinates) how to create the constructor for that in flutter like this and call it. so that i can use the constructor and call it everwhere i need. It will be so helpful if you can answer this.
You can do it manually and also use this site for lengthy data models https://app.quicktype.io/
please check the image below for reference you can choose language and give data as you saved in the database,

Firebase Translate Text Extension - Is it possible to translate fields in nested collections?

I am using Firebase Translate Text Extension to translate few documents fields in my project. I want to add one more field that is in a documents in the nested collection:
So each document in collection "spots_test" has collection "reviews". I want to translate one field in each new review added, and I am wondering how can I set up it in Firebase Translate Text Extension, I was trying to set up something like this, but it didn't work:
Is there any way to handle nested collections?
I wasn't able to find proper documentation, however I experimented a bit. It seems to be working this way on my side (LevelOne is collection, test is sub-collection in any document of the collection):
LevelOne/{doc}/test
I don't think that it's important what is in the brackets I tested {something} as well. Working fine.
As this is Firebase Function base feature, I tried the same wildcards logic as in Firebase Function background triggers for Firestore. To be honest, as I didn't found any documentation in extension docs so I am not sure if this is intended behavior, but it works.
UPDATE:
I have continued the test. The extension is generating function visible in Functions tab of Firebase console. The trigger is visible there. The value of the trigger is gendered from extension configuration "Collection path"+{messageId}. So for example you can setup:
{collection}/{doc}/{subcollection}
In this situation translate text extension will work on every document in 2nd level collection no matter what the path is.

Firebase: I want to update the metadata of files in storage after an administrator check

I'm developing an image posting app that lets users post image files to firebase's cloud storage.
Map <String, String> type information can be written to the metadata of the image file.
So when the user posts an image, as metadata
{'displayability':'false'}
Is stored.
And after the administrator checks the posted image, the metadata of the image
{'displayability':'true'}
Update to.
And in the image list list display, I checked the metadata,
"{'Displayability':'true'}" as metadata
I want to show the user only the files that hold.
However, I'm not sure what to do specifically.
When I read documents etc., it means server side processing,
so I wonder if I will use cloud functions, but
If you have any hints, please teach me.
As far as I know, there is no efficient way to "query" the images based on their metadata except for downloading them all then process it client side, which is not advisable in any case. Instead, I suggest you use a Firestore collection to keep track of which image has been approved by the admin. Each document should be something like this:
{
"ref": "path/to/image",
"displayability": true //or false, depends on whether the image has been approved
//You can add any additional info here, like upload time, uploader's name, etc...
}
Then you can make a simple query to Firestore to get a list of approved images, then download them from Cloud Storage. And when you need to get list of unapproved images, the process is basically the same. This solution doesn't require any Cloud Functions code at all.
There is a method called "updataMetadata(SettableMetadata metadata)".
just pass the updated metadata using above function.

Removing firebase-generated id of user saved in database

When I save a user in my firebase db I do it via this path:
/allUsers/serviceUsers/${usersUID}
However, firebase adds another apparent UID to this UID.
Is there anyway to prevent this or tell firebase to not do it?
The first ID after serviceUsers is the ID I care about. The second one is generated by firebase and is making working with these objects in the app more painful. I would like that the user object be directly nested under the ID after serviceUsers. How can I achieve this?
Try to call :
firebase.database().ref("/allUsers/serviceUsers/${uid}").setValue(user)
Instead of :
firebase.database().ref("/allUsers/serviceUsers/${uid}").pus‌​h(user)
The push function generate an automatic id.
Why not just use a regular POST to
/allUsers/serviceUsers/
and return the ID? The path parameter should only be used on an update, read, or delete, if you are adhering to REST.
https://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_Web_services

Resources