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

Recommended practice for issuing a DELETE? #313

Closed
catastrophe-brandon opened this issue Apr 17, 2017 · 2 comments
Closed

Recommended practice for issuing a DELETE? #313

catastrophe-brandon opened this issue Apr 17, 2017 · 2 comments
Labels

Comments

@catastrophe-brandon
Copy link

In some POC code I've been working on, it seems that if I issue a DELETE statement using pg.one an error is thrown. To work around the issue I've been executing the statement using pg.none, but I'm uncomfortable with the fact that I can't confirm deletion occurred.

Here's my question: What is the recommended practice for issuing delete statements using pg-promise?

Here is the code I've been using. Forgive me if some of what I've done doesn't make sense; I'm rather new to Node.


function removeItem(req, res) {
    "use strict";
    console.log("Attempting to delete item " + req.params.id)
    pg.none(
        "DELETE FROM ITEMS WHERE ID=$1",
        [req.params.id]
    ).then (
        data => {
            console.log("Deletion stuff happened");
            res.sendStatus(204).send({message: "Deleted item with id: " + data.id})
        }
    ).catch(error => {
        "use strict";

        if (error.name == "QueryResultError") {
            console.log("Nothing was deleted");
            res.sendStatus(404);
        }

        console.log('ERR: ', error);
        res.sendStatus(500).send({message: error});
    })

}

@vitaly-t
Copy link
Owner

vitaly-t commented Apr 17, 2017

By using the right method + RETURNING * you can monitor how many records are being deleted, as you would receive back the rows that just have been deleted:

  • db.one('DELETE FROM table_name WHERE id = $1 RETURNING *', 123) - will reject, if it deletes anything other than one record.
  • db.many('DELETE FROM table_name WHERE id = $1 RETURNING *', 123) - will reject, if it deletes less than 1 record.

and so on.

Alternatively, use the result method, which resolves with verbose Result object, which includes, among other things, the number of rows affected (deleted in your case) - property rowCount:

db.result('DELETE FROM table_name WHERE id = $1', 123, a => a.rowCount) - will resolve with the number of rows deleted.

The last approach is best-performing, as it doesn't request any rows back, only the counter.

@vitaly-t vitaly-t changed the title Q: Recommended practice for issuing DELETE using pg-promise? Q: Recommended practice for issuing DELETE? Apr 17, 2017
@vitaly-t vitaly-t changed the title Q: Recommended practice for issuing DELETE? Recommended practice for issuing DELETE? Apr 17, 2017
@vitaly-t vitaly-t changed the title Recommended practice for issuing DELETE? Recommended practice for issuing a DELETE? Apr 17, 2017
@catastrophe-brandon
Copy link
Author

Thank you! This helps clarify things a lot!

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

No branches or pull requests

2 participants