doix-db 1.0.60 → 1.0.62

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/index.js CHANGED
@@ -2,11 +2,12 @@ const DbQueryBinary = require ('./lib/query/DbQueryBinary.js')
2
2
 
3
3
  module.exports = {
4
4
 
5
- DbCallTracker : require ('./lib/DbCallTracker.js'),
6
5
  DbClient: require ('./lib/DbClient.js'),
7
6
  DbPool: require ('./lib/DbPool.js'),
8
7
  DbLang: require ('./lib/DbLang.js'),
9
8
  DbCsvPrinter: require ('./lib/DbCsvPrinter.js'),
9
+ DbQueue: require ('./lib/DbQueue.js'),
10
+
10
11
  DbColumn: require ('./lib/model/DbColumn.js'),
11
12
  DbReference: require ('./lib/model/DbReference.js'),
12
13
  DbObjectMerger: require ('./lib/model/DbObjectMerger.js'),
package/lib/DbCall.js CHANGED
@@ -18,7 +18,8 @@ const MD_OBJECT = 'object'
18
18
  const MD_SCALAR = 'scalar'
19
19
 
20
20
  const EventEmitter = require ('events')
21
- const {Transform} = require ('stream')
21
+ const {Transform} = require ('stream')
22
+ const {Tracker} = require ('events-to-winston')
22
23
 
23
24
  class DbCall extends EventEmitter {
24
25
 
@@ -93,6 +94,45 @@ class DbCall extends EventEmitter {
93
94
 
94
95
  }
95
96
 
97
+ get [Tracker.LOGGING_PARENT] () {
98
+
99
+ return this.db
100
+
101
+ }
102
+
103
+ get [Tracker.LOGGING_ID] () {
104
+
105
+ return this.ord
106
+
107
+ }
108
+
109
+ get [Tracker.LOGGING_EVENTS] () {
110
+
111
+ return {
112
+
113
+ start: {
114
+ level: 'info',
115
+ message: function () {return this.sql},
116
+ details: {},
117
+ },
118
+
119
+ finish: {
120
+ level: 'info',
121
+ elapsed: true,
122
+ }
123
+
124
+ }
125
+
126
+ }
127
+
128
+ get [Tracker.LOGGING_DETAILS] () {
129
+
130
+ const {params} = this
131
+
132
+ return {params}
133
+
134
+ }
135
+
96
136
  get objectMode () {
97
137
 
98
138
  return this.options.rowMode === MD_OBJECT
package/lib/DbClient.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const EventEmitter = require ('events')
2
2
  const {randomUUID} = require ('crypto')
3
3
  const {Readable, Writable} = require ('stream')
4
+ const {Tracker} = require ('events-to-winston')
4
5
 
5
6
  const DbCsvPrinter = require ('./DbCsvPrinter.js')
6
7
  const DbCall = require ('./DbCall.js')
@@ -38,6 +39,18 @@ class DbClient extends EventEmitter {
38
39
 
39
40
  }
40
41
 
42
+ get [Tracker.LOGGING_PARENT] () {
43
+
44
+ return this.job
45
+
46
+ }
47
+
48
+ get [Tracker.LOGGING_ID] () {
49
+
50
+ return this.name
51
+
52
+ }
53
+
41
54
  async createTempTable (table, options) {
42
55
 
43
56
  if (typeof table === 'string') table = this.model.find (table)
@@ -336,12 +349,19 @@ class DbClient extends EventEmitter {
336
349
 
337
350
  const call = new DbCall (this, q, params, options)
338
351
 
339
- call.tracker = new this.pool.trackerClass (call)
352
+ call.tracker = new Tracker (call, this.pool.logger)
353
+ call.tracker.listen ()
340
354
 
341
355
  return call
342
356
 
343
357
  }
344
358
 
359
+ async peek (queue) {
360
+
361
+ return this.getObject (this.lang.genPeekSql (queue), [], {notFound: null})
362
+
363
+ }
364
+
345
365
  }
346
366
 
347
367
  module.exports = DbClient
package/lib/DbLang.js CHANGED
@@ -528,6 +528,12 @@ class DbLang {
528
528
 
529
529
  }
530
530
 
531
+ genPeekSql ({qName, queue: {order}}) {
532
+
533
+ return `SELECT * FROM ${qName} ORDER BY ${order}`
534
+
535
+ }
536
+
531
537
  }
532
538
 
533
539
  module.exports = DbLang
package/lib/DbPool.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const {ResourcePool} = require ('doix')
2
- const DbCallTracker = require ('./DbCallTracker.js')
3
2
 
4
3
  class DbPool extends ResourcePool {
5
4
 
@@ -12,8 +11,6 @@ class DbPool extends ResourcePool {
12
11
 
13
12
  this.logger = o.logger
14
13
 
15
- this.trackerClass = o.trackerClass || DbCallTracker
16
-
17
14
  this.on ('acquire', db => db.waitFor (this.onAcquire (db)))
18
15
 
19
16
  }
package/lib/DbQueue.js ADDED
@@ -0,0 +1,30 @@
1
+ const {Queue} = require ('doix')
2
+
3
+ class DbQueue extends Queue {
4
+
5
+ constructor (view, o) {
6
+
7
+ const {name} = view
8
+
9
+ if (!('order' in o)) throw Error ('DbQueuePg: order not set for ' + view.name)
10
+
11
+ if ('maxPending' in o && o.maxPending !== 1) throw Error (`DbQueuePg: maxPending=${o.maxPending} set for ${view.name}, must be 1`)
12
+
13
+ super (view.model.db.app, {name, ...o})
14
+
15
+ this.view = view
16
+ this.order = o.order
17
+
18
+ }
19
+
20
+ async peek (job) {
21
+
22
+ const {view} = this
23
+
24
+ return job [view.model.db.name].peek (view)
25
+
26
+ }
27
+
28
+ }
29
+
30
+ module.exports = DbQueue
@@ -1,4 +1,5 @@
1
1
  const DbRelation = require ('./DbRelation.js')
2
+ const DbQueue = require ('../DbQueue.js')
2
3
 
3
4
  class DbView extends DbRelation {
4
5
 
@@ -14,18 +15,18 @@ class DbView extends DbRelation {
14
15
 
15
16
  if (typeof this.wrap !== 'boolean') throw Error (`${this.name}: wrap option must be boolean`)
16
17
 
17
-
18
- }
18
+ }
19
19
 
20
20
  setLang (lang) {
21
21
 
22
22
  super.setLang (lang)
23
-
23
+
24
24
  if (this.wrap) lang.wrapViewSql (this)
25
25
 
26
+ if (this.queue) this.queue = new DbQueue (this, this.queue)
27
+
26
28
  }
27
29
 
28
-
29
30
  }
30
31
 
31
32
  module.exports = DbView
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "1.0.60",
3
+ "version": "1.0.62",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -40,13 +40,12 @@
40
40
  },
41
41
  "homepage": "https://github.com/do-/node-doix-db#readme",
42
42
  "peerDependencies": {
43
- "doix": "^1.0.32"
43
+ "doix": "^1.0.39"
44
44
  },
45
45
  "devDependencies": {
46
46
  "jest": "^29.6.1"
47
47
  },
48
48
  "dependencies": {
49
- "string-escape-map": "^1.0.0",
50
- "subclassable-object-merger": "^1.0.0"
49
+ "string-escape-map": "^1.0.0"
51
50
  }
52
51
  }
