Releases: vitaly-t/pg-promise
v.1.1.5
v.1.1.4
v.1.1.2
v.1.1.1
this
for functions-parameters
This is an improvement on the most recent feature - Functions as Parameters.
When formatting a query with a variable-function from an object, the library will now call the function with the same object being the calling context (this
), so the previous code example can be changed to the following:
var account = {
balance: 123.45,
expenses: 2.7,
margin: 0.1,
total: function () {
var t = this.balance + this.expenses;
return this.margin ? (t + t * this.margin / 10) : t;
}
};
db.none("insert into activity values(${balance},${expenses},${total})", account)
.then(function () {
// success;
}, function (reason) {
// error;
});
i.e. where we had to use account
variable before, we now can use this
instead, which is far more convenient, especially when using inline formatting objects that have no names.
It is important to understand that this change has no effect when using simple variable syntax of $1, $2, etc..
, because in this case there is no context object that can be used.
Method as.func
was extended with optional parameter obj
for consistent protocol and testability, with additional tests added.
v.1.1.0
Functions as Parameters
This release is a major non-breaking change, adding support for parameters - functions.
Previously, passing a function as a parameter when formatting a query would have resulted in an error/reject. Now when the parser encounters a function among parameters, it will call the function to get the actual value, and then process the returned value according to its type.
Here's an example:
var account = {
balance: 123.45,
expenses: 2.7,
margin: 0.1,
total: function () {
var t = account.balance + account.expenses;
return account.margin ? (t + t * account.margin / 10) : t;
}
};
db.none("insert into activity values(${balance},${expenses},${total})", account)
.then(function () {
// success;
}, function (reason) {
// error;
});
and the same with simple parameter syntax:
db.none("insert into activity values($1,$2,$3)",
[account.balance, account.expenses, account.total])
.then(function () {
// success;
}, function (reason) {
// failed;
});
NOTE: If function total()
in the example were to throw an error, it would have been handled and resulted in a reject.
As part of this change the library has undergone a significant refactoring towards simplification, because now when formatting queries all existing data types are supported.
The protocol has been extended with method as.func(func, raw)
that's consistent with the library's usage pattern.
P.S. This feature should open many interesting variations in query formatting, making it ultimately flexible.
v.1.0.10
v.1.0.9
v.1.0.8
v.1.0.7
v.1.0.6
This is a quick follow-up on the new Synchronous Transactions feature, reversing parameters passed into the factory function, so the transaction context doesn't need to be re-declared for an inline factory.
See Synchronous Transactions updated with an inline demo.
- Tests updated for the changes;
- Documentation updates.
Update: Renamed the feature into Synchronous Transactions