Pg-promise: UPDATE and INSERT convenience methods

Created on 5 Oct 2015  路  10Comments  路  Source: vitaly-t/pg-promise

Hey there,

I am switching from mysql to postgres, and am having to rewrite some of my inserts and updates. I was wondering if there were some convenience methods or something that I'm missing that would help simplify building these queries. For example, the mysql library allowed this kind of syntax:

var insertUserQuery = 'INSERT INTO user SET ?';

and then:

mysql.query(insertUserQuery, userObject, callback);

but doing it with pg-promise can become cumbersome if you're working with an object that has a lot of properties, especially if you're doing an update with an object and aren't updating all of the fields.

Thanks.

question

Most helpful comment

@RyanMcDonald The Custom Type Formatting is very flexible, and allows formatting override in many ways. I won't be going into every case possible. You should try it yourself first, and see how it works.

I will give a quick example of what can be done, similar to what you described. But that's not the only approach, there can be many ways, depending on what you want precisely.

var pgp = require('pg-promise');

// sets all the object's properties as they are;
function SetValues(obj) {
    this.obj = obj;
    this._rawDBType = true; // raw-text output override;
    this.formatDBType = function () {
        var props = Object.keys(this.obj);
        var s = props.map(function (m) {
            return m + '=${' + m + '}'; // creating the formatting parameters;
        });
        return pgp.as.format(s.join(", "), this.obj); // returning the formatted string;
    }
}

Test

var testObj = {
    first: 123,
    last: "text"
};

var result = pgp.as.format("UPDATE users SET $1", new SetValues(testObj));

console.log(result);

Output

UPDATE users SET first=123, last='text'

All 10 comments

This library formats and executes queries in their natural form. It does not provide any ORM facility beyond variable query formatting, since it is not an ORM.

It does however support Custom Types Formatting which allows complete freedom in formatting queries. You can implement your own type that represents SET ? and then inject it via your custom, pre-formatted type, which can achieve the same what you described.

@RyanMcDonald Did you get this working?

@vitaly-t I'm not sure I understand how to apply the custom formatting in this scenario. Could you provide an example?

@RyanMcDonald The Custom Type Formatting is very flexible, and allows formatting override in many ways. I won't be going into every case possible. You should try it yourself first, and see how it works.

I will give a quick example of what can be done, similar to what you described. But that's not the only approach, there can be many ways, depending on what you want precisely.

var pgp = require('pg-promise');

// sets all the object's properties as they are;
function SetValues(obj) {
    this.obj = obj;
    this._rawDBType = true; // raw-text output override;
    this.formatDBType = function () {
        var props = Object.keys(this.obj);
        var s = props.map(function (m) {
            return m + '=${' + m + '}'; // creating the formatting parameters;
        });
        return pgp.as.format(s.join(", "), this.obj); // returning the formatted string;
    }
}

Test

var testObj = {
    first: 123,
    last: "text"
};

var result = pgp.as.format("UPDATE users SET $1", new SetValues(testObj));

console.log(result);

Output

UPDATE users SET first=123, last='text'

@RyanMcDonald Did this answer your question? If so, then please close the issue.

Closing due to inactivity.

Sorry, I just got around to this. Thanks so much, @vitaly-t, this was exactly what I needed!

@RyanMcDonald You are welcome! :)

There is a further progress with this in 4.0.6 update ;)

F.Y.I. Important breaking change in v6.5.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cortopy picture cortopy  路  5Comments

seanh1414 picture seanh1414  路  4Comments

blendsdk picture blendsdk  路  3Comments

leemhenson picture leemhenson  路  5Comments

alpertuna picture alpertuna  路  4Comments