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

Use an upsert instead of insert + catch + update #7

Open
brendanlong opened this issue Nov 29, 2017 · 0 comments
Open

Use an upsert instead of insert + catch + update #7

brendanlong opened this issue Nov 29, 2017 · 0 comments

Comments

@brendanlong
Copy link
Contributor

Right now you do this:

        self._client.query('INSERT INTO ' + self._table + '(uid,token, origin, ttl) VALUES($1, $2, $3, $4)',[uid, hashedToken, originUrl, (Date.now() + msToLive)], function(err) {
            if(err){
                self._client.query('UPDATE ' + self._table + ' SET token=$1, origin=$2, ttl=$3 WHERE uid=$4',[hashedToken, originUrl, (Date.now() + msToLive), uid], function(err) {

This ignores all errors in the INSERT, and also requires two round trips.

It would be better to do a single query with an ON CONFLICT (uid) DO UPDATE ....

brendanlong added a commit to arenadotio/passwordless-postgrestore that referenced this issue Dec 3, 2017
This uses `ON CONFLICT DO UPDATE` instead of checking the error
and making a second query. This is faster (fewer round trips) and
also simpler (don't need to make sure the error is actually a
conflict -- the server does that for us).

Fixes Battochon#7
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

1 participant