@bdkinc/knex-ibmi 0.2.9 → 0.3.0

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 CHANGED
@@ -13,14 +13,12 @@ For more information on IBMi OSS here are the [docs](https://ibmi-oss-docs.readt
13
13
  ## Supported functionality
14
14
 
15
15
  - Query building
16
- - Query execution (see [Limitations](#Limitations))
16
+ - Query execution
17
17
  - Transactions
18
-
19
- ## Limitations
20
-
21
- - No streaming support
18
+ - Streaming
22
19
 
23
20
  ## Installation
21
+
24
22
  ```
25
23
  npm install --save odbc knex @bdkinc/knex-ibmi
26
24
  ```
@@ -29,7 +27,7 @@ Requires Node v16 or higher.
29
27
 
30
28
  ## Dependencies
31
29
 
32
- `npm install odbc` see [odbc](https://github.com/markdirish/node-odbc)
30
+ `npm install odbc` see [odbc](https://github.com/IBM/node-odbc)
33
31
 
34
32
  `npm install knex` see [knex](https://github.com/tgriesser/knex)
35
33
 
@@ -51,10 +49,11 @@ const db = knex({
51
49
  user: "<user>", // IBMi username
52
50
  password: "<password>", // IBMi password
53
51
  driver: "IBM i Access ODBC Driver", // defined in odbcinst.ini
54
- connectionStringParams: { // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
55
- ALLOWPROCCALLS: 1,
52
+ connectionStringParams: {
53
+ // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
54
+ ALLOWPROCCALLS: 1,
56
55
  CMT: 0,
57
- DBQ: 'MYLIB' // library or schema that holds the tables
56
+ DBQ: "MYLIB", // library or schema that holds the tables
58
57
  },
59
58
  },
60
59
  pool: {
@@ -88,17 +87,18 @@ const config = {
88
87
  user: "<user>", // IBMi username
89
88
  password: "<password>", // IBMi password
90
89
  driver: "IBM i Access ODBC Driver", // defined in odbcinst.ini
91
- connectionStringParams: { // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
90
+ connectionStringParams: {
91
+ // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
92
92
  ALLOWPROCCALLS: 1,
93
93
  CMT: 0,
94
- DBQ: 'MYLIB' // library or schema that holds the tables
94
+ DBQ: "MYLIB", // library or schema that holds the tables
95
95
  },
96
96
  },
97
97
  pool: {
98
98
  min: 2,
99
99
  max: 10,
100
100
  },
101
- }
101
+ };
102
102
 
103
103
  const db = knex(config);
104
104
 
@@ -126,10 +126,11 @@ const config: DB2Config = {
126
126
  user: "<user>", // IBMi username
127
127
  password: "<password>", // IBMi password
128
128
  driver: "IBM i Access ODBC Driver", // defined in odbcinst.ini
129
- connectionStringParams: { // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
129
+ connectionStringParams: {
130
+ // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
130
131
  ALLOWPROCCALLS: 1,
131
132
  CMT: 0,
132
- DBQ: 'MYLIB' // library or schema that holds the tables
133
+ DBQ: "MYLIB", // library or schema that holds the tables
133
134
  },
134
135
  },
135
136
  pool: {
@@ -150,6 +151,52 @@ try {
150
151
  }
151
152
  ```
152
153
 
154
+ ### Streaming example
155
+
156
+ ```typescript
157
+ import { knex } from "knex";
158
+ import { DB2Dialect, DB2Config } from "@bdkinc/knex-ibmi";
159
+
160
+ const config: DB2Config = {
161
+ client: DB2Dialect,
162
+ connection: {
163
+ host: "localhost", // hostname or ip address of server
164
+ database: "*LOCAL", // usually named in your odbc.ini connection
165
+ user: "<user>", // IBMi username
166
+ password: "<password>", // IBMi password
167
+ driver: "IBM i Access ODBC Driver", // defined in odbcinst.ini
168
+ connectionStringParams: {
169
+ // DSN connection string parameters https://www.ibm.com/docs/en/i/7.5?topic=details-connection-string-keywords
170
+ ALLOWPROCCALLS: 1,
171
+ CMT: 0,
172
+ DBQ: "MYLIB", // library or schema that holds the tables
173
+ },
174
+ },
175
+ pool: {
176
+ min: 2,
177
+ max: 10,
178
+ },
179
+ };
180
+
181
+ const db = knex(config);
182
+
183
+ try {
184
+ const data = await db
185
+ .select("*")
186
+ .from("table")
187
+ .stream({ fetchSize: 1 }); // optional, fetchSize defaults to 1
188
+
189
+ for await (const record of data) {
190
+ // returns an array of objects, length of array is determined by fetchSize
191
+ console.log(record);
192
+ }
193
+ } catch (err) {
194
+ throw err;
195
+ } finally {
196
+ process.exit();
197
+ }
198
+ ```
199
+
153
200
  ## Configuring your driver
154
201
 
155
202
  If you don't know the name of your installed driver, then look in `odbcinst.ini`. You can find the full path of the file by running `odbcinst -j`.
package/dist/index.d.ts CHANGED
@@ -59,6 +59,10 @@ declare class DB2Client extends knex.Client {
59
59
  destroyRawConnection(connection: any): Promise<any>;
60
60
  _getConnectionString(connectionConfig: DB2ConnectionConfig): string;
61
61
  _query(connection: any, obj: any): Promise<any>;
62
+ _stream(connection: any, obj: any, stream: any, options: {
63
+ fetchSize?: number;
64
+ initialBufferSize?: number;
65
+ }): Promise<unknown>;
62
66
  transaction(container: any, config: any, outerTx: any): Knex.Transaction;
63
67
  schemaCompiler(tableBuilder: any): IBMiSchemaCompiler;
64
68
  tableCompiler(tableBuilder: any): IBMiTableCompiler;
@@ -86,6 +90,7 @@ interface DB2ConnectionParams {
86
90
  DECFLOATROUNDMODE?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
87
91
  MAPDECIMALFLOATDESCRIBE?: 1 | 3;
88
92
  ALLOWPROCCALLS?: 0 | 1;
93
+ XDYNAMIC?: 0 | 1;
89
94
  }
90
95
  interface DB2ConnectionConfig {
91
96
  database: string;
package/dist/index.js CHANGED
@@ -313,6 +313,7 @@ var IBMiQueryCompiler = class extends import_querycompiler.default {
313
313
  var ibmi_querycompiler_default = IBMiQueryCompiler;
314
314
 
315
315
  // src/index.ts
316
+ var import_node_stream = require("stream");
316
317
  var DB2Client = class extends import_knex.knex.Client {
317
318
  constructor(config) {
318
319
  super(config);
@@ -448,6 +449,38 @@ var DB2Client = class extends import_knex.knex.Client {
448
449
  this.printDebug(obj);
449
450
  return obj;
450
451
  }
452
+ _stream(connection, obj, stream, options) {
453
+ if (!obj.sql) throw new Error("A query is required to stream results");
454
+ return new Promise(async (resolve, reject) => {
455
+ stream.on("error", (err) => {
456
+ if (err) {
457
+ connection.__knex__disposed = err;
458
+ }
459
+ reject(err);
460
+ });
461
+ stream.on("end", resolve);
462
+ const readableStream = new import_node_stream.Readable({
463
+ objectMode: true,
464
+ async read() {
465
+ const cursor = await connection.query(obj.sql, obj.bindings, {
466
+ cursor: true,
467
+ fetchSize: options?.fetchSize || 1,
468
+ ...options
469
+ });
470
+ while (!cursor.noData) {
471
+ const result = await cursor.fetch();
472
+ this.push(result);
473
+ }
474
+ await cursor.close();
475
+ }
476
+ });
477
+ readableStream.pipe(stream);
478
+ readableStream.on("error", (err) => {
479
+ reject(err);
480
+ stream.emit("error", err);
481
+ });
482
+ });
483
+ }
451
484
  transaction(container, config, outerTx) {
452
485
  return new ibmi_transaction_default(this, container, config, outerTx);
453
486
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bdkinc/knex-ibmi",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "Knex dialect for IBMi",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",