@@ -1,70 +0,0 @@
1
- const {LifeCycleTracker} = require ('doix')
2
- const stringEscape = require ('string-escape-map')
3
-
4
- const ESC_PARAMS = new stringEscape ([
5
- ['\t', '\\t'],
6
- ['\n', '\\n'],
7
- ['\r', '\\r'],
8
- [ "'", "''"],
9
- ])
10
-
11
- const stringifyParams = params => {
12
-
13
- if (!Array.isArray (params)) return JSON.stringify (params)
14
-
15
- if (params.length === 0) return ''
16
-
17
- let s = '['; for (const p of params) {
18
-
19
- if (s.length !== 1) s += ', '
20
-
21
- if (typeof p === 'string') {
22
-
23
- s += "'"
24
- s += ESC_PARAMS.escape (p)
25
- s += "'"
26
-
27
- }
28
- else {
29
-
30
- s += p
31
-
32
- }
33
-
34
- }
35
-
36
- return s + ']'
37
-
38
- }
39
-
40
- class DbCallTracker extends LifeCycleTracker {
41
-
42
- constructor (call) {
43
-
44
- const {db} = call
45
-
46
- super (call, db.pool.logger)
47
-
48
- this.call = call
49
-
50
- this.prefix = db.job.tracker.prefix + '/' + db.uuid + '/' + call.ord
51
-
52
- }
53
-
54
- startMessage () {
55
-
56
- const {sql, params} = this.call
57
-
58
- const s = super.startMessage () + ' ' + sql
59
-
60
- if (params.length === 0) return s
61
-
62
- return s + ' ' + stringifyParams (params)
63
-
64
- }
65
-
66
- }
67
-
68
- DbCallTracker.stringifyParams = stringifyParams
69
-
70
- module.exports = DbCallTracker