Skip to content

upsertWithWhere method - #1001

Merged
Amir-61 merged 1 commit into
loopbackio:masterfrom
mountain1234585:upsertNPK
Aug 16, 2016
Merged

upsertWithWhere method#1001
Amir-61 merged 1 commit into
loopbackio:masterfrom
mountain1234585:upsertNPK

Conversation

@mountain1234585

@mountain1234585 mountain1234585 commented Jul 13, 2016

Copy link
Copy Markdown

Added a new method for performing upsertWithWhere in dao.js

Connect to strongloop/loopback#2331

Connect to strongloop/loopback#2539

@mountain1234585

mountain1234585 commented Jul 13, 2016

Copy link
Copy Markdown
Author

@raymondfeng @Amir-61 : Please consider this pull request. I believe following things are pending from the merge : !) Test case addition 2) All tests passing Please let me know if anything else is pending for the merge to happen.

I have already started to write the tests. Should be able to commit by tomorrow.

Also, I have already signed the CLA and when I click on details, it says you have signed the CLA. I was wondering why is it saying not all contributors have signed the CLA. This is a fresh fork with a fresh commit and a new pull request.

Reference : I have incorporated the following review comments from @raymondfeng and @Amir-61 :
@raymondfeng

  1. why we don't update multiple instances rather return error if multiple instances are found.
    This was discussed as part of the requirement we had and we plan to restrict this only for single instance update. I had a look at how upsert with foreign key is performed in some of the popularly used endpoints and they follow the similar approach.

  2. why the key value pair mentioned in the "where" is merged into data
    In case of create, we want the field to be updated when a new record is created. And similar is seen in upsert operation with Salesforce as well.

  3. method signature is wrong in memory.js
    I have now removed the method in memory.js since the entire logic is handled in dao.js

  4. too many empty lines in dao.js
    I have removed them now.
    5)wrapped the callback with process.nextTick
    Made the changes with the latest commit now

@Amir-61
resolved the eslint errors , Signed the CLA

I wanted to get these changes merged soon since we have a blocking , high priority requirement. Request your support and guidance in doing the same.

@mountain1234585

Copy link
Copy Markdown
Author

Follow up from : #995

@Amir-61

Amir-61 commented Jul 13, 2016

Copy link
Copy Markdown
Contributor

@mountain1234585

I'm confused why creating another PR, while you had another open PR#995.

So IIUC you are making the scope of this work smaller by proposing to skip the atomic implementation for connectors including memory connector as well as skipping operation hooks? This may lead to inconsistency between our approach for other methods; with that being said, I leave the decision to @raymondfeng @bajtos.

@Amir-61 Amir-61 assigned Amir-61 and unassigned Amir-61 Jul 13, 2016
@mountain1234585

mountain1234585 commented Jul 13, 2016

Copy link
Copy Markdown
Author

@Amir-61
I had got too many conflicts in that PR, while trying to do a rebase and sync. Hence created a new fork and PR, not to mess it up anymore. However, I have retained the comments I had received after you and Raymond had reviewed in the comment above so that it comes handy.
This implementation is as per the requirement we have which I have discussed with Raymond Feng earlier . I have tested these changes for memory connector, salesforce and sapodata (our inhouse connector) and it works perfectly fine. Also, I have ensured, for connectors who want to use their native endpoint APIs , can provide their own implementation in the connector runtime code and those who want to fallback to the default implementation can do so. As you suggested yesterday, I have started writing the tests. Please let me know if you have any more questions.

@bajtos

bajtos commented Jul 14, 2016

Copy link
Copy Markdown
Member
  1. method signature is wrong in memory.js
    I have now removed the method in memory.js since the entire logic is handled in dao.js

It's important to provide an atomic implementation in the memory connector. The current implementation, which executes multiple database commands, is prone to race conditions. (Consider what happens when two requests to upsert the same record are handled at the same time in parallel.)

skipping operation hooks

The promise of operation hooks is that regardless of the operation invoked, the hook observers will be always executed. I.e. if a user installs "before save" observer, they can be confident their handler will be always invoked before any change is being persisted.

I am not comfortable breaking that promise. I would much rather see operation hooks implemented in as part of this patch.

It's ok to keep this work incremental though. You can continue adding unit-tests for the functionality already there, and leave implementation of operation hooks only after the other known issues are resolved.

Comment thread lib/dao.js
. This is a PUT operation and based on
* non-primary key. The filter takes one key value pair and updates single matching instance.*
*/
DataAccessObject.upsertWithWhere = function(where, data, options, cb) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In longer term, we want to promote patchOrCreate instead of upsert/updateOrCreate. Can you please add an alias patchOrCreateWithWhere = upsertWithWhere? See how other aliases in this file are set up.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@mountain1234585

