doix-db 1.0.26 → 1.0.28
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 +57 -1
- package/lib/model/DbModel.js +1 -0
- package/package.json +1 -1
package/lib/DbClient.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const EventEmitter = require ('events')
|
|
2
2
|
const {randomUUID} = require ('crypto')
|
|
3
|
+
const {Readable} = require ('stream')
|
|
3
4
|
|
|
4
5
|
const DbCsvPrinter = require ('./DbCsvPrinter.js')
|
|
5
6
|
const DbCall = require ('./DbCall.js')
|
|
@@ -146,7 +147,62 @@ class DbClient extends EventEmitter {
|
|
|
146
147
|
|
|
147
148
|
async insert (name, data, options = {}) {
|
|
148
149
|
|
|
149
|
-
|
|
150
|
+
if (data instanceof Readable) {
|
|
151
|
+
|
|
152
|
+
return this.insertStream (name, data, options)
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
else if (Array.isArray (data)) {
|
|
156
|
+
|
|
157
|
+
return this.insertArray (name, data, options)
|
|
158
|
+
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
|
|
162
|
+
return this.insertRecord (name, data, options)
|
|
163
|
+
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async insertStream (name, is, options) {
|
|
169
|
+
|
|
170
|
+
const firstRecord = await new Promise ((ok, fail) => {
|
|
171
|
+
is.once ('error', fail)
|
|
172
|
+
is.once ('readable', () => ok (is.read (1)))
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
if (firstRecord === null) return
|
|
176
|
+
|
|
177
|
+
let {columns} = options; if (!columns) columns = Object.keys (firstRecord)
|
|
178
|
+
|
|
179
|
+
const o = {}; for (const [k, v] of Object.entries (options)) if (k !== 'columns') o [k] = v
|
|
180
|
+
|
|
181
|
+
return new Promise ((ok, fail) => {
|
|
182
|
+
|
|
183
|
+
is.on ('error', fail)
|
|
184
|
+
|
|
185
|
+
const os = this.putObjectStream (name, columns, o)
|
|
186
|
+
|
|
187
|
+
os.on ('error', fail)
|
|
188
|
+
os.on ('complete', ok)
|
|
189
|
+
|
|
190
|
+
os.write (firstRecord)
|
|
191
|
+
is.pipe (os)
|
|
192
|
+
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async insertArray (name, records, options) {
|
|
198
|
+
|
|
199
|
+
return this.insertStream (name, Readable.from (records), options)
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async insertRecord (name, record, options) {
|
|
204
|
+
|
|
205
|
+
const params = this.lang.genInsertParamsSql (name, record, options)
|
|
150
206
|
|
|
151
207
|
const sql = params.pop ()
|
|
152
208
|
|
package/lib/model/DbModel.js
CHANGED