@mojaloop/database-lib 11.0.6 → 11.1.1

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/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [11.1.1](https://github.com/mojaloop/database-lib/compare/v11.1.0...v11.1.1) (2025-01-08)
6
+
7
+ ## [11.1.0](https://github.com/mojaloop/database-lib/compare/v11.0.6...v11.1.0) (2024-12-20)
8
+
9
+
10
+ ### Features
11
+
12
+ * add maxPendingAcquire handling and related methods ([#165](https://github.com/mojaloop/database-lib/issues/165)) ([62f80a9](https://github.com/mojaloop/database-lib/commit/62f80a99b922a86d7a8b711c9d58f2ac89ac7d12))
13
+
5
14
  ### [11.0.6](https://github.com/mojaloop/database-lib/compare/v11.0.5...v11.0.6) (2024-06-26)
6
15
 
7
16
  ### [11.0.5](https://github.com/mojaloop/database-lib/compare/v11.0.4...v11.0.5) (2024-04-11)
package/audit-ci.jsonc CHANGED
@@ -3,13 +3,5 @@
3
3
  // audit-ci supports reading JSON, JSONC, and JSON5 config files.
4
4
  // Only use one of ["low": true, "moderate": true, "high": true, "critical": true]
5
5
  "moderate": true,
6
- "allowlist": [
7
- "GHSA-pfrx-2q88-qq97",
8
- "GHSA-4jv9-3563-23j3",
9
- "GHSA-vh95-rmgr-6w4m",
10
- "GHSA-xvch-5gv4-984h",
11
- "GHSA-p8p7-x288-28g6",
12
- "GHSA-72xf-g2v4-qvf3",
13
- "GHSA-w5p7-h5w8-2hfq"
14
- ]
6
+ "allowlist": []
15
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojaloop/database-lib",
3
- "version": "11.0.6",
3
+ "version": "11.1.1",
4
4
  "description": "Shared database code for central services",
5
5
  "main": "src/index.js",
6
6
  "license": "Apache-2.0",
@@ -41,25 +41,44 @@
41
41
  "release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'"
42
42
  },
43
43
  "dependencies": {
44
+ "@mojaloop/central-services-error-handling": "13.0.3",
44
45
  "knex": "3.1.0",
45
46
  "lodash": "4.17.21",
46
47
  "mysql": "2.18.1"
47
48
  },
48
49
  "overrides": {
49
- "tar": "6.2.1"
50
+ "tar": "6.2.1",
51
+ "@mojaloop/ml-schema-transformer-lib": {
52
+ "@mojaloop/central-services-shared": {
53
+ "shins": {
54
+ "ajv": "6.12.3",
55
+ "ejs": "3.1.10",
56
+ "sanitize-html": "2.12.1",
57
+ "markdown-it": "12.3.2"
58
+ },
59
+ "widdershins": {
60
+ "swagger2openapi": "7.0.8",
61
+ "markdown-it": "12.3.2"
62
+ }
63
+ }
64
+ },
65
+ "cross-spawn": "7.0.6",
66
+ "trim": "0.0.3",
67
+ "yargs-parser": "21.1.1"
50
68
  },
51
69
  "devDependencies": {
52
- "audit-ci": "^7.0.1",
53
- "npm-check-updates": "16.14.20",
54
- "nyc": "17.0.0",
70
+ "@mojaloop/sdk-standard-components": ">=19.x.x",
71
+ "audit-ci": "^7.1.0",
72
+ "npm-check-updates": "17.1.13",
73
+ "nyc": "17.1.0",
55
74
  "pre-commit": "1.2.2",
56
75
  "proxyquire": "2.1.3",
57
- "sinon": "18.0.0",
58
- "standard": "17.1.0",
76
+ "sinon": "19.0.2",
77
+ "standard": "17.1.2",
59
78
  "standard-version": "9.5.0",
60
79
  "tap-spec": "^5.0.0",
61
80
  "tap-xunit": "2.4.1",
62
- "tape": "5.8.1",
81
+ "tape": "5.9.0",
63
82
  "tapes": "4.1.0"
64
83
  },
65
84
  "publishConfig": {
package/src/database.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const Knex = require('knex')
4
+ const ErrorHandler = require('@mojaloop/central-services-error-handling')
4
5
  const Table = require('./table')
5
6
  const Utils = require('./utils.js')
6
7
 
@@ -73,6 +74,7 @@ class Database {
73
74
  }
74
75
 
75
76
  this._schema = config.connection.database
77
+ this._maxPendingAcquire = config.maxPendingAcquire
76
78
  this._knex = await configureKnex(config)
77
79
  this._tables = await this._listTables()
78
80
  await this._setTableProperties()
@@ -117,9 +119,25 @@ class Database {
117
119
  }
118
120
  return this._listTableQueries[dbType](this._knex)
119
121
  }
122
+
123
+ assertPendingAcquires () {
124
+ if (this._maxPendingAcquire && this._maxPendingAcquire < this._knex.client.pool.numPendingAcquires()) {
125
+ throw ErrorHandler.CreateFSPIOPError(
126
+ ErrorHandler.Enums.FSPIOPErrorCodes.SERVICE_CURRENTLY_UNAVAILABLE,
127
+ `DB Pool pending acquires ${this._knex.client.pool.numPendingAcquires()} > ${this._maxPendingAcquire}`
128
+ )
129
+ }
130
+ }
131
+
132
+ getPendingAcquires () {
133
+ if (!this._knex) {
134
+ throw new Error('The database must be connected to get the number of pending acquires')
135
+ }
136
+ return this._knex.client.pool.numPendingAcquires()
137
+ }
120
138
  }
121
139
 
122
- const configureKnex = async (config) => {
140
+ const configureKnex = async ({ maxPendingAcquire, ...config }) => {
123
141
  return Knex(config)
124
142
  }
125
143
 
@@ -54,7 +54,7 @@ Test('database', databaseTest => {
54
54
 
55
55
  knexConnStub = sandbox.stub()
56
56
  knexConnStub.destroy = sandbox.stub()
57
- knexConnStub.client = { config: { client: 'mysql' } }
57
+ knexConnStub.client = { config: { client: 'mysql' }, pool: { numPendingAcquires () {} } }
58
58
  knexConnStub.withArgs('information_schema.tables').returns({ where: sandbox.stub().withArgs('TABLE_SCHEMA', 'databaseSchema').returns({ select: sandbox.stub().withArgs('TABLE_NAME').returns(Promise.resolve(tableNames)) }) })
59
59
  knexConnStub.withArgs('pg_catalog.pg_tables').returns({ where: sandbox.stub().withArgs({ schemaname: 'public' }).returns({ select: sandbox.stub().withArgs('tablename').returns(Promise.resolve(tableNames)) }) })
60
60
 
@@ -313,5 +313,74 @@ Test('database', databaseTest => {
313
313
  fromTest.end()
314
314
  })
315
315
 
316
+ databaseTest.test('assertPendingAcquires should', assertPendingAcquiresTest => {
317
+ assertPendingAcquiresTest.test('throw error if pending acquires exceed maxPendingAcquire', async test => {
318
+ // Arrange
319
+ const maxPendingAcquire = 5
320
+ const config = { ...connectionConfig, maxPendingAcquire }
321
+ await dbInstance.connect(config)
322
+ sandbox.stub(dbInstance._knex.client.pool, 'numPendingAcquires').returns(6)
323
+
324
+ // Act & Assert
325
+ try {
326
+ dbInstance.assertPendingAcquires()
327
+ test.fail('Should have thrown error')
328
+ } catch (err) {
329
+ test.equal(err.httpStatusCode, 503, 'Max pending acquires exceeded http status code 503')
330
+ test.equal(err.message, `DB Pool pending acquires 6 > ${maxPendingAcquire}`)
331
+ }
332
+
333
+ test.end()
334
+ })
335
+
336
+ assertPendingAcquiresTest.test('not throw error if pending acquires are within limit', async test => {
337
+ // Arrange
338
+ const maxPendingAcquire = 5
339
+ const config = { ...connectionConfig, maxPendingAcquire }
340
+ await dbInstance.connect(config)
341
+ sandbox.stub(dbInstance._knex.client.pool, 'numPendingAcquires').returns(4)
342
+
343
+ // Act & Assert
344
+ try {
345
+ dbInstance.assertPendingAcquires()
346
+ test.pass('No error thrown')
347
+ } catch (err) {
348
+ test.fail('Should not have thrown error')
349
+ }
350
+
351
+ test.end()
352
+ })
353
+
354
+ assertPendingAcquiresTest.end()
355
+ })
356
+
357
+ databaseTest.test('getPendingAcquires should', getPendingAcquiresTest => {
358
+ getPendingAcquiresTest.test('return the number of pending acquires', async test => {
359
+ // Arrange
360
+ const pendingAcquires = 3
361
+ await dbInstance.connect(connectionConfig)
362
+ sandbox.stub(dbInstance._knex.client.pool, 'numPendingAcquires').returns(pendingAcquires)
363
+
364
+ // Act
365
+ const result = dbInstance.getPendingAcquires()
366
+
367
+ // Assert
368
+ test.equal(result, pendingAcquires)
369
+ test.end()
370
+ })
371
+
372
+ getPendingAcquiresTest.test('throw error if database is not connected', test => {
373
+ try {
374
+ dbInstance.getPendingAcquires()
375
+ test.fail('Should have thrown error')
376
+ } catch (err) {
377
+ test.equal(err.message, 'The database must be connected to get the number of pending acquires')
378
+ test.end()
379
+ }
380
+ })
381
+
382
+ getPendingAcquiresTest.end()
383
+ })
384
+
316
385
  databaseTest.end()
317
386
  })