delete many in convex-ents
I'm trying to delete multiple ents by IDs but for some reason there is no .delete on Ents returned from getMany unlike an Ent returned from .get.
What is the best way to delete using an array of ids?
7 Replies
I haven't used Ents, but I believe if you await
getMany()
you'll end up with an array of Ents, which you could then loop over and call delete()
on. I'm not certain whether this is the intended approach, but .map()
and .docs()
don't seem like they're made for this specifically or offer any improvement over looping.
Again, this is just from looking at the docs and source a bit, take with multiple grains of salt.Yeah that's what I was trying to do. The returned array consists of Ents with .edge, .edges and .doc. No delete on them. Right now I loop over the ids and query each one using .get(), then I delete them all in a Promise.all but I don't know if it's optimal
Ah okay, I see that in the docs now, retrieved docs only have the edge methods. What methods are on the object returned from
getMany()
if you don't await it?
From the source it looks like .map()
and .docs()
, not sure if either of those could be used to meaningfully improve your current approach.
Maybe @Michal Srb can advise next time he's around1. With ents, doing
ctx.table.getX(someId).delete()
is as efficient as ctx.db.delete(someId)
(so what you did @hyperzone is totally ok!)
2. You should be able to chain the APIs to do delete many ents, or at least to load many ents and then delete them. Can you share the actual code you were trying to use? It might not have worked because of https://github.com/xixixao/convex-ents/issues/2 or https://github.com/xixixao/convex-ents/issues/8GitHub
getMany() doesn't have a fluent API · Issue #2 · xixixao/convex-ents
It should return PromiseEntIDOrNulls and allow map chaining
GitHub
edge() and edgeX() don't allow writing · Issue #8 · xixixao/convex-...
It should be possible to do this inside mutations: await ctx.table("foos").firstX().edge("bla").patch({x: "something"}) but edges currently only return Ents, not EntWr...
I was trying to do:
If this is the whole snippet, you can do:
This snippet is the correct one because Promise.all takes only one arg. Thanks for your help!