1.4.0
Callback Context
Added this
context to 3 cases where it is applicable, with reference to the protocol object:
- Transaction callback;
- Event
extend
- Sequence factory
Where previously you had to rely on the object - parameter that provided access to the protocol, this
can be used instead from now on.
NOTE: In order to keep compatibility with the existing code, the protocol object is still passed into those callbacks as before, so no changes required.
There is however a slight chance that it may break someone's code, if this
was used within any of those callbacks, with reference to an outside object, which would be an exceptional case, and not a good example of code writing. This is the reason the version has been upped to 1.4, to emphasize that there is a small chance of breaking the existing code.
Transaction callback
Old syntax:
db.tx(function (t) {
return t.query("select * from users");
})
.then(function (data) {
console.log(data); // print result;
}, function (err) {
console.log(err); // print error;
});
New syntax:
db.tx(function () {
return this.query("select * from users");
})
.then(function (data) {
console.log(data); // print result;
}, function (err) {
console.log(err); // print error;
});
NOTE: Parameter t
is still passed, for compatibility.
Event extend
Old syntax:
var options = {
extend: function (obj) {
obj.addImage = function (data) {
return obj.one("insert into images(data) values($1) returning id",
'\\x' + data);
}
}
};
New syntax:
var options = {
extend: function () {
this.addImage = function (data) {
// note: the caller may switch 'this' context;
// and it won't work for repository objects;
return this.one("insert into images(data) values($1) returning id",
'\\x' + data);
}
}
};
NOTE: Parameter obj
is still passed, for compatibility.
Event extend
received this
context for consistency, but it is not as useful there as in other cases, because when setting up repositories of objects, the context changes (this
changes to the repository instance), so you are likely to end up using the old syntax.
Sequence factory
Old syntax
db.tx(function (t) {
return t.sequence(function (idx) {
switch (idx) {
case 0:
return t.query("select 0");
case 1:
return t.query("select 1");
case 2:
return t.query("select 2");
}
});
})
.then(function (data) {
console.log(data); // print result;
}, function (reason) {
console.log(reason); // print error;
});
New syntax
db.tx(function () {
return this.sequence(function (idx) {
switch (idx) {
case 0:
return this.query("select 0");
case 1:
return this.query("select 1");
case 2:
return this.query("select 2");
}
});
})
.then(function (data) {
console.log(data); // print result;
}, function (reason) {
console.log(reason); // print error;
});
NOTE: The old syntax remains to be valid, for compatibility.
Other changes
- Code refactoring
- Test updates
- Documentation updates