Query with multiple references. Firestore - Flutter - dart

I'm new in NoSQL so I don't know if my question is possible, there is my problem. I have a List of DocumentReference (or ID) and I would like to make a query on firestore in order to get all the documents in one request.
I manage to do it with a forloop but it's reading a lot my firestore... And I don't know if take all the documents of the collection and filter the List is good practice.
I use flutter and the cloud_firestore(0.8.2) plugin.

Here you can use this code to get all the document in Firestore.
Firestore _myStore = Firestore.instance;
_myStore.collection('users').document('${user.uid}').setData({
"Email": _emailAddress.text.toLowerCase(),
"Name": _userName.text,
'Role': 'user',
'Created': FieldValue.serverTimestamp(),
});

Related

Search Outlook Calendar Event categories for multiple hits - Microsoft Graph

I am trying to keep track of Outlook calendar events without the need to store information about them on my own systems. I decided to do this by adding the required ids as categories with their type of id before it as shown in the code sample below.
{
"#odata.etag": "",
"createdDateTime": "",
"categories": [
"ID1::abc123",
"ID2::def456"
]
}
I tried using the 'any' lambda operator and this works fine if I want to filter based on one category using the query below:
https://graph.microsoft.com/v1.0/me/events?$filter=categories/any(x:x%20eq%20'ID1::abc123')
What I need is a query that will check if an event has both ids so in this case only the events where ID1=abc123 and ID2=def456. I figured https://graph.microsoft.com/v1.0/me/events?$filter=categories/any(x:x%20eq%20'ID1::abc123')%20AND%20categories/any(x:x%20eq%20'ID2::def456') should do the trick but this keeps returning empty arrays.
Thanks in advance!
Since categories are available to the user (and this is going to look really strange in outlook), I would suggest you to use the transactionId on the events to store the external id. This will automatically deny your new event if you try to create a duplicate.
I know this isn’t the answer you were looking for, but using this solution will be much more feature proof.

How to manage/store static and dynamic data for an IOS App

My question might sound basic but it is more logic related about how to manage and store data in an IOS app.
I am not sure if I have to store my data in json or database or firebase as some of my data is static and some dynamic.
Imagine I am making a cooking app. I have JSON files of all ingredients, and cooking equipments (these are statics). This information will never change so they are in JSON. But I want users to use the library of ingredients and equipments to create recipes and I want to know what each user did with this information.
example:
{
"User": 124213,
"recipesList": [
"recipe1",
"recipe2"
],
"date": "SomeDate"
}
Also each recipe would have a similar dependencies for example directions, ingredients, tools, and it will be referred to my static JSON files, such as ingredients: [3, 1, 5, 3, ...]
So my question is how I should set up my dataset. Do I need to have everything in a database such as firebase? But in that case why would user download static ingredients list each time when it never changes? Some part of the data is basic and never changes and some data which is about the user interactions and should get updated regularly.
Hope this is clear enough and this is more about the logic in managing and storing data.
Also what database is common to use for IOS apps?
The best way is to map the JSON string into an object and then use the object to manipulate data, and in the moment that you have to send to the server the recipe or some change you can trasforma back the object into a JSON. If you use an object, the software will be simpler and cleaner.

How to build structured realtime firebase database for iOS, swift?

