doix-db 1.0.29 → 1.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/lib/DbClient.js +88 -2
- package/package.json +1 -1
package/lib/DbClient.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const EventEmitter = require ('events')
|
|
2
2
|
const {randomUUID} = require ('crypto')
|
|
3
|
-
const {Readable} = require ('stream')
|
|
3
|
+
const {Readable, Writable} = require ('stream')
|
|
4
4
|
|
|
5
5
|
const DbCsvPrinter = require ('./DbCsvPrinter.js')
|
|
6
6
|
const DbCall = require ('./DbCall.js')
|
|
@@ -130,7 +130,49 @@ class DbClient extends EventEmitter {
|
|
|
130
130
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
async do (
|
|
133
|
+
async do (q, params = [], options = {}) {
|
|
134
|
+
|
|
135
|
+
let sql
|
|
136
|
+
|
|
137
|
+
if (typeof q === 'string') {
|
|
138
|
+
|
|
139
|
+
sql = q
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
else if (Array.isArray (q)) {
|
|
143
|
+
|
|
144
|
+
const {length} = q; if (length === 0 || length > 2) throw Error ('Invalid `do` array parameter length: ' + length)
|
|
145
|
+
|
|
146
|
+
sql = q [0]
|
|
147
|
+
|
|
148
|
+
if (length === 2) params = q [1]
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
else if (typeof q === 'object') {
|
|
152
|
+
|
|
153
|
+
if ('sql' in q) {
|
|
154
|
+
|
|
155
|
+
sql = q.sql
|
|
156
|
+
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
|
|
160
|
+
throw Error ('Invalid `do` object parameter')
|
|
161
|
+
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if ('params' in q) {
|
|
165
|
+
|
|
166
|
+
params = q.params
|
|
167
|
+
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
|
|
173
|
+
throw Error ('Invalid `do` parameter: ' + q)
|
|
174
|
+
|
|
175
|
+
}
|
|
134
176
|
|
|
135
177
|
const call = this.call (sql, params, {
|
|
136
178
|
...options,
|
|
@@ -145,6 +187,50 @@ class DbClient extends EventEmitter {
|
|
|
145
187
|
|
|
146
188
|
}
|
|
147
189
|
|
|
190
|
+
batch (options = {}) {
|
|
191
|
+
|
|
192
|
+
const db = this
|
|
193
|
+
|
|
194
|
+
return new Writable ({...options, objectMode: true,
|
|
195
|
+
|
|
196
|
+
write (qp, _, callback) {
|
|
197
|
+
|
|
198
|
+
db.do (qp).then (_ => callback (), callback)
|
|
199
|
+
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async doAll (statements, options = {}) {
|
|
207
|
+
|
|
208
|
+
if (typeof statements !== 'object') throw Error ('Expected iterable or readable, got ' + statements)
|
|
209
|
+
|
|
210
|
+
if (statements === null) throw Error ('Expected iterable or readable, got null')
|
|
211
|
+
|
|
212
|
+
if (!(statements instanceof Readable)) {
|
|
213
|
+
|
|
214
|
+
if (typeof statements [Symbol.iterator] !== 'function') throw Error ('Expected iterable or readable, got ' + statements)
|
|
215
|
+
|
|
216
|
+
statements = Readable.from (statements)
|
|
217
|
+
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const executor = this.batch (options)
|
|
221
|
+
|
|
222
|
+
return new Promise ((ok, fail) => {
|
|
223
|
+
|
|
224
|
+
statements.on ('error', fail)
|
|
225
|
+
executor.on ('error', fail)
|
|
226
|
+
executor.on ('close', ok)
|
|
227
|
+
|
|
228
|
+
statements.pipe (executor)
|
|
229
|
+
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
}
|
|
233
|
+
|
|
148
234
|
async insert (name, data, options = {}) {
|
|
149
235
|
|
|
150
236
|
if (data instanceof Readable) {
|