File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11const EventEmitter = require ( 'node:events' )
22const plans = require ( './plans' )
3+ const { unwrapSQLResult } = require ( './tools' )
34
45const events = {
56 error : 'error' ,
@@ -58,7 +59,7 @@ class Boss extends EventEmitter {
5859 async #executeSql ( sql , values ) {
5960 const started = Date . now ( )
6061
61- const result = await this . #db. executeSql ( sql , values )
62+ const result = unwrapSQLResult ( await this . #db. executeSql ( sql , values ) )
6263
6364 const ended = Date . now ( )
6465
@@ -130,10 +131,8 @@ class Boss extends EventEmitter {
130131 const queues = rows . map ( q => q . name )
131132
132133 const cacheStatsSql = plans . cacheQueueStats ( this . #config. schema , table , queues )
133- const results = await this . #executeSql( cacheStatsSql )
134-
135- const inter = results . flatMap ( i => i . rows )
136- const warnings = inter . filter ( i => i . queuedCount > ( i . warningQueueSize || WARNINGS . LARGE_QUEUE . size ) )
134+ const { rows : rowsCacheStats } = await this . #executeSql( cacheStatsSql )
135+ const warnings = rowsCacheStats . filter ( i => i . queuedCount > ( i . warningQueueSize || WARNINGS . LARGE_QUEUE . size ) )
137136
138137 for ( const warning of warnings ) {
139138 this . emit ( events . warning , { message : WARNINGS . LARGE_QUEUE . mesasge , data : warning } )
Original file line number Diff line number Diff line change @@ -2,7 +2,22 @@ const { setTimeout } = require('node:timers/promises')
22
33module . exports = {
44 delay,
5- resolveWithinSeconds
5+ resolveWithinSeconds,
6+ unwrapSQLResult
7+ }
8+
9+ /**
10+ * When sql contains multiple queries, result is an array of objects with rows property
11+ * This function unwraps the result into a single object with rows property
12+ * @param {{rows: Array<Object>} | Array<{rows: Array<Object>}> } result
13+ * @returns {{rows: Array<Object>} }
14+ */
15+ function unwrapSQLResult ( result ) {
16+ if ( result instanceof Array ) {
17+ return { rows : result . flatMap ( i => i . rows ) }
18+ }
19+
20+ return result
621}
722
823function delay ( ms , error ) {
Original file line number Diff line number Diff line change 1+ const assert = require ( 'node:assert' )
2+
3+ describe ( 'tools.unwrapSQLResult' , function ( ) {
4+ it ( 'should return the same object when input is an object with rows' , function ( ) {
5+ const { unwrapSQLResult } = require ( '../src/tools' )
6+
7+ const input = { rows : [ { id : 1 } , { id : 2 } ] }
8+ const output = unwrapSQLResult ( input )
9+
10+ assert . strictEqual ( output , input )
11+ assert . deepStrictEqual ( output , input )
12+ } )
13+
14+ it ( 'should flatten an array of results into a single rows array' , function ( ) {
15+ const { unwrapSQLResult } = require ( '../src/tools' )
16+
17+ const part1 = { rows : [ { id : 'a' } ] }
18+ const part2 = { rows : [ { id : 'b' } , { id : 'c' } ] }
19+ const output = unwrapSQLResult ( [ part1 , part2 ] )
20+
21+ assert . deepStrictEqual ( output , { rows : [ part1 . rows , part2 . rows ] . flat ( ) } )
22+ } )
23+
24+ it ( 'should handle empty array by returning empty rows' , function ( ) {
25+ const { unwrapSQLResult } = require ( '../src/tools' )
26+
27+ const output = unwrapSQLResult ( [ ] )
28+ assert . deepStrictEqual ( output , { rows : [ ] } )
29+ } )
30+ } )
You can’t perform that action at this time.
0 commit comments