0

Throwing this out there but I know vitaly is such a hawk that he'll probably give me the answer or at least a solid hint ;)

I am basically uploading a batch of records and checking if the Company has already been added to a Company table. If not, I'll add the Company and then add all the records linking with the new CompanyID.

This isn't a pg-promise specific issue for sure but some of the dialogue here https://github.com/vitaly-t/pg-promise/blob/master/examples/select-insert.md show it's a true design concern and people are trying to come up with an elegant solution ... at least back in October.

I'm still new to asynchronous stuff but my gut says while I could insert a delay or possible chain promises together and process them in parallel (https://daveceddia.com/waiting-for-promises-in-a-loop/ or Resolve promises one after another (i.e. in sequence)?) but what vitaly mentions related to single query alternatives may be the real way to do this and not lock down the event loop

I'll hack on this today but I also want to do this in an elegant way since it's a common pattern

Thanks to vitaly in advance ;)

PS you rock vitaly

Redu
  • 25,060
  • 6
  • 56
  • 76
Barry Smith
  • 105
  • 1
  • 2
  • 10
  • I don't know what to add here, the links provided already give you all the possible solutions, and pg-promise bears little relevance here. You have enough at this point to complete your research on your own. – vitaly-t Apr 26 '18 at 12:16
  • Rephrase your para 2 to read "I want to check if the Company has already been added to a Company table; if not, I'll add the Company; then add a batch of records, linking with the existing/new CompanyID." After that, your master expression more-or-less writes itself getOrCreate(companyDetails).then(function(companyID) { uploadBatch(records, companyID); }); All you have to do is `write getOrCreate()` and `uploadBatch()`, both of which should return Promise. – Roamer-1888 Apr 26 '18 at 12:48
  • I guess i was just asking if someone completed your theoretical work and did some sample code @vitaly-t . Since I don't quite understand the single query recommendation you were making I think I'm just going to do the serial promise approach which won't tie up the event loop too bad for what I'm doing. – Barry Smith Apr 26 '18 at 20:04
  • I'm going to read and re-read this https://stackoverflow.com/questions/15939902/is-select-or-insert-in-a-function-prone-to-race-conditions as well ... very thorough ideas in here – Barry Smith Apr 26 '18 at 20:07

0 Answers0