How could I sort a svelte array-store? - store

I am doing a Todo app with store.
All is good now , but i want to sort the items in the store , or maybe to show them ordered.
The store is an array , and all item in the store is an object with the keys : text,id,editing,line,checkboxed.
So "checkboxed" is a boolean value , and i want that all the "checkboxed:true" objects would be first in the array.
how could i do it ?
Or maybe :
how could i sort items in svelte-store-array in general ?
thanks !!!!
link to the app

You can create a derived store, like:
const sorted = derived(todos_, todos => sortBy(todos, 'checkboxed'))
Derives a store from one or more other stores. Whenever those
dependencies change, the callback runs.
(Example)

another solution :
to add that to the TodoLIst component :
$:sortedTodos=$customTodos
.sort((a,b)=>b.checkboxed-a.checkboxed)
.sort((a,b)=>a.line-b.line)
and work with sortedTodos variable in the "each" loop.

Related

Overwriting some fields within a nested array of a mongoDB collection

I have an array of data that needs to be used to overwrite certain fields in a collection named "Players"
The structure of a Player looks like so:
The array of data that needs to go into documents in the Player collection is being passed into a controller function that looks like this:
exports.updateManyStats = (req, res) => {
console.log("updateManyStats API");
console.log(req.body.season);
console.log(req.body.stats);
Player.updateMany();
};
The console.logs looks like this:
What I need to do is overwrite the Players document fields goals , assists and pim for each person in the array (email is unique) where the season is whatever req.body.season is. In this case it is 2020-2021. The rest of the fields, like team, jersey, _id need to be left un-altered.
Question: What is the best way to do this? The only idea I have is to loop through each player and use $set, but that is not efficient. I feel like it might be possible with updateMany(), but not sure how to go about it that way.

How do I delete a string object element stored in Redis?

I am trying to delete an entire object stored in Redis using LREM, but I don't think I am doing it right, below is the code I tried:
GraphQL mutation:
deleteImage: async(_,args) => {
let data = await client.lrem("postedImagesList",0,JSON.stringify(args.id));
return data
}
postedImagesList is a redis collection of all the object elements stringified:
1) "{\"id\":\"aadc8456-a5c9-46b9-a58c-670446d95c70\",\"url\":\"chas\",\"description\":\"asd\",\"posterName\":\"asda\",\"binned\":false,\"userPosted\":true}"
2) "{\"id\":\"a24d6d1b-224f-400d-8b84-dac83d8eaf92\",\"url\":\"ihir\",\"description\":\"helped\",\"posterName\":\"bhi\",\"binned\":false,\"userPosted\":true}"
I am given the id as an argument and I have to delete the entire object, how can I do so? I am adding the elements using lpushasync.
Please see the LREM docs.
LREM key count element
The element argument has to have an exact match, so you can't match by ID.
You might want to consider using the RedisJSON using the JSON.DEL you should be easily achieve what you're looking for.

Updating firestore document nested data overwrites it

I'm trying to set some new fields in a nested dict within a Firestore document, which results in the data being overwritten.
Here's where I write the first part of the info I need:
upd = {
"idOffer": {
<offerId> : {
"ref" : <ref>,
"value" : <value>
}
}
}
<documentRef>.update(upd)
So output here is something like:
<documentid>:{idOffer:{<offerId>:{ref:<ref>, value:<value>}}}
Then I use this code to add some fields to the current <offerId> nested data:
approval = {
"isApproved" : <bool>,
"dateApproved" : <date>,
"fullApproval" : <bool>
}
<documentRef>.update({
"idOffer.<offerId>" : approval
})
From which I expect to get:
<documentid>:{idOffer:{<offerId>:{ref:<ref>, value:<value>, isApproved:<bool>,dateApproved:<date>,fullApproval:<bool>}}}
But I end up with:
<documentid>:{idOffer:{<offerId>:{isApproved:<bool>,dateApproved:<date>,fullApproval:<bool>}}}
Note: I use <> to refer to dynamic data, like document Ids or References.
When you call update with a dictionary (or map, or object, or whatever key/value pair structure used in other languages), the entire set of data behind the given top-level keys are going to be replaced. So, if you call update with a key of idOffer.<offerId>, then everything under that key is going to be replaced, while every other child key of the idOffer level will remain unchanged.
If you don't want to replace the entire object behind the key, then be more specific about which children you'd like to update. In your example, instead of updating a single idOffer.<offerId> key, specify three keys for the nested children:
idOffer.<offerId>.isApproved
idOffer.<offerId>.dateApproved
idOffer.<offerId>.fullApproval
That is to say, the dictionary you pass should have three keyed entries like this at the top level, rather than a single key of idOffer.<offerId>.

Is it possibile to update an entity saved into my database with Graph?

i am using the Graph library in order to save my data correctly.
I was wondering, if there's a way to update an existing Entity, without duplicate the entity 2 times: user will be allowed to update just two values of the entity.
Also, i would like to know if there's a proper save method
I give you an example
let person = Entity(type: "Person")
person["name"] = //not editable
person["work"] = //editable
person["age"] = //editable
graph.sync() //or something like graph.update
Writing this, i am just creating a new entity, which is not what i want. Maybe, i have to search for the entity, delete that every time, and insert the new one?Hope not.
Thank you for any help you could give!
yes, you can update Entities.
You need to search for the one you want to update first. For example:
let graph = Graph()
let search = Search<Entity>(graph: graph).for(types: "Person")
for entity in search.sync() {
// do something
}
Once you have the entity you want to update, you can set any property, group, or tag as per usual.
entity["name"] = "Daniel"
From there you need to call graph.sync() or graph.async() so that all is saved.
Thats it :)

Passing data in a pop/back event to a previous view model

I have a structure like so :
SessionsView -> CreateSessionView
with view models like so :
SessionsViewModel - List of Session objects
CreateSessionViewModel - Single Session Object
The user fills in a form in create session, populates the Session object on the view model, hits done and I call : NavigationController.PopViewControllerAnimated(true) to take them back to the list of sessions.
Is there a way of passing my newly created session object back to the previous views Viewmodel and adding it to its list of session objects? I know how to pass params in a ShowViewModel<TYPE>(PARAM) command, but not sure how to do it whilst navigating back.
Update 1 :
I found 'a' way to do it.. doesn't feel too nice though :
var sessionsView = (SessionsView)NavigationController.ViewControllers.FirstOrDefault(vc => vc is SessionsView);
var sessionsViewModel = (SessionsViewModel)sessionsView.ViewModel;
sessionsViewModel.Sessions.Add(vModel.Session);
NavigationController.PopViewControllerAnimated(true);
Just make use of the return parameter of PopViewControllerAnimated(bool animated).
NavigationController.PopViewControllerAnimated(true);
ViewControllerClass viewController = (ViewControllerClass)NavigationController.TopViewController;
viewController.StoreSessionObject(session); <-- you need to create this method

Resources