i would like to know what is better:
The example is a shopping app and it is about the Main screen with a List of every Item available.
A) One document, for example "productList", with a Map in it where every Category, following another Map with every productId and a List with Data like the product Title, the showcase Image and the price.
Example code:
Stream stream = db.collection('products').document('productList').snapshots().asyncMap((list) => list.data);
or
B) many documents in a collection called "products" where the data is stored.
Example code:
Stream stream = db.collection('products').where('category', isEqualTo: "CategoryName").snapshots().map((list) => list.documents.map((doc) => doc.data));
(Both are working but with B i got with 3 products muuuuch more document reads wherefore i thought A would save a lot product reads but opening the question if there could be problems with that method)
Thank you very much :)
If you go with the first approach of trying to put everything into a single document, you will have scalability problems when the number of products becomes large. A Firestore document can only contain 1MB of data. After that, writes to it will fail, and you will have a hard time restructuring your app and its data in order to resolve the issue.
The only "right" way to store lots of items is to use one document for each item. The exception to this is when you have a hard guarantee about the number of items, and you are absolutely certain that the data for all those items can always fit into one document.
Related
I am using Node js with Express.js and I am getting data from firebase's real-time database
so How can I get data after 10 children or nodes by skipping?
example
I have a lot of data like -
Images
imageId
- {some properties ie: imgUrl:"somevalue.jpg"}
and more
So first I am taking first 10 images using limitFirst(10) function.
and if the user clicks on link or next button I want to load the next first 10 images if available
does firebase real-time database have some functionality like this
//limitToAfter(afterChild, limit);
const imageRef = query("Images", limitToAfter(10,10));
onValue(imageRef,(snapshot)=>{
//some operation here...
});
Or can anyone give me some ways I can solve this problem?
I will always appreciate any help :)
Firebase Realtime Database doesn't have an offset mechanism. Instead to get the next page of data, you need to know the last node on the current page.
For example, say that the key of the last item on page 1 is "key10", then you can get the next 10 items with:
query("Images", orderByKey(), startAfter("key10"), limitToFirst(10));
In words: take all nodes under Images, order them by their key, then start after "key10" and return the first 10 nodes.
I have many to many relational data model. For example , i have a chat room and it has many users.
{Chatroom:
ChatRoomID:ID,
Somedata...
User:{
ID:userid,
Somedata...
}
}
I have a user object and i need to filtre those by user.
DataStore.query(ChatRoom,e=>e.User.Id("eq",myID))
I tried to query the data which has my userid then get the ChatRoomID. But i didnt work as expected. I can filter it with its main keys but i cant go through inside the object to filter. Currently i am doing this the same as the aws document. Which is-->
DataStore.query(ChatRoom).filter(e=> e.user.id==myID)
My concern is ,is this query above gets the whole data to filter ? For the first one, it looks like it getting the filtered data like select queries as we do in SQL. But i m worried about using second one. What if i have 100.000 rooms and 500.000 users inside it ? Is it get the whole data before filtering with a JavaScript functions like filter, map etc. ? I dont get the logic behind.
I'm having trouble trying to figure out how to get a list to query as desired. Full disclosure, it may not be possible and if that's the case then at least it will provide me with some closure. I want to query a friend's list based on presence, where friends who are active are queried on top.
Here is my database structure:
friendships
$UID
$UID: true
users
$UID
active:true/false
I need the list to stay synced so I need to use .on('value'). I've tried a couple different methods using child_added as well as iterating through a snapshot using snapshot.forEach() and pushing the childSnapshot key:value pairs into an array so I could then use lodash to order the list via the active key on the client side, but ran into the issue of it pushing a "new" item into the array for each active value change so there would be multiple items for any given user.
Any help or insight would be appreciated, spent the majority of yesterday attempting to figure this out before resorting back to an unorganized list using the following code:
const {currentUser} = firebase.auth();
firebase.database().ref(`/friendships/${currentUser.uid}`).on('value, snapshot => { //redux dispatch action, payload: snapshot.val() })
There's no great solution for this. Firebase doesn't have any sort of join or join query, so you could do usersRef.orderByChild('active').equalTo(true), but that will just return you all active users. You'll need to do separate queries to pull in their friendships.
I'm building my first chrome extension and I want it to track the TV series I watch and I'm currently trying to get it to save metadata on the series that I am following.
I have a content script that returns the title, the newest episode (and the URL of this episode) as well as the URL of the cover image of the series. I am currently trying to save it with some code on my background script (I have made sure to include "storage" under the permissions section of the manifest file).
So far my script looks like this (This was developed with help from Trying to save and fetch a Javascript object using chrome.storage API?):
var bkg = chrome.extension.getBackgroundPage();
response.aID = new Series(response.aTitle,response.aNewEp,response.aNewEpURL,response.aImage);
chrome.storage.sync.set(response.aID, function(){
chrome.storage.sync.get(function(val){
bkg.console.log("The saved title is: ", val.anTitle);
bkg.console.log("The saved newEp is: ", val.anNewEp);
bkg.console.log("The saved newEpURL is: ", val.anNewEpURL);
bkg.console.log("The saved imageURL is: ", val.anImage);
});
});
Problem is, the script only seems to store one response.aID at a time, so I can never store data for more than 1 TV series. Every time I try, the script seems to overwrite my previous entry. So I would like to ask whether there's any way to store more than 1 TV series at a time?
I have looked at storing an array and then pushing each new object into that array (Store an array with chrome.storage.local), but I don't quite understand the syntax involved so I'm not sure if this would work for me.
Unfortunately you didn't include the piece of code where you save your data, but i think you dont store your data with indices for the different TV series so the stored one gets overwritten everytime you store another one.
Anyway I would prefer storing your data in a JSON element (basically every javascript element can by converted to one but continue reading) because js provides several functions for this format which make it quite easy to use.
When opening your extension, load the data and call
var data = JSON.parse (yourloadedstring);
so the string (which should look like {"TVShows": [{"title": "How i met your mother", "url": ...}, {...}]} (look here for an explenation how JSON works) gets "translated" to an element from which you can read simply by calling
data.TVShows[0].title
or
data.TVShows[1].imageURL
You can edit this data JSON element when you add a new show for example by saying
data.TVShows[2].title = "The Big Bang Theory";
data.TVShows[2].URL= ...;
data.TVShows[2].imageURL= ...;
and save this element to chromes storage by calling
var dataToSave = JSON.stringify(data);
You have a string in your storage then, containing all information you need and you can simply parse it later like explained above :)
I hope everything is clearly to understand, if not pls ask me!
Cheers
In my application, I have an observableArray that loads data from DB. This observableArray fills first 25 items from DB and from scrolled down it loads another 25 and it goes on.
Now, I want to implement search, that should display the result searching the whole data from DB and not just from the displayed 25 items.
I tried to get the search result by sending the whole searching text to DB on clicking search button and there is lot of datas in DB which takes much time to load data.
Please let me know how I can get the desired result from DB within ms. Thanks in advance.
To get a well behaving search with Knockout, you should extend your searchText observable that is bound to the input with a rate-limiter
this.searchText = ko.observable('').extend({ rateLimit: { timeout: 500, method: "notifyWhenChangesStop" } })
This will call any subscribers after the input has remained unchanged for 500ms (i.e. when the user stops typing). You could also use the default method notifyAtFixedRate to call the API at least once every X seconds.
And to top it off, a fiddle!
Note: with this being said, if your query is taking 40 seconds, that sounds like a problem with your database query. It's possible that it's taking that long because you're flooding the server with requests, but that still seems awfully slow. This is the strategy we use and it works excellent, but our API response time is <200ms.