doix-db 1.0.29 → 1.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/lib/DbClient.js +45 -1
- 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')
|
|
@@ -145,6 +145,50 @@ class DbClient extends EventEmitter {
|
|
|
145
145
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
+
batch (options = {}) {
|
|
149
|
+
|
|
150
|
+
const db = this
|
|
151
|
+
|
|
152
|
+
return new Writable ({...options, objectMode: true,
|
|
153
|
+
|
|
154
|
+
write ({sql, params}, _, callback) {
|
|
155
|
+
|
|
156
|
+
db.do (sql, params).then (_ => callback (), callback)
|
|
157
|
+
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async doAll (statements, options = {}) {
|
|
165
|
+
|
|
166
|
+
if (typeof statements !== 'object') throw Error ('Expected iterable or readable, got ' + statements)
|
|
167
|
+
|
|
168
|
+
if (statements === null) throw Error ('Expected iterable or readable, got null')
|
|
169
|
+
|
|
170
|
+
if (!(statements instanceof Readable)) {
|
|
171
|
+
|
|
172
|
+
if (typeof statements [Symbol.iterator] !== 'function') throw Error ('Expected iterable or readable, got ' + statements)
|
|
173
|
+
|
|
174
|
+
statements = Readable.from (statements)
|
|
175
|
+
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const executor = this.batch (options)
|
|
179
|
+
|
|
180
|
+
return new Promise ((ok, fail) => {
|
|
181
|
+
|
|
182
|
+
statements.on ('error', fail)
|
|
183
|
+
executor.on ('error', fail)
|
|
184
|
+
executor.on ('close', ok)
|
|
185
|
+
|
|
186
|
+
statements.pipe (executor)
|
|
187
|
+
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
}
|
|
191
|
+
|
|
148
192
|
async insert (name, data, options = {}) {
|
|
149
193
|
|
|
150
194
|
if (data instanceof Readable) {
|