@bdkinc/knex-ibmi 0.3.0 → 0.3.3
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/README.md +23 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +7 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -156,6 +156,8 @@ try {
|
|
|
156
156
|
```typescript
|
|
157
157
|
import { knex } from "knex";
|
|
158
158
|
import { DB2Dialect, DB2Config } from "@bdkinc/knex-ibmi";
|
|
159
|
+
import { Transform } from "node:stream";
|
|
160
|
+
import { finished } from "node:stream/promises";
|
|
159
161
|
|
|
160
162
|
const config: DB2Config = {
|
|
161
163
|
client: DB2Dialect,
|
|
@@ -186,8 +188,28 @@ try {
|
|
|
186
188
|
.from("table")
|
|
187
189
|
.stream({ fetchSize: 1 }); // optional, fetchSize defaults to 1
|
|
188
190
|
|
|
191
|
+
// use an objectMode transformer
|
|
192
|
+
const transform = new Transform({
|
|
193
|
+
objectMode: true,
|
|
194
|
+
transform(
|
|
195
|
+
chunk: any,
|
|
196
|
+
encoding: BufferEncoding,
|
|
197
|
+
callback: TransformCallback,
|
|
198
|
+
) {
|
|
199
|
+
// chunk will be an array of objects
|
|
200
|
+
// the length of the array is the chunk size
|
|
201
|
+
console.log(chunk);
|
|
202
|
+
callback(null, chunk);
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// pipe through the transformer
|
|
207
|
+
data.pipe(transform);
|
|
208
|
+
|
|
209
|
+
await finished(data); // db queries are promises, we need to wait until resolved
|
|
210
|
+
|
|
211
|
+
// or we can iterate through each record one at a time (fetchSize has no effect)
|
|
189
212
|
for await (const record of data) {
|
|
190
|
-
// returns an array of objects, length of array is determined by fetchSize
|
|
191
213
|
console.log(record);
|
|
192
214
|
}
|
|
193
215
|
} catch (err) {
|
package/dist/index.d.ts
CHANGED
|
@@ -61,7 +61,6 @@ declare class DB2Client extends knex.Client {
|
|
|
61
61
|
_query(connection: any, obj: any): Promise<any>;
|
|
62
62
|
_stream(connection: any, obj: any, stream: any, options: {
|
|
63
63
|
fetchSize?: number;
|
|
64
|
-
initialBufferSize?: number;
|
|
65
64
|
}): Promise<unknown>;
|
|
66
65
|
transaction(container: any, config: any, outerTx: any): Knex.Transaction;
|
|
67
66
|
schemaCompiler(tableBuilder: any): IBMiSchemaCompiler;
|
package/dist/index.js
CHANGED
|
@@ -452,33 +452,28 @@ var DB2Client = class extends import_knex.knex.Client {
|
|
|
452
452
|
_stream(connection, obj, stream, options) {
|
|
453
453
|
if (!obj.sql) throw new Error("A query is required to stream results");
|
|
454
454
|
return new Promise(async (resolve, reject) => {
|
|
455
|
-
stream.on("error",
|
|
456
|
-
if (err) {
|
|
457
|
-
connection.__knex__disposed = err;
|
|
458
|
-
}
|
|
459
|
-
reject(err);
|
|
460
|
-
});
|
|
455
|
+
stream.on("error", reject);
|
|
461
456
|
stream.on("end", resolve);
|
|
457
|
+
const cursor = await connection.query(obj.sql, obj.bindings, {
|
|
458
|
+
cursor: true,
|
|
459
|
+
fetchSize: options?.fetchSize || 1
|
|
460
|
+
});
|
|
462
461
|
const readableStream = new import_node_stream.Readable({
|
|
463
462
|
objectMode: true,
|
|
464
463
|
async read() {
|
|
465
|
-
const cursor = await connection.query(obj.sql, obj.bindings, {
|
|
466
|
-
cursor: true,
|
|
467
|
-
fetchSize: options?.fetchSize || 1,
|
|
468
|
-
...options
|
|
469
|
-
});
|
|
470
464
|
while (!cursor.noData) {
|
|
471
465
|
const result = await cursor.fetch();
|
|
472
466
|
this.push(result);
|
|
473
467
|
}
|
|
468
|
+
this.push(null);
|
|
474
469
|
await cursor.close();
|
|
475
470
|
}
|
|
476
471
|
});
|
|
477
|
-
readableStream.pipe(stream);
|
|
478
472
|
readableStream.on("error", (err) => {
|
|
479
473
|
reject(err);
|
|
480
474
|
stream.emit("error", err);
|
|
481
475
|
});
|
|
476
|
+
readableStream.pipe(stream);
|
|
482
477
|
});
|
|
483
478
|
}
|
|
484
479
|
transaction(container, config, outerTx) {
|