Skip to content

Commit

Permalink
upgrading for pg-promise v11
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Dec 30, 2022
1 parent 1664860 commit ee14c97
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 53 deletions.
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Detaches from all the events to which attached after the last successful call to

Calling it while not attached will throw `Event monitor not attached.`

## connect(client, dc, fresh, [detailed])
## connect({client, dc, useCount}, [detailed])

Monitors and reports event [connect].

Expand All @@ -198,17 +198,17 @@ Monitors and reports event [connect].

Database Context.

#### fresh
#### useCount

Fresh connection flag.
Number of times the connection has been used.

#### [detailed]

Optional. When set, it reports such connection details as `user@database`.

When not set, it defaults to the value of [monitor.detailed].

## disconnect(client, dc, [detailed])
## disconnect({client, dc}, [detailed])

Monitors and reports event [disconnect].

Expand Down
32 changes: 13 additions & 19 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ const monitor = {

///////////////////////////////////////////////
// 'connect' event handler;
// parameters:
// - client - the only parameter for the event;
// - detailed - optional, indicates that user@database is to be reported;
connect(client, dc, useCount, detailed) {
connect(e, detailed) {
const event = 'connect';
const cp = client ? client.connectionParameters : null;
const cp = e?.client?.connectionParameters;
if (!cp) {
throw new TypeError(errors.redirectParams(event));
}
const d = (detailed === undefined) ? monitor.detailed : !!detailed;
if (d) {
const countInfo = typeof useCount === 'number' ? cct.cn('; useCount: ') + cct.value(useCount) : '';
const countInfo = typeof e.useCount === 'number' ? cct.cn('; useCount: ') + cct.value(e.useCount) : '';
print(null, event, cct.cn('connect(') + cct.value(cp.user + '@' + cp.database) + cct.cn(')') + countInfo);
} else {
print(null, event, cct.cn('connect'));
Expand All @@ -34,12 +31,9 @@ const monitor = {

///////////////////////////////////////////////
// 'connect' event handler;
// parameters:
// - client - the only parameter for the event;
// - detailed - optional, indicates that user@database is to be reported;
disconnect(client, dc, detailed) {
disconnect(e, detailed) {
const event = 'disconnect';
const cp = client ? client.connectionParameters : null;
const cp = e?.client?.connectionParameters;
if (!cp) {
throw new TypeError(errors.redirectParams(event));
}
Expand Down Expand Up @@ -255,9 +249,9 @@ const monitor = {
exists: 'connect' in options
};
if (typeof options.connect === 'function' && !override) {
options.connect = function (client, dc, useCount) {
$state.connect.value(client, dc, useCount); // call the original handler;
self.connect(client, dc, useCount);
options.connect = function (e) {
$state.connect.value(e); // call the original handler;
self.connect(e);
};
} else {
options.connect = self.connect;
Expand All @@ -271,9 +265,9 @@ const monitor = {
exists: 'disconnect' in options
};
if (typeof options.disconnect === 'function' && !override) {
options.disconnect = function (client, dc) {
$state.disconnect.value(client, dc); // call the original handler;
self.disconnect(client, dc);
options.disconnect = function (e) {
$state.disconnect.value(e); // call the original handler;
self.disconnect(e);
};
} else {
options.disconnect = self.disconnect;
Expand Down Expand Up @@ -400,9 +394,9 @@ const monitor = {
}
},

////////////////////////////////////////////////////
//////////////////////////////////////////////////
// global 'detailed' flag override, to report all
// of the optional details that are supported;
// the optional details that are supported;
detailed: true,

//////////////////////////////////////////////////////////////////
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-monitor",
"version": "1.5.0",
"version": "2.0.0",
"description": "Event monitor for pg-promise.",
"main": "lib/index.js",
"typings": "typescript/pg-monitor.d.ts",
Expand Down Expand Up @@ -32,16 +32,16 @@
},
"license": "MIT",
"engines": {
"node": ">=7.6"
"node": ">=14"
},
"dependencies": {
"cli-color": "2.0.3"
},
"devDependencies": {
"coveralls": "3.1.1",
"eslint": "8.23.1",
"eslint": "8.30.0",
"istanbul": "0.4.5",
"jasmine-node": "3.0.0",
"typescript": "4.8.3"
"typescript": "4.9.4"
}
}
14 changes: 7 additions & 7 deletions test/events/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ describe('Connect - Positive', () => {
mon.setLog(log);
});
it('must log detailed message', () => {
mon.connect(client, 123, true);
mon.connect({client, dc: 123}, true);
expect(text).toBe('connect(guest@test)');
});
it('must log short message', () => {
mon.connect(client, 123, true, false);
mon.connect({client, dc: 123}, false);
expect(text).toBe('connect');
});
afterEach(() => {
Expand All @@ -35,11 +35,11 @@ describe('Connect - Positive', () => {
});

describe('indirect call', () => {
let options, text, ctx;
let options, text, params;
beforeEach(() => {
options = {
connect: c => {
ctx = c;
connect: e => {
params = e;
}
};
text = null;
Expand All @@ -50,13 +50,13 @@ describe('Connect - Positive', () => {
};
mon.setLog(log);

options.connect(client, 123, false);
options.connect({client, dc: 123}, false);
});
it('must log detailed message', () => {
expect(text).toBe('connect(guest@test)');
});
it('must call the old method', () => {
expect(ctx).toEqual(client);
expect(params.client).toEqual(client);
});
afterEach(() => {
mon.detach();
Expand Down
16 changes: 7 additions & 9 deletions test/events/disconnect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ describe('Disconnect - Positive', () => {
options = {};
text = null;
mon.attach(options, ['disconnect']);

const log = (msg, info) => {
text = info.text;
info.display = false;
};
mon.setLog(log);

});
it('must log detailed message', () => {
mon.disconnect(client);
mon.disconnect({client});
expect(text).toBe('disconnect(guest@test)');
});
it('must log short message', () => {
mon.disconnect(client, 123, false);
mon.disconnect({client, dc: 123}, false);
expect(text).toBe('disconnect');
});
afterEach(() => {
Expand All @@ -36,11 +34,11 @@ describe('Disconnect - Positive', () => {
});

describe('indirect call', () => {
let options, text, ctx;
let options, text, param;
beforeEach(() => {
options = {
disconnect: c => {
ctx = c;
disconnect: e => {
param = e;
}
};
text = null;
Expand All @@ -52,13 +50,13 @@ describe('Disconnect - Positive', () => {
};
mon.setLog(log);

options.disconnect(client, 123);
options.disconnect({client, dc: 123});
});
it('must log detailed message', () => {
expect(text).toBe('disconnect(guest@test)');
});
it('must call the old method', () => {
expect(ctx).toEqual(client);
expect(param.client).toEqual(client);
});
afterEach(() => {
mon.detach();
Expand Down

0 comments on commit ee14c97

Please sign in to comment.