@bdkinc/knex-ibmi 0.0.8 → 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
 
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,19 @@ 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 {
395
- const connection = await pool.connect();
396
407
  await connection.beginTransaction();
408
+ console.log("transaction begun");
397
409
  try {
398
410
  const statement = await connection.createStatement();
399
411
  await statement.prepare(obj.sql);
@@ -420,17 +432,20 @@ 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
- obj.response = { rows: selected.map(
424
- (row) => selected.columns.length > 0 ? row[selected.columns[0].name] : row
425
- ), 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
+ };
426
441
  } else {
427
442
  obj.response = { rows: result, rowCount: result.length };
428
443
  }
429
444
  } catch (err) {
430
445
  console.error(err);
431
- await connection.rollback();
432
446
  throw new Error(err);
433
447
  } finally {
448
+ console.log("transaction committed");
434
449
  await connection.commit();
435
450
  }
436
451
  }
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,19 @@ 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 {
360
- const connection = await pool.connect();
361
372
  await connection.beginTransaction();
373
+ console.log("transaction begun");
362
374
  try {
363
375
  const statement = await connection.createStatement();
364
376
  await statement.prepare(obj.sql);
@@ -385,17 +397,20 @@ var DB2Client = class extends knex.Client {
385
397
  await selectStatement.bind(obj.bindings);
386
398
  }
387
399
  const selected = await selectStatement.execute();
388
- obj.response = { rows: selected.map(
389
- (row) => selected.columns.length > 0 ? row[selected.columns[0].name] : row
390
- ), 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
+ };
391
406
  } else {
392
407
  obj.response = { rows: result, rowCount: result.length };
393
408
  }
394
409
  } catch (err) {
395
410
  console.error(err);
396
- await connection.rollback();
397
411
  throw new Error(err);
398
412
  } finally {
413
+ console.log("transaction committed");
399
414
  await connection.commit();
400
415
  }
401
416
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bdkinc/knex-ibmi",
3
- "version": "0.0.8",
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,13 @@ 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 {
113
- const connection = await pool.connect();
114
127
  await connection.beginTransaction();
128
+ console.log("transaction begun");
115
129
  try {
116
130
  const statement = await connection.createStatement();
117
131
  await statement.prepare(obj.sql);
@@ -148,20 +162,23 @@ class DB2Client extends knex.Client {
148
162
  await selectStatement.bind(obj.bindings);
149
163
  }
150
164
  const selected = await selectStatement.execute();
151
- obj.response = {rows: selected.map((row) =>
152
- selected.columns.length > 0 ? row[selected.columns[0].name] : row,
153
- ), 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
+ };
154
171
  } else {
155
172
  obj.response = { rows: result, rowCount: result.length };
156
173
  }
157
174
  } catch (err: any) {
158
175
  console.error(err);
159
- await connection.rollback()
176
+ // await connection.rollback()
160
177
  throw new Error(err);
161
178
  } finally {
162
- await connection.commit()
179
+ console.log("transaction committed");
180
+ await connection.commit();
163
181
  }
164
-
165
182
  }
166
183
 
167
184
  return obj;
@@ -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) {