doix-db 0.0.28 → 0.0.30

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
@@ -1,5 +1,6 @@
1
1
  module.exports = {
2
2
  DbEventLogger : require ('./lib/DbEventLogger.js'),
3
+ DbClient: require ('./lib/DbClient.js'),
3
4
  DbPool: require ('./lib/DbPool.js'),
4
5
  DbLang: require ('./lib/DbLang.js'),
5
6
  DbColumn: require ('./lib/model/DbColumn.js'),
@@ -0,0 +1,77 @@
1
+ const EventEmitter = require ('events')
2
+ const {randomUUID} = require ('crypto')
3
+ const DbQuery = require ('./query/DbQuery.js')
4
+
5
+ class DbClient extends EventEmitter {
6
+
7
+ constructor () {
8
+
9
+ super ()
10
+
11
+ this.uuid = randomUUID ()
12
+
13
+ }
14
+
15
+ async getArray (q, p, o) {
16
+
17
+ if (!(q instanceof DbQuery)) return this.getArrayBySql (q, p, o)
18
+
19
+ const addCount = 'offset' in q.options, todo = addCount ? [null, this.getScalar (q.toQueryCount ())] : []
20
+
21
+ const params = this.lang.toParamsSql (q), sql = params.pop (); todo [0] = this.getArrayBySql (sql, params, o)
22
+
23
+ const done = await Promise.all (todo), rows = done [0]
24
+
25
+ Object.defineProperty (rows, Symbol.for ('query'), {
26
+ configurable: false,
27
+ enumerable: false,
28
+ get: () => q
29
+ })
30
+
31
+ if (addCount) {
32
+
33
+ const count = parseInt (done [1]), get = () => count
34
+
35
+ Object.defineProperty (rows, Symbol.for ('count'), {
36
+ configurable: false,
37
+ enumerable: false,
38
+ get
39
+ })
40
+
41
+ }
42
+
43
+ return rows
44
+
45
+ }
46
+
47
+ async getObject (sqlOrName, p = [], options = {}) {
48
+
49
+ const params = this.lang.genSelectObjectParamsSql (sqlOrName, p), sql = params.pop ()
50
+
51
+ const a = await this.getArray (sql, params, {
52
+ ...options,
53
+ maxRows: 1,
54
+ isPartial: true,
55
+ })
56
+
57
+ if (a.length === 1) return a [0]
58
+
59
+ const {notFound} = options; if (notFound instanceof Error) throw notFound
60
+
61
+ return 'notFound' in options ? notFound : {}
62
+
63
+ }
64
+
65
+ async getScalar (sql, params = [], options = {}) {
66
+
67
+ return this.getObject (sql, params, {
68
+ notFound: undefined,
69
+ ...options,
70
+ rowMode: 'scalar',
71
+ })
72
+
73
+ }
74
+
75
+ }
76
+
77
+ module.exports = DbClient
package/lib/DbLang.js CHANGED
@@ -147,12 +147,10 @@ class DbLang {
147
147
  }
148
148
 
149
149
  genSelectObjectParamsSql (sqlOrName, params) {
150
-
151
- const {model} = this; if (!model) throw Error ('Model not set')
152
150
 
153
151
  if (!Array.isArray (params)) params = [params]
154
152
 
155
- const {map} = model; if (map.has (sqlOrName)) {
153
+ const {map} = this.model || {}; if (map && map.has (sqlOrName)) {
156
154
 
157
155
  const {qName, pk, columns} = map.get (sqlOrName)
158
156
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "Shared database related code for doix",
5
5
  "main": "index.js",
6
6
  "files": [