hyperzone
hyperzone12mo ago

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
erquhart
erquhart12mo ago
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.
hyperzone
hyperzoneOP12mo ago
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
erquhart
erquhart12mo ago
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 around
Michal Srb
Michal Srb12mo ago
1. 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/8
GitHub
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...
hyperzone
hyperzoneOP12mo ago
I was trying to do:
const items = await ctx.table("items").getMany(itemIds); // itemIds is Id<"items">[]

await Promise.all(items.map(item => item.delete()));
const items = await ctx.table("items").getMany(itemIds); // itemIds is Id<"items">[]

await Promise.all(items.map(item => item.delete()));
Michal Srb
Michal Srb12mo ago
If this is the whole snippet, you can do:
await Promise.all(
itemIds.map((id) =>
ctx
.table("items")
.getX(id)
.delete()
)
);
await Promise.all(
itemIds.map((id) =>
ctx
.table("items")
.getX(id)
.delete()
)
);
hyperzone
hyperzoneOP12mo ago
await Promise.all(
itemIds.map((id) =>
ctx
.table("items")
.getX(id)
.delete()
)
);
await Promise.all(
itemIds.map((id) =>
ctx
.table("items")
.getX(id)
.delete()
)
);
This snippet is the correct one because Promise.all takes only one arg. Thanks for your help!

Did you find this page helpful?