Skip to content

Commit

Permalink
improving support for prepared statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Apr 24, 2016
1 parent 2916b19 commit d1b3da3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ var monitor = {
print(event, cct.errorTitle("error: ") + cct.error(errMsg));
var q = e.query;
if (q !== undefined && typeof q !== 'string') {
if (typeof q === 'object' && 'name' in q && 'text' in q) {
var tmp = {name: q.name, text: q.text};
if ('values' in q) {
tmp.values = q.values;
}
q = tmp;
}
q = JSON.stringify(q);
}
if (e.cn) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-monitor",
"version": "0.5.2",
"version": "0.5.3",
"description": "Event monitor for pg-promise.",
"main": "lib/index.js",
"scripts": {
Expand Down
44 changes: 44 additions & 0 deletions test/events/errorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,50 @@ describe("Error - Positive", function () {
});
});

describe("query is a prepared statement with params", function () {
var context = {
query: {name: 'test-name', text: 'text-text', values: [123]}
};
var options = {}, text = [];
beforeEach(function () {
mon.attach(options, ['error']);
mon.log = function (msg, info) {
text.push(info.text);
info.display = false;
};
options.error("errMsg", context);
});
it("must parse the value", function () {
expect(text).toEqual(['error: errMsg', 'query: {"name":"test-name","text":"text-text","values":[123]}']);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});

describe("query is a prepared statement without params", function () {
var context = {
query: {name: 'test-name', text: 'text-text'}
};
var options = {}, text = [];
beforeEach(function () {
mon.attach(options, ['error']);
mon.log = function (msg, info) {
text.push(info.text);
info.display = false;
};
options.error("errMsg", context);
});
it("must parse the value", function () {
expect(text).toEqual(['error: errMsg', 'query: {"name":"test-name","text":"text-text"}']);
});
afterEach(function () {
mon.detach();
mon.log = null;
});
});

describe("connection", function () {
var context = {
cn: 123
Expand Down

0 comments on commit d1b3da3

Please sign in to comment.