From ee14c97f79345a335726a1921e2536e3b76d21d1 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Fri, 30 Dec 2022 00:44:22 +0000 Subject: [PATCH] upgrading for pg-promise v11 --- .travis.yml | 10 ---------- README.md | 8 ++++---- lib/index.js | 32 +++++++++++++------------------- package.json | 8 ++++---- test/events/connect.spec.js | 14 +++++++------- test/events/disconnect.spec.js | 16 +++++++--------- 6 files changed, 35 insertions(+), 53 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index cb9f09e..0000000 --- a/.travis.yml +++ /dev/null @@ -1,10 +0,0 @@ -language: node_js -sudo: required -matrix: - include: - - node_js: "8" - script: "npm test" - - node_js: "12" - script: "npm test" - - node_js: "14" - script: "npm run travis" diff --git a/README.md b/README.md index 0436fbe..2a855fe 100644 --- a/README.md +++ b/README.md @@ -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]. @@ -198,9 +198,9 @@ Monitors and reports event [connect]. Database Context. -#### fresh +#### useCount -Fresh connection flag. +Number of times the connection has been used. #### [detailed] @@ -208,7 +208,7 @@ 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]. diff --git a/lib/index.js b/lib/index.js index 6a4471d..08b3246 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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')); @@ -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)); } @@ -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; @@ -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; @@ -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, ////////////////////////////////////////////////////////////////// diff --git a/package.json b/package.json index d201e49..9051643 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } diff --git a/test/events/connect.spec.js b/test/events/connect.spec.js index 895aaab..c4e3706 100644 --- a/test/events/connect.spec.js +++ b/test/events/connect.spec.js @@ -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(() => { @@ -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; @@ -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(); diff --git a/test/events/disconnect.spec.js b/test/events/disconnect.spec.js index 190f33a..5ed609c 100644 --- a/test/events/disconnect.spec.js +++ b/test/events/disconnect.spec.js @@ -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(() => { @@ -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; @@ -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();