tjmoses / postgraphile-plugin-batch-create-update-delete

A postgraphile plugin that allows for batch create, update, & delete mutations in a single transaction.
https://www.npmjs.com/package/postgraphile-plugin-many-create-update-delete
MIT License
37 stars 9 forks source link

Configuration options for mutations prefix and ON CONFLICT clause #1

Closed volodymyr-kushnir closed 4 years ago

volodymyr-kushnir commented 4 years ago

Hey πŸ‘‹. I've just found this plugin in the list of PostGraphile community plugins and I'm curious about the overall approach. I've got two questions:

  1. Do you plan to make the mn and @mncud configurable?
  2. Do you plan to add configuration options for ON CONFLICT clause?

It's just that bulkCreate and @bulk seem more natural to me (but of course I could use inflection plugin to rename those, sure). Also I had the same problem with creating many records at once, so I've solved it with SQL function that does INSERT INTO in a FOREACH loop, i.e.

FOREACH location IN ARRAY locations LOOP
  INSERT INTO location (address) VALUES (vars.location.address) ON CONFLICT (address) DO NOTHING;
END LOOP;

but I quickly figured that when you do bulk inserts chances are you'll have an entity that's already in the table, so you probably want to either skip it silently or update the rest of the record, hence ON CONFLICT clause, and this is actually what's stopping me from switching to your plugin. I thought that maybe @bulk with @bulkOnConflict doNothing and @bulkOnConflict doUpdate smart comments could solve the problem (not sure, though, because graphile-upsert-plugin, for instance, uses primaryKeys[0].name in the ON CONFLICT clause, and in my particular case address is UNIQUE, but not PRIMARY, so it may be a little bit more complex than using the first key to detect conflicts). Anyway, my approach with functions doesn't really scale well (but plugin does), so I'm just curious if you have any plans on these or are these out of scope.

tjmoses commented 4 years ago
  1. Yes, I would like to add onto this functionality with maybe more in depth smart comments. I was thinking about adding a way to have specific create, delete, or update mutations generate based off maybe providing @mncud create, update, delete (similar to @omit) and if the options are not specifically provided, then they all are generated like what's currently set. I prefer mn, mainly b/c that's how this plugin is branded and name-spaced to be separate and unique from other generated mutations. I suppose people could possibly rename with the inflection plugin as you mentioned.

  2. Currently, conflicts are set to where if a Create occurs with say, attribute "address" from your example, having just one conflict in the set of values sent causes the transaction to fail completely. After failing, it rolls back the changes to the prior created save point. I personally like this, but know there are a lot of cases out there with different requirements. It's def possible to add a smart comment like you suggested (say @mncudconflict doNothing) with the doNothing / doUpdate value which could be in a conditional to add the ON CONFLICT respective sql.

I too have been using sql functions for batch updates, and personally think they are just not necessary and bloated. I'm definitely open to PRs if you're interested. When I get a chance I'll look into what you proposed and possibly come up with a solution. I want more people to use this plugin, and even hopefully in the future a variant gets added to postgraphile core in some way.

volodymyr-kushnir commented 4 years ago

I'll definitely fork this on the weekend to try and tailor it to my own needs and then we'll see if it would be worth of a PR. Thank you very much for the insights, I guess this issue can be marked as "question" and closed πŸ‘πŸ˜Š.

hegelstad commented 4 years ago

I kinda agree with @volodymyr-kushnir, it would be nice with configurable prefixes/names. many reminds me too much about many-to-many. batch or bulk reminds me more of what this plugin does.

tjmoses commented 4 years ago

I kinda agree with @volodymyr-kushnir, it would be nice with configurable prefixes/names. many reminds me too much about many-to-many. batch or bulk reminds me more of what this plugin does.

When I think of batch/bulk, it makes me think of several repetitions of the exact same thing. These mutations create many batches of potentially different creates/updates from each payload record. My original problem was bulk updating many-to-many tables, lol. That notion plus "mn" was short and worked good with long mutation names is why I chose it. Custom prefixes/names are on the Todo list for options. Let me know if you have any other idea/concerns :-)