Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updateMany, deleteMany #311

Open
FOSSforlife opened this issue Mar 25, 2020 · 6 comments
Open

updateMany, deleteMany #311

FOSSforlife opened this issue Mar 25, 2020 · 6 comments

Comments

@FOSSforlife
Copy link
Contributor

FOSSforlife commented Mar 25, 2020

I would love to see mutations that can update and delete multiple items based on a "where" clause. It would be based on the Postgres functionality that already exists, which Hasura has a nice implementation of.

@goldcaddy77
Copy link
Owner

Good idea. This should be pretty straightforward given I already have a pattern for createMany:

https://github.com/goldcaddy77/warthog/blob/master/src/core/BaseService.ts#L151

If you want to take a crack at this, feel free. You should really just need to search for createMany and put separate paths for update and delete. Otherwise I’ll get to it when I get through the next few items on my list.

@goldcaddy77
Copy link
Owner

A few follow-ups... some assumptions:

  • For updateMany: this would run all validations and hooks (@BeforeUpdate, @AfterUpdate) for each item. Otherwise you could end up with invalid records.
  • For deleteMany: I assume you’d want to run the @BeforeRemove and @AfterRemove hooks, right? Otherwise any side effects that run would not be generated for these deleted items.

Note that running the hooks will require individual saves which will be much slower, but safer.

@FOSSforlife
Copy link
Contributor Author

FOSSforlife commented Mar 25, 2020

Good point. Although I assume that is something TypeORM already takes care of, since it already supports the functionality of updating and deleting multiple records through EntityManager. But we should test it out and make sure it works as expected (and is performant-- not making too many SQL queries for one bulk update)

I'll take a look at it sometime later this week.

Edit: I'm referring to your latest comment about the hooks, in case it wasn't clear

@goldcaddy77
Copy link
Owner

@FOSSforlife - per TypeORM's docs:

Updates entity partially. Entity can be found by a given conditions. Unlike save method executes a primitive operation without cascades, relations and other operations included. Executes fast and efficient UPDATE query. Does not check if entity exist in the database.

So this essentially does an UPDATE query blindly. I could easily get this in place, but I imagine folks would want this to run the hooks. What is your use case?

@FOSSforlife
Copy link
Contributor Author

My use case is enabling users to update their album collections using bulk queries. Thinking about it now, I'm not sure if I'll be writing update or destroy hooks for this, but if I was I would definitely want them to run on updateMany and deleteMany.

I wonder what's the most performant way to do this. I'm imagining that we'd have to batch together any queries that are run in the hooks, so that it's not making a separate SQL query on each update/delete. I'm not sure how hard it would be to do that.

@goldcaddy77
Copy link
Owner

I'm wondering if this is the same discussion that the TypeORM folks had when they were considering adding updateMany and createMany :). There are ways we can do this, but it would just be a bit slower. In theory, you could do all of the before hooks including validations, then run the SQL update, then run all of the after hooks. I'd just need to see if there is a way to do so that isn't completely hacky.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants