@bdkinc/knex-ibmi 0.0.7 → 0.0.9

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
@@ -1,6 +1,6 @@
1
1
  [![npm version](http://img.shields.io/npm/v/@bdkinc/knex-ibmi.svg)](https://npmjs.org/package/@bdkinc/knex-ibmi)
2
2
 
3
- **Disclaimer: this library is in early stages of development. Please submit an issue for any bugs encounter or any questions you have.**
3
+ **Disclaimer: this library is in alpha. Please submit an issue for any bugs encounter or any questions you have.**
4
4
 
5
5
  ## Description
6
6
 
@@ -17,8 +17,8 @@ This is an external dialect for [knex](https://github.com/tgriesser/knex). This
17
17
  Currently, this dialect has limited functionality compared to the Knex built-in dialects. Below are some of the limitations:
18
18
 
19
19
  - No streaming support
20
+ - Updates return the value of the first column in that row. Make sure your identifier is the first column in the table. The returning option does not work on updates.
20
21
  - Possibly other missing functionality
21
- - Uses a pool for all connections
22
22
  - Journaling must be handled separately. After a migration is ran journaling can be configured on the newly created tables. I recommend using the schema utility in the i access client solutions software.
23
23
 
24
24
  ## Installing
package/dist/index.js CHANGED
@@ -35,7 +35,7 @@ __export(src_exports, {
35
35
  });
36
36
  module.exports = __toCommonJS(src_exports);
37
37
  var process = __toESM(require("process"));
38
- var import_knex = __toESM(require("knex"));
38
+ var import_knex = require("knex");
39
39
  var odbc = __toESM(require("odbc"));
40
40
  var console = __toESM(require("console"));
41
41
 
@@ -318,7 +318,7 @@ var IBMiQueryCompiler = class extends import_querycompiler.default {
318
318
  var ibmi_querycompiler_default = IBMiQueryCompiler;
319
319
 
320
320
  // src/index.ts
321
- var DB2Client = class extends import_knex.default.Client {
321
+ var DB2Client = class extends import_knex.knex.Client {
322
322
  constructor(config) {
323
323
  super(config);
324
324
  this.driverName = "odbc";
@@ -361,7 +361,19 @@ var DB2Client = class extends import_knex.default.Client {
361
361
  this.printDebug("acquiring raw connection");
362
362
  const connectionConfig = this.config.connection;
363
363
  console.log(this._getConnectionString(connectionConfig));
364
- return await this.driver.pool(this._getConnectionString(connectionConfig));
364
+ if (this.pool) {
365
+ const pool = await this.driver.pool({
366
+ connectionString: this._getConnectionString(connectionConfig),
367
+ connectionTimeout: this.pool?.acquireTimeoutMillis || 6e4,
368
+ initialSize: this.pool?.min || 2,
369
+ maxSize: this.pool?.max || 10,
370
+ reuseConnection: true
371
+ });
372
+ return await pool.connect();
373
+ }
374
+ return await this.driver.connect(
375
+ this._getConnectionString(connectionConfig)
376
+ );
365
377
  }
366
378
  // Used to explicitly close a connection, called internally by the pool manager
367
379
  // when a connection times out or the pool is shutdown.
@@ -381,19 +393,20 @@ var DB2Client = class extends import_knex.default.Client {
381
393
  }
382
394
  // Runs the query on the specified connection, providing the bindings
383
395
  // and any other necessary prep work.
384
- async _query(pool, obj) {
396
+ async _query(connection, obj) {
385
397
  if (!obj || typeof obj == "string")
386
398
  obj = { sql: obj };
387
399
  const method = (obj.hasOwnProperty("method") && obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]).toLowerCase();
388
400
  obj.sqlMethod = method;
389
401
  if (method === "select" || method === "first" || method === "pluck") {
390
- const rows = await pool.query(obj.sql, obj.bindings);
402
+ const rows = await connection.query(obj.sql, obj.bindings);
391
403
  if (rows) {
392
404
  obj.response = { rows, rowCount: rows.length };
393
405
  }
394
406
  } else {
407
+ await connection.beginTransaction();
408
+ console.log("transaction begun");
395
409
  try {
396
- const connection = await pool.connect();
397
410
  const statement = await connection.createStatement();
398
411
  await statement.prepare(obj.sql);
399
412
  console.log({ obj });
@@ -409,7 +422,6 @@ var DB2Client = class extends import_knex.default.Client {
409
422
  rowCount: result.length
410
423
  };
411
424
  } else if (method === "update") {
412
- console.log(this.queryCompiler);
413
425
  let returningSelect = obj.sql.replace("update", "select * from ");
414
426
  returningSelect = returningSelect.replace("where", "and");
415
427
  returningSelect = returningSelect.replace("set", "where");
@@ -420,14 +432,21 @@ var DB2Client = class extends import_knex.default.Client {
420
432
  await selectStatement.bind(obj.bindings);
421
433
  }
422
434
  const selected = await selectStatement.execute();
423
- console.log(selected.columns);
424
- obj.response = { rows: selected, rowCount: selected.length };
435
+ obj.response = {
436
+ rows: selected.map(
437
+ (row) => selected.columns.length > 0 ? row[selected.columns[0].name] : row
438
+ ),
439
+ rowCount: selected.length
440
+ };
425
441
  } else {
426
442
  obj.response = { rows: result, rowCount: result.length };
427
443
  }
428
444
  } catch (err) {
429
445
  console.error(err);
430
446
  throw new Error(err);
447
+ } finally {
448
+ console.log("transaction committed");
449
+ await connection.commit();
431
450
  }
432
451
  }
433
452
  return obj;
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
2
  import * as process from "process";
3
- import knex from "knex";
3
+ import { knex } from "knex";
4
4
  import * as odbc from "odbc";
5
5
  import * as console from "console";
6
6
 
@@ -326,7 +326,19 @@ var DB2Client = class extends knex.Client {
326
326
  this.printDebug("acquiring raw connection");
327
327
  const connectionConfig = this.config.connection;
328
328
  console.log(this._getConnectionString(connectionConfig));
329
- return await this.driver.pool(this._getConnectionString(connectionConfig));
329
+ if (this.pool) {
330
+ const pool = await this.driver.pool({
331
+ connectionString: this._getConnectionString(connectionConfig),
332
+ connectionTimeout: this.pool?.acquireTimeoutMillis || 6e4,
333
+ initialSize: this.pool?.min || 2,
334
+ maxSize: this.pool?.max || 10,
335
+ reuseConnection: true
336
+ });
337
+ return await pool.connect();
338
+ }
339
+ return await this.driver.connect(
340
+ this._getConnectionString(connectionConfig)
341
+ );
330
342
  }
331
343
  // Used to explicitly close a connection, called internally by the pool manager
332
344
  // when a connection times out or the pool is shutdown.
@@ -346,19 +358,20 @@ var DB2Client = class extends knex.Client {
346
358
  }
347
359
  // Runs the query on the specified connection, providing the bindings
348
360
  // and any other necessary prep work.
349
- async _query(pool, obj) {
361
+ async _query(connection, obj) {
350
362
  if (!obj || typeof obj == "string")
351
363
  obj = { sql: obj };
352
364
  const method = (obj.hasOwnProperty("method") && obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]).toLowerCase();
353
365
  obj.sqlMethod = method;
354
366
  if (method === "select" || method === "first" || method === "pluck") {
355
- const rows = await pool.query(obj.sql, obj.bindings);
367
+ const rows = await connection.query(obj.sql, obj.bindings);
356
368
  if (rows) {
357
369
  obj.response = { rows, rowCount: rows.length };
358
370
  }
359
371
  } else {
372
+ await connection.beginTransaction();
373
+ console.log("transaction begun");
360
374
  try {
361
- const connection = await pool.connect();
362
375
  const statement = await connection.createStatement();
363
376
  await statement.prepare(obj.sql);
364
377
  console.log({ obj });
@@ -374,7 +387,6 @@ var DB2Client = class extends knex.Client {
374
387
  rowCount: result.length
375
388
  };
376
389
  } else if (method === "update") {
377
- console.log(this.queryCompiler);
378
390
  let returningSelect = obj.sql.replace("update", "select * from ");
379
391
  returningSelect = returningSelect.replace("where", "and");
380
392
  returningSelect = returningSelect.replace("set", "where");
@@ -385,14 +397,21 @@ var DB2Client = class extends knex.Client {
385
397
  await selectStatement.bind(obj.bindings);
386
398
  }
387
399
  const selected = await selectStatement.execute();
388
- console.log(selected.columns);
389
- obj.response = { rows: selected, rowCount: selected.length };
400
+ obj.response = {
401
+ rows: selected.map(
402
+ (row) => selected.columns.length > 0 ? row[selected.columns[0].name] : row
403
+ ),
404
+ rowCount: selected.length
405
+ };
390
406
  } else {
391
407
  obj.response = { rows: result, rowCount: result.length };
392
408
  }
393
409
  } catch (err) {
394
410
  console.error(err);
395
411
  throw new Error(err);
412
+ } finally {
413
+ console.log("transaction committed");
414
+ await connection.commit();
396
415
  }
397
416
  }
398
417
  return obj;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bdkinc/knex-ibmi",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Knex dialect for IBMi",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as process from "process";
2
2
  import { Connection } from "odbc";
3
- import knex, { Knex } from "knex";
3
+ import { knex, Knex } from "knex";
4
4
  import * as odbc from "odbc";
5
5
  import * as console from "console";
6
6
  import SchemaCompiler from "./schema/ibmi-compiler";
@@ -61,7 +61,21 @@ class DB2Client extends knex.Client {
61
61
  this.printDebug("acquiring raw connection");
62
62
  const connectionConfig = this.config.connection;
63
63
  console.log(this._getConnectionString(connectionConfig));
64
- return await this.driver.pool(this._getConnectionString(connectionConfig));
64
+
65
+ if (this.pool) {
66
+ const pool = await this.driver.pool({
67
+ connectionString: this._getConnectionString(connectionConfig),
68
+ connectionTimeout: this.pool?.acquireTimeoutMillis || 60000,
69
+ initialSize: this.pool?.min || 2,
70
+ maxSize: this.pool?.max || 10,
71
+ reuseConnection: true,
72
+ });
73
+ return await pool.connect();
74
+ }
75
+
76
+ return await this.driver.connect(
77
+ this._getConnectionString(connectionConfig),
78
+ );
65
79
  }
66
80
 
67
81
  // Used to explicitly close a connection, called internally by the pool manager
@@ -90,7 +104,7 @@ class DB2Client extends knex.Client {
90
104
 
91
105
  // Runs the query on the specified connection, providing the bindings
92
106
  // and any other necessary prep work.
93
- async _query(pool: any, obj: any) {
107
+ async _query(connection: any, obj: any) {
94
108
  // @ts-ignore
95
109
  // TODO: verify correctness
96
110
  if (!obj || typeof obj == "string") obj = { sql: obj };
@@ -105,13 +119,14 @@ class DB2Client extends knex.Client {
105
119
  // which is needed for queries that modify the database
106
120
 
107
121
  if (method === "select" || method === "first" || method === "pluck") {
108
- const rows: any = await pool.query(obj.sql, obj.bindings);
122
+ const rows: any = await connection.query(obj.sql, obj.bindings);
109
123
  if (rows) {
110
124
  obj.response = { rows, rowCount: rows.length };
111
125
  }
112
126
  } else {
127
+ await connection.beginTransaction();
128
+ console.log("transaction begun");
113
129
  try {
114
- const connection = await pool.connect();
115
130
  const statement = await connection.createStatement();
116
131
  await statement.prepare(obj.sql);
117
132
  console.log({ obj });
@@ -137,7 +152,6 @@ class DB2Client extends knex.Client {
137
152
  // it would be a lot easier if the table-reference function
138
153
  // worked the same for updates as it does inserts
139
154
  // on DB2 LUW it does work so if they ever add it we need to fix
140
- console.log(this.queryCompiler)
141
155
  let returningSelect = obj.sql.replace("update", "select * from ");
142
156
  returningSelect = returningSelect.replace("where", "and");
143
157
  returningSelect = returningSelect.replace("set", "where");
@@ -148,14 +162,22 @@ class DB2Client extends knex.Client {
148
162
  await selectStatement.bind(obj.bindings);
149
163
  }
150
164
  const selected = await selectStatement.execute();
151
- console.log(selected.columns)
152
- obj.response = {rows: selected, rowCount: selected.length}
165
+ obj.response = {
166
+ rows: selected.map((row) =>
167
+ selected.columns.length > 0 ? row[selected.columns[0].name] : row,
168
+ ),
169
+ rowCount: selected.length,
170
+ };
153
171
  } else {
154
172
  obj.response = { rows: result, rowCount: result.length };
155
173
  }
156
174
  } catch (err: any) {
157
175
  console.error(err);
176
+ // await connection.rollback()
158
177
  throw new Error(err);
178
+ } finally {
179
+ console.log("transaction committed");
180
+ await connection.commit();
159
181
  }
160
182
  }
161
183
 
@@ -1,5 +1,4 @@
1
1
  import SchemaCompiler from "knex/lib/schema/compiler";
2
- import * as console from "console";
3
2
 
4
3
  class IBMiSchemaCompiler extends SchemaCompiler {
5
4
  hasTable(tableName) {