@mojaloop/database-lib 11.1.3 → 11.1.4-snapshot.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojaloop/database-lib",
3
- "version": "11.1.3",
3
+ "version": "11.1.4-snapshot.2",
4
4
  "description": "Shared database code for central services",
5
5
  "main": "src/index.js",
6
6
  "license": "Apache-2.0",
@@ -43,7 +43,8 @@
43
43
  "release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'"
44
44
  },
45
45
  "dependencies": {
46
- "@mojaloop/central-services-error-handling": "13.0.6",
46
+ "@mojaloop/central-services-error-handling": "13.0.7",
47
+ "async-exit-hook": "^2.0.1",
47
48
  "knex": "3.1.0",
48
49
  "lodash": "4.17.21",
49
50
  "mysql": "2.18.1"
@@ -70,9 +71,9 @@
70
71
  "yargs-parser": "21.1.1"
71
72
  },
72
73
  "devDependencies": {
73
- "@mojaloop/sdk-standard-components": ">=19.x.x",
74
+ "@mojaloop/sdk-standard-components": "19.10.3",
74
75
  "audit-ci": "^7.1.0",
75
- "npm-check-updates": "17.1.14",
76
+ "npm-check-updates": "17.1.15",
76
77
  "nyc": "17.1.0",
77
78
  "pre-commit": "1.2.2",
78
79
  "proxyquire": "2.1.3",
package/src/database.js CHANGED
@@ -4,6 +4,7 @@ const Knex = require('knex')
4
4
  const ErrorHandler = require('@mojaloop/central-services-error-handling')
5
5
  const Table = require('./table')
6
6
  const Utils = require('./utils.js')
7
+ const exitHook = require('async-exit-hook')
7
8
 
8
9
  /* Default config to fall back to when using deprecated URI connection string */
9
10
  const defaultConfig = {
@@ -78,6 +79,8 @@ class Database {
78
79
  this._knex = await configureKnex(config)
79
80
  this._tables = await this._listTables()
80
81
  await this._setTableProperties()
82
+
83
+ exitHook(() => this.disconnect())
81
84
  }
82
85
 
83
86
  async disconnect () {
@@ -13,6 +13,7 @@ Test('database', databaseTest => {
13
13
  let knexConnStub
14
14
  let Database
15
15
  let dbInstance
16
+ let exitHookStub
16
17
 
17
18
  const connectionConfig = {
18
19
  client: 'mysql',
@@ -52,6 +53,8 @@ Test('database', databaseTest => {
52
53
  databaseTest.beforeEach(t => {
53
54
  sandbox = Sinon.createSandbox()
54
55
 
56
+ exitHookStub = sandbox.stub()
57
+
55
58
  knexConnStub = sandbox.stub()
56
59
  knexConnStub.destroy = sandbox.stub()
57
60
  knexConnStub.client = { config: { client: 'mysql' }, pool: { numPendingAcquires () {} } }
@@ -62,7 +65,7 @@ Test('database', databaseTest => {
62
65
 
63
66
  tableStub = sandbox.stub()
64
67
 
65
- Database = Proxyquire(`${src}/database`, { knex: knexStub, './table': tableStub })
68
+ Database = Proxyquire(`${src}/database`, { knex: knexStub, './table': tableStub, 'async-exit-hook': exitHookStub })
66
69
  dbInstance = new Database()
67
70
 
68
71
  t.end()
@@ -382,5 +385,40 @@ Test('database', databaseTest => {
382
385
  getPendingAcquiresTest.end()
383
386
  })
384
387
 
388
+ databaseTest.test('connect should', connectTest => {
389
+ connectTest.test('register exit hook', async test => {
390
+ // Act
391
+ await dbInstance.connect(connectionConfig)
392
+
393
+ // Assert
394
+ test.ok(exitHookStub.calledOnce)
395
+ test.ok(exitHookStub.calledWith(Sinon.match.func))
396
+
397
+ // Cleanup
398
+ test.end()
399
+ })
400
+
401
+ connectTest.test('call disconnect on process exit', async test => {
402
+ // Arrange
403
+ // Simulate process exit by manually calling the registered function
404
+ await dbInstance.connect(connectionConfig)
405
+ const exitCallback = exitHookStub.firstCall.args[0] // Get the first function registered
406
+ const disconnectStub = sandbox.stub(dbInstance, 'disconnect').resolves()
407
+
408
+ // Act
409
+ exitCallback() // Simulate process exit
410
+
411
+ // Assert
412
+ test.ok(exitHookStub.calledOnce)
413
+ test.ok(disconnectStub.calledOnce)
414
+
415
+ // Cleanup
416
+ disconnectStub.restore()
417
+ test.end()
418
+ })
419
+
420
+ connectTest.end()
421
+ })
422
+
385
423
  databaseTest.end()
386
424
  })