@mojaloop/database-lib 11.0.5 → 11.1.0
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 +9 -0
- package/audit-ci.jsonc +2 -1
- package/package.json +26 -8
- package/src/database.js +19 -1
- package/test/unit/database.test.js +70 -1
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.0](https://github.com/mojaloop/database-lib/compare/v11.0.6...v11.1.0) (2024-12-20)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add maxPendingAcquire handling and related methods ([#165](https://github.com/mojaloop/database-lib/issues/165)) ([62f80a9](https://github.com/mojaloop/database-lib/commit/62f80a99b922a86d7a8b711c9d58f2ac89ac7d12))
|
|
11
|
+
|
|
12
|
+
### [11.0.6](https://github.com/mojaloop/database-lib/compare/v11.0.5...v11.0.6) (2024-06-26)
|
|
13
|
+
|
|
5
14
|
### [11.0.5](https://github.com/mojaloop/database-lib/compare/v11.0.4...v11.0.5) (2024-04-11)
|
|
6
15
|
|
|
7
16
|
### [11.0.4](https://github.com/mojaloop/database-lib/compare/v11.0.3...v11.0.4) (2024-04-09)
|
package/audit-ci.jsonc
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mojaloop/database-lib",
|
|
3
|
-
"version": "11.0
|
|
3
|
+
"version": "11.1.0",
|
|
4
4
|
"description": "Shared database code for central services",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -41,25 +41,43 @@
|
|
|
41
41
|
"release": "standard-version --releaseCommitMessageFormat 'chore(release): {{currentTag}} [skip ci]'"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
+
"@mojaloop/central-services-error-handling": "13.0.2",
|
|
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
|
+
"yargs-parser": "18.1.1",
|
|
58
|
+
"markdown-it": "12.3.2"
|
|
59
|
+
},
|
|
60
|
+
"widdershins": {
|
|
61
|
+
"swagger2openapi": "7.0.8",
|
|
62
|
+
"yargs-parser": "13.1.2",
|
|
63
|
+
"markdown-it": "12.3.2"
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
50
67
|
},
|
|
51
68
|
"devDependencies": {
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
69
|
+
"@mojaloop/sdk-standard-components": ">=19.x.x",
|
|
70
|
+
"audit-ci": "^7.1.0",
|
|
71
|
+
"npm-check-updates": "17.1.11",
|
|
72
|
+
"nyc": "17.1.0",
|
|
55
73
|
"pre-commit": "1.2.2",
|
|
56
74
|
"proxyquire": "2.1.3",
|
|
57
|
-
"sinon": "
|
|
58
|
-
"standard": "17.1.
|
|
75
|
+
"sinon": "19.0.2",
|
|
76
|
+
"standard": "17.1.2",
|
|
59
77
|
"standard-version": "9.5.0",
|
|
60
78
|
"tap-spec": "^5.0.0",
|
|
61
79
|
"tap-xunit": "2.4.1",
|
|
62
|
-
"tape": "5.
|
|
80
|
+
"tape": "5.9.0",
|
|
63
81
|
"tapes": "4.1.0"
|
|
64
82
|
},
|
|
65
83
|
"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
|
})
|