I am currently setting up my realtime firebase database to my iOS application.
It is my first time trying to structure user data in a firebase database, and I am really, really struggling with understanding a few key things.
A bit of context to my application's database needs:
When a new user is created, the attributes assigned directly to the user are:
Age
Email
Username
Nationality
Later on, the user needs the option of creating personal diaries!
Each of these diaries being arrays/lists of objects... Where each object in a diary furthermore holds a few attributes in a list/array.
After reading everything I could find anywhere, I picture my database something like this:
I am terribly sorry if it becomes too specific - I will try to make the question as open as possible:
My question is:
How to create the different "children" programmatically and how to find the keys leading back to them, so I can refer to them at other times again? (when editing an attribute in a child).
A few methods I have tried:
setValue([ArrayOfObjects]) --> This creates the desired array, but I can't seem to find e.g. index 3 in this array, to allow user to change his/her email later on.
childByAutoID() --> This as well creates my array, but gives several other problems: User can only store one diary, still can't find the paths to specific indexes...
setValue(), andPriority() --> Can't seem to make the priority function. (Is this function also outdated??)
And a few more...
If anyone can tell me how to achieve just the first few steps in setting up my database structure, I will be forever grateful - I have spent literally all day on it and I am not moving forward ...
Or, at least tell me, if I am on the right track regarding my desired setup of the database. Is it flat enough? Is there a smarter way to store all these user created lists?
Thank you so much! :-)
I don't know Swift so my examples are in Dart but the methods are similar I believe.
First off, I would split the Users node into two. One to hold the user data, which is normally pretty static, and the other to hold the diaries. You would use the same uid key as reference to both. This way you have less nesting to worry about and therefore it is much easier to CRUD the data. If you are using Firebase to authenticate your users then I would use the unique key that Firebase creates for each user as the keys for these two nodes.
Then...
To create a user data node record the Dart code would be something like:
referenceUserData.child(<authenticated user id>).set({
"age": <age value>,
"email": <email value>,
"name": <name value>,
});
To create a user diary node object record the Dart code would be something like:
referenceUserData.child(<authenticated user id>).child(<diary key>).child(<diary object key>).set({
"object info value 1": <object value>,
"object info value 2": <object value>,
"object info value 3": <object value>,
});
You could also create all the object records at once by writing them as a List (array) using .set().
You also need to decide what your diary key should be. You could use Firebase to generate a unique key by using .push().set().
To read eg. the user data then your call could be:
referenceUserData
.child(<authenticated user id>)
.once()
.then(
(DataSnapshot snapshot) {
print(snapshot.key);
if (snapshot.value != null) {
print(snapshot.value);
<code to process your snapshot value>
}
};
BTW, 'priority' is legacy from the early days of Firebase RTDB so I wouldn't try to use it.

Firebase query to fetch specific users by their user IDs in iOS swift

I have a firebase database structure like this
"Users": {
"ghjghjFc1S3KO0y8yJwORdfgret": { //user ID
"name": "John"
}
"3uqWZJRFc1S3KO0y8yJwORTMtWC2": { //user ID
"name": "Smith"
}
"4SrHGdRFc1S8KO0y8yJwORTMtQAR": { //user ID
"name": "Alan"
}
"5fvxhaxFc1S8Kf0ygyJwORyMtFdy": { //user ID
"name": "Ben"
}
}
I have an array: usersToRetreive = ["5fvxhaxFc1S8Kf0ygyJwORyMtFdy", "3uqWZJRFc1S3KO0y8yJwORTMtWC2"]
and I want to retrieve only those users whose userID is given in the array 'usersToRetreive'
Does firebase support this type of query? if yes then how can I make this query in swift?
Yes Firebase does support this type of query. You have to use the orderByChild() method on the database reference. This Firebase Youtube video goes through how to do this.
Common SQL Queries converted for the Firebase Database - The Firebase Database For SQL Developers #4
As I understand from your question you are trying to fetch specfic users all informations which are inside your Arraylist given by their ID number.
First of all this kind of data map structure is not competible with firebase. There is no specific easy way to do that. It would be very expensive if you fetch all data then search locally. You can dublicate the information of the users where it is written their id numbers if you use this way .. Becouse 1 million character will cost around 2MB .. The important thing in Firebase is How you use the TRAFFIC. NOT MEMORY .
You can alternatively use Firebase BIGDATA queries .
Just do a for loop over your array and fetch each user separately, don't see what the big problem is..

Firebase rules to access specific child's value

The childByAutoId would be useful if you want to save in a node multiple children of the same type, that way each child will have its own unique identifier.
List:{
KJHBJJHB:{
name:List-1,
owner:John Doe,
user_id:<Fire base generated User_id>
},
KhBHJBJjJ:{
name:List-2,
owner:Jane Lannister,
user_id:<Fire base generated User_id>
},
KhBHJZJjZ:{
name:List-3,
owner:John Doe,
user_id:<Fire base generated User_id>
}
}
I am trying to access the List with the help of the following code:
let ref = FIRDatabase.database().reference(withPath: "/List")
The current user logged into the app is John Doe. When the user accesses the list, I want all the List child whose owner is John Doe(i.e. List-1 & List-3) and ignore the other child values.
Do I have to do this in my application or can this be achieved via Firebase Security rules?
My current rule definition is:
"List":{
".read": "root.child('List/'+root.child('List').val()+'/user_id').val() === auth.uid" }
But this rule is not giving me any success. Any idea how to achieve the desired result?
You're trying to use security rules to filter the list. This is not possible and one of the common pitfalls for developers coming to Firebase from a SQL background. We commonly refer to it as "rules are not filters" and you can learn more about it in:
the Firebase documentation
this answer
our new video series Firebase for SQL developers
and many previous questions mentioning "rules are not filters"
The solution is almost always the same: keep a separate list of the keys of posts that each user has access to.
UserLists:{
JohnUid: {
KJHBJJHB: true,
KhBHJZJjZ: true
},
JaneUid: {
KhBHJBJjJ: true
}
}
This type of list is often referred to as an index, since it contains references to the actual post. You can also find more about this structure in the Firebase documentation on structuring data.

Resources