Copy link
Copy Markdown
Author

@bajtos
I will look into the operation hooks as per your suggestion and will revert if I need help. Thanks for the review.

@bajtos

bajtos commented Jul 14, 2016

Copy link
Copy Markdown
Member

@slnode ok to test

@mountain1234585

Copy link
Copy Markdown
Author

@bajtos .. one clarification.. so will it be ok to merge after resolving the comments you have suggested to the current implementation and along with unit tests passing. And then incrementally add on these operation hooks soon after the first merge. Or, do we need to have everything together for the first merge.

@bajtos

bajtos commented Jul 14, 2016

Copy link
Copy Markdown
Member

so will it be ok to merge after resolving the comments you have suggested to the current implementation and along with unit tests passing. And then incrementally add on these operation hooks soon after the first merge. Or, do we need to have everything together for the first merge.

I would prefer to have everything together for the first merge.

@mountain1234585

Copy link
Copy Markdown
Author

@bajtos
I have now implemented the operation hooks and did a quick test with two scenarios :

  1. Connector has its own implementation
    2)Connector want to use the default implementation.
    It worked as expected. I will go ahead and make other modifications as per your comments and commit the changes to PR for your review.

@mountain1234585

Copy link
Copy Markdown
Author

@bajtos
I have addressed all your comments except having the implementation in memory.js, which I am working on currently. Please review the other changes I have done and let me know your comments. Thanks.

Comment thread lib/dao.js

var pk = idName(Model);
if (keys[0] !== pk) return false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, please remove. same applies for the removed line above.

@mountain1234585

mountain1234585 commented Aug 10, 2016

Copy link
Copy Markdown
Author

@Amir-61 I have squashed all commits into one now and rebased on top of master.

Comment thread lib/dao.js
hookState: ctx.hookState,
options: options,
};
Model.notifyObserversOf('loaded', contxt, function(err) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ummm not sure why you could not use context as opposed to contxt; probably it conflicts the name. How about using ctx instead of contxt which is missing e

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are making use of ctx.hookstate , which is defined before we define the context properties for 'loaded'. Using ctx instead will make ctx.hookState undefined. Hence, I have left the variable name as contxt .

@Amir-61

Amir-61 commented Aug 12, 2016

Copy link
Copy Markdown
Contributor

@Amir-61 I have squashed all commits into one now and rebased on top of master.

Thanks! I left a few minor comments which should be pretty easy to address; the code looks almost good to me. I leave the final approval to @bajtos.

@superkhau also you may look into this patch as well before @bajtos goes for final approval.

@superkhau

Copy link
Copy Markdown
Contributor

@raymondfeng @bajtos I've looked at this PR enough and I am about as confident as I can be. Gonna need your opinions here for final LGTM.

@Amir-61

Amir-61 commented Aug 12, 2016

Copy link
Copy Markdown
Contributor

LGTM from my side after addressing my minor comments. Final LGTM needs to be either from @bajtos or @raymondfeng

@bajtos

bajtos commented Aug 15, 2016

Copy link
Copy Markdown
Member

I took a quick look and did not see any obvious problems. Let's get this landed, we can always fix bugs later when they are discovered by a real user.

@Amir-61
Amir-61 merged commit 56aeeeb into loopbackio:master Aug 16, 2016
@Amir-61 Amir-61 removed the #review label Aug 16, 2016
@Amir-61

Amir-61 commented Aug 16, 2016

Copy link
Copy Markdown
Contributor

@mountain1234585 Thanks for the contribution! I landed it! 🚢

@Amir-61

Amir-61 commented Aug 16, 2016

Copy link
Copy Markdown
Contributor

I will back-port it 2.x as well

@mountain1234585

Copy link
Copy Markdown
Author

Thankyou @Amir-61 @bajtos @superkhau @rmg @raymondfeng : For your valuable feedback and the reviews on this PR. I had a great learning !

@Amir-61

Amir-61 commented Aug 16, 2016

Copy link
Copy Markdown
Contributor

Backport PR #1052

@Amir-61

Amir-61 commented Aug 16, 2016

Copy link
Copy Markdown
Contributor

Thankyou @Amir-61 @bajtos @superkhau @rmg @raymondfeng : For your valuable feedback and the reviews on this PR. I had a great learning !

No problem. My pleasure!

Amir-61 pushed a commit that referenced this pull request Aug 23, 2016
Amir-61 pushed a commit that referenced this pull request Aug 29, 2016
Amir-61 pushed a commit that referenced this pull request Sep 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants