doix-db 0.0.29 → 0.0.31
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 +1 -0
- package/lib/DbClient.js +119 -0
- package/package.json +1 -1
package/index.js
CHANGED
package/lib/DbClient.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
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 getArrayBySql (sql, params = [], options = {}) {
|
|
16
|
+
|
|
17
|
+
const maxRows = options.maxRows || 1000
|
|
18
|
+
const isPartial = options.isPartial === true
|
|
19
|
+
|
|
20
|
+
const s = await this.getStream (sql, params, options)
|
|
21
|
+
const rows = []
|
|
22
|
+
|
|
23
|
+
Object.defineProperty (rows, Symbol.for ('columns'), {
|
|
24
|
+
configurable: false,
|
|
25
|
+
enumerable: false,
|
|
26
|
+
get: () => s [Symbol.for ('columns')]
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
for await (const r of s) {
|
|
30
|
+
|
|
31
|
+
if (rows.length === maxRows && !isPartial) {
|
|
32
|
+
|
|
33
|
+
const err = Error (maxRows + ' rows limit exceeded. Please fix the request or consider using getStream instead of getArray')
|
|
34
|
+
|
|
35
|
+
s.destroy (err)
|
|
36
|
+
|
|
37
|
+
throw err
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
rows.push (r)
|
|
42
|
+
|
|
43
|
+
if (rows.length === maxRows && isPartial) {
|
|
44
|
+
|
|
45
|
+
s.destroy ()
|
|
46
|
+
|
|
47
|
+
break
|
|
48
|
+
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return rows
|
|
54
|
+
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async getArray (q, p, o) {
|
|
58
|
+
|
|
59
|
+
if (!(q instanceof DbQuery)) return this.getArrayBySql (q, p, o)
|
|
60
|
+
|
|
61
|
+
const addCount = 'offset' in q.options, todo = addCount ? [null, this.getScalar (q.toQueryCount ())] : []
|
|
62
|
+
|
|
63
|
+
const params = this.lang.toParamsSql (q), sql = params.pop (); todo [0] = this.getArrayBySql (sql, params, o)
|
|
64
|
+
|
|
65
|
+
const done = await Promise.all (todo), rows = done [0]
|
|
66
|
+
|
|
67
|
+
Object.defineProperty (rows, Symbol.for ('query'), {
|
|
68
|
+
configurable: false,
|
|
69
|
+
enumerable: false,
|
|
70
|
+
get: () => q
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
if (addCount) {
|
|
74
|
+
|
|
75
|
+
const count = parseInt (done [1]), get = () => count
|
|
76
|
+
|
|
77
|
+
Object.defineProperty (rows, Symbol.for ('count'), {
|
|
78
|
+
configurable: false,
|
|
79
|
+
enumerable: false,
|
|
80
|
+
get
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return rows
|
|
86
|
+
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async getObject (sqlOrName, p = [], options = {}) {
|
|
90
|
+
|
|
91
|
+
const params = this.lang.genSelectObjectParamsSql (sqlOrName, p), sql = params.pop ()
|
|
92
|
+
|
|
93
|
+
const a = await this.getArray (sql, params, {
|
|
94
|
+
...options,
|
|
95
|
+
maxRows: 1,
|
|
96
|
+
isPartial: true,
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
if (a.length === 1) return a [0]
|
|
100
|
+
|
|
101
|
+
const {notFound} = options; if (notFound instanceof Error) throw notFound
|
|
102
|
+
|
|
103
|
+
return 'notFound' in options ? notFound : {}
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async getScalar (sql, params = [], options = {}) {
|
|
108
|
+
|
|
109
|
+
return this.getObject (sql, params, {
|
|
110
|
+
notFound: undefined,
|
|
111
|
+
...options,
|
|
112
|
+
rowMode: 'scalar',
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
module.exports = DbClient
|