@bdkinc/knex-ibmi 0.0.4 → 0.0.6

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/CHANGELOG.md CHANGED
@@ -1,21 +1,14 @@
1
- ## 1.0.0 - 15 Feb 2019
2
- - Bump up to 1.0.0 to follow semver rules
3
- - Internal enhancements
1
+ ## 0.0.5 - July 23rd, 2023
2
+ - add unique and dropUnique methods
4
3
 
5
- ## 0.1.5 - 10 Dec 2018
6
- - Documentation updates
4
+ ## 0.0.4 - July 21st, 2023
5
+ - remove unnecessary compiler methods
7
6
 
8
- ## 0.1.4 - 18 Nov 2018
9
- - Support Node v6
7
+ ## 0.0.3 - July 19th, 2023
8
+ - add transaction support
10
9
 
11
- ## 0.1.3 - 17 Nov 2018
12
- - Don't show debug logs by default #6
10
+ ## 0.0.2 - July 15th, 2023
11
+ - update readme and code examples
13
12
 
14
- ## 0.1.2 - 5 Oct 2018
15
- - Make SQL method case-insensitive (fixes #3)
16
-
17
- ## 0.1.1 - 4 Oct 2018
18
- - Fix raw queries not running as expected #3
19
-
20
- ## 0.1.0 - 29 Sep 2018
21
- - Initial release
13
+ ## 0.0.1 - July 14th, 2023
14
+ - Imported DB2 Dialect and re-wrote
package/README.md CHANGED
@@ -19,6 +19,7 @@ Currently, this dialect has limited functionality compared to the Knex built-in
19
19
  - No streaming support
20
20
  - Possibly other missing functionality
21
21
  - Uses a pool for all connections
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.
22
23
 
23
24
  ## Installing
24
25
 
package/dist/index.js CHANGED
@@ -37,15 +37,11 @@ module.exports = __toCommonJS(src_exports);
37
37
  var process = __toESM(require("process"));
38
38
  var import_knex = __toESM(require("knex"));
39
39
  var odbc = __toESM(require("odbc"));
40
- var console3 = __toESM(require("console"));
40
+ var console = __toESM(require("console"));
41
41
 
42
42
  // src/schema/ibmi-compiler.ts
43
43
  var import_compiler = __toESM(require("knex/lib/schema/compiler"));
44
- var console = __toESM(require("console"));
45
44
  var IBMiSchemaCompiler = class extends import_compiler.default {
46
- constructor(client, builder) {
47
- super(client, builder);
48
- }
49
45
  hasTable(tableName) {
50
46
  const formattedTable = this.client.parameter(
51
47
  // @ts-ignore
@@ -55,7 +51,7 @@ var IBMiSchemaCompiler = class extends import_compiler.default {
55
51
  // @ts-ignore
56
52
  this.bindingsHolder
57
53
  );
58
- const bindings = [tableName.toUpperCase()];
54
+ const bindings = [tableName];
59
55
  let sql = `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TYPE = 'T' AND TABLE_NAME = ${formattedTable}`;
60
56
  if (this.schema) {
61
57
  sql += " AND TABLE_SCHEMA = ?";
@@ -73,7 +69,6 @@ var IBMiSchemaCompiler = class extends import_compiler.default {
73
69
  const sequence = this.builder._sequence;
74
70
  for (let i = 0, l = sequence.length; i < l; i++) {
75
71
  const query = sequence[i];
76
- console.log(query.method, query);
77
72
  this[query.method].apply(this, query.args);
78
73
  }
79
74
  return this.sequence;
@@ -86,6 +81,7 @@ var ibmi_compiler_default = IBMiSchemaCompiler;
86
81
 
87
82
  // src/schema/ibmi-tablecompiler.ts
88
83
  var import_tablecompiler = __toESM(require("knex/lib/schema/tablecompiler"));
84
+ var import_isObject = __toESM(require("lodash/isObject"));
89
85
  var IBMiTableCompiler = class extends import_tablecompiler.default {
90
86
  createQuery(columns, ifNot, like) {
91
87
  let createStatement = ifNot ? `if object_id('${this.tableName()}', 'U') is null ` : "";
@@ -106,6 +102,29 @@ var IBMiTableCompiler = class extends import_tablecompiler.default {
106
102
  this.addColumns(columns, this.addColumnsPrefix);
107
103
  }
108
104
  }
105
+ dropUnique(columns, indexName) {
106
+ indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand("unique", this.tableNameRaw, columns);
107
+ this.pushQuery(`drop index ${indexName}`);
108
+ }
109
+ unique(columns, indexName) {
110
+ let deferrable;
111
+ let predicate;
112
+ if ((0, import_isObject.default)(indexName)) {
113
+ ({ indexName, deferrable, predicate } = indexName);
114
+ }
115
+ if (deferrable && deferrable !== "not deferrable") {
116
+ this.client.logger.warn(
117
+ `IBMi: unique index \`${indexName}\` will not be deferrable ${deferrable}.`
118
+ );
119
+ }
120
+ indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand("unique", this.tableNameRaw, columns);
121
+ columns = this.formatter.columnize(columns);
122
+ const predicateQuery = predicate ? " " + this.client.queryCompiler(predicate).where() : "";
123
+ this.pushQuery(
124
+ // @ts-ignore
125
+ `CREATE UNIQUE INDEX ${indexName} ON ${this.tableName()} (${columns})${predicateQuery}`
126
+ );
127
+ }
109
128
  // All of the columns to "add" for the query
110
129
  addColumns(columns, prefix) {
111
130
  prefix = prefix || this.addColumnsPrefix;
@@ -141,7 +160,6 @@ var ibmi_columncompiler_default = IBMiColumnCompiler;
141
160
 
142
161
  // src/execution/ibmi-transaction.ts
143
162
  var import_transaction = __toESM(require("knex/lib/execution/transaction"));
144
- var console2 = __toESM(require("console"));
145
163
  var IBMiTransaction = class extends import_transaction.default {
146
164
  async begin(conn) {
147
165
  const connection = await conn.connect();
@@ -149,7 +167,6 @@ var IBMiTransaction = class extends import_transaction.default {
149
167
  return connection;
150
168
  }
151
169
  async rollback(conn) {
152
- console2.log({ conn });
153
170
  const connection = await conn.connect();
154
171
  await connection.rollback();
155
172
  return connection;
@@ -163,12 +180,12 @@ var ibmi_transaction_default = IBMiTransaction;
163
180
 
164
181
  // src/query/ibmi-querycompiler.ts
165
182
  var import_querycompiler = __toESM(require("knex/lib/query/querycompiler"));
166
- var import_isObject = __toESM(require("lodash/isObject"));
183
+ var import_isObject2 = __toESM(require("lodash/isObject"));
167
184
  var import_wrappingFormatter = require("knex/lib/formatter/wrappingFormatter");
168
185
  var import_date_fns = require("date-fns");
169
186
  var IBMiQueryCompiler = class extends import_querycompiler.default {
170
187
  _prepInsert(data) {
171
- if ((0, import_isObject.default)(data)) {
188
+ if ((0, import_isObject2.default)(data)) {
172
189
  if (data.hasOwnProperty("migration_time")) {
173
190
  const parsed = new Date(data.migration_time);
174
191
  data.migration_time = (0, import_date_fns.format)(parsed, "yyyy-MM-dd HH:mm:ss");
@@ -256,12 +273,6 @@ var DB2Client = class extends import_knex.default.Client {
256
273
  _driver() {
257
274
  return odbc;
258
275
  }
259
- wrapIdentifierImpl(value) {
260
- if (value.includes("knex_migrations")) {
261
- return value.toUpperCase();
262
- }
263
- return value;
264
- }
265
276
  printDebug(message) {
266
277
  if (process.env.DEBUG === "true") {
267
278
  this.logger.debug(message);
@@ -272,13 +283,13 @@ var DB2Client = class extends import_knex.default.Client {
272
283
  async acquireRawConnection() {
273
284
  this.printDebug("acquiring raw connection");
274
285
  const connectionConfig = this.config.connection;
275
- console3.log(this._getConnectionString(connectionConfig));
286
+ console.log(this._getConnectionString(connectionConfig));
276
287
  return await this.driver.pool(this._getConnectionString(connectionConfig));
277
288
  }
278
289
  // Used to explicitly close a connection, called internally by the pool manager
279
290
  // when a connection times out or the pool is shutdown.
280
291
  async destroyRawConnection(connection) {
281
- console3.log("destroy connection");
292
+ console.log("destroy connection");
282
293
  return await connection.close();
283
294
  }
284
295
  _getConnectionString(connectionConfig) {
@@ -314,11 +325,10 @@ var DB2Client = class extends import_knex.default.Client {
314
325
  const result = await statement.execute();
315
326
  obj.response = { rows: [result.count], rowCount: result.count };
316
327
  } catch (err) {
317
- console3.error(err);
328
+ console.error(err);
318
329
  throw new Error(err);
319
330
  }
320
331
  }
321
- console3.log({ obj });
322
332
  return obj;
323
333
  }
324
334
  transaction(container, config, outerTx) {
@@ -346,13 +356,13 @@ var DB2Client = class extends import_knex.default.Client {
346
356
  return obj.output.call(runner, resp);
347
357
  switch (method) {
348
358
  case "select":
359
+ return rows;
349
360
  case "pluck":
350
- case "first": {
351
- if (method === "pluck")
352
- return rows.map(obj.pluck);
353
- return method === "first" ? rows[0] : rows;
354
- }
361
+ return rows.map(obj.pluck);
362
+ case "first":
363
+ return rows[0];
355
364
  case "insert":
365
+ return rows;
356
366
  case "del":
357
367
  case "delete":
358
368
  case "update":
package/dist/index.mjs CHANGED
@@ -2,15 +2,11 @@
2
2
  import * as process from "process";
3
3
  import knex from "knex";
4
4
  import * as odbc from "odbc";
5
- import * as console3 from "console";
5
+ import * as console from "console";
6
6
 
7
7
  // src/schema/ibmi-compiler.ts
8
8
  import SchemaCompiler from "knex/lib/schema/compiler";
9
- import * as console from "console";
10
9
  var IBMiSchemaCompiler = class extends SchemaCompiler {
11
- constructor(client, builder) {
12
- super(client, builder);
13
- }
14
10
  hasTable(tableName) {
15
11
  const formattedTable = this.client.parameter(
16
12
  // @ts-ignore
@@ -20,7 +16,7 @@ var IBMiSchemaCompiler = class extends SchemaCompiler {
20
16
  // @ts-ignore
21
17
  this.bindingsHolder
22
18
  );
23
- const bindings = [tableName.toUpperCase()];
19
+ const bindings = [tableName];
24
20
  let sql = `SELECT TABLE_NAME FROM QSYS2.SYSTABLES WHERE TYPE = 'T' AND TABLE_NAME = ${formattedTable}`;
25
21
  if (this.schema) {
26
22
  sql += " AND TABLE_SCHEMA = ?";
@@ -38,7 +34,6 @@ var IBMiSchemaCompiler = class extends SchemaCompiler {
38
34
  const sequence = this.builder._sequence;
39
35
  for (let i = 0, l = sequence.length; i < l; i++) {
40
36
  const query = sequence[i];
41
- console.log(query.method, query);
42
37
  this[query.method].apply(this, query.args);
43
38
  }
44
39
  return this.sequence;
@@ -51,6 +46,7 @@ var ibmi_compiler_default = IBMiSchemaCompiler;
51
46
 
52
47
  // src/schema/ibmi-tablecompiler.ts
53
48
  import TableCompiler from "knex/lib/schema/tablecompiler";
49
+ import isObject from "lodash/isObject";
54
50
  var IBMiTableCompiler = class extends TableCompiler {
55
51
  createQuery(columns, ifNot, like) {
56
52
  let createStatement = ifNot ? `if object_id('${this.tableName()}', 'U') is null ` : "";
@@ -71,6 +67,29 @@ var IBMiTableCompiler = class extends TableCompiler {
71
67
  this.addColumns(columns, this.addColumnsPrefix);
72
68
  }
73
69
  }
70
+ dropUnique(columns, indexName) {
71
+ indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand("unique", this.tableNameRaw, columns);
72
+ this.pushQuery(`drop index ${indexName}`);
73
+ }
74
+ unique(columns, indexName) {
75
+ let deferrable;
76
+ let predicate;
77
+ if (isObject(indexName)) {
78
+ ({ indexName, deferrable, predicate } = indexName);
79
+ }
80
+ if (deferrable && deferrable !== "not deferrable") {
81
+ this.client.logger.warn(
82
+ `IBMi: unique index \`${indexName}\` will not be deferrable ${deferrable}.`
83
+ );
84
+ }
85
+ indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand("unique", this.tableNameRaw, columns);
86
+ columns = this.formatter.columnize(columns);
87
+ const predicateQuery = predicate ? " " + this.client.queryCompiler(predicate).where() : "";
88
+ this.pushQuery(
89
+ // @ts-ignore
90
+ `CREATE UNIQUE INDEX ${indexName} ON ${this.tableName()} (${columns})${predicateQuery}`
91
+ );
92
+ }
74
93
  // All of the columns to "add" for the query
75
94
  addColumns(columns, prefix) {
76
95
  prefix = prefix || this.addColumnsPrefix;
@@ -106,7 +125,6 @@ var ibmi_columncompiler_default = IBMiColumnCompiler;
106
125
 
107
126
  // src/execution/ibmi-transaction.ts
108
127
  import Transaction from "knex/lib/execution/transaction";
109
- import * as console2 from "console";
110
128
  var IBMiTransaction = class extends Transaction {
111
129
  async begin(conn) {
112
130
  const connection = await conn.connect();
@@ -114,7 +132,6 @@ var IBMiTransaction = class extends Transaction {
114
132
  return connection;
115
133
  }
116
134
  async rollback(conn) {
117
- console2.log({ conn });
118
135
  const connection = await conn.connect();
119
136
  await connection.rollback();
120
137
  return connection;
@@ -128,12 +145,12 @@ var ibmi_transaction_default = IBMiTransaction;
128
145
 
129
146
  // src/query/ibmi-querycompiler.ts
130
147
  import QueryCompiler from "knex/lib/query/querycompiler";
131
- import isObject from "lodash/isObject";
148
+ import isObject2 from "lodash/isObject";
132
149
  import { rawOrFn as rawOrFn_ } from "knex/lib/formatter/wrappingFormatter";
133
150
  import { format } from "date-fns";
134
151
  var IBMiQueryCompiler = class extends QueryCompiler {
135
152
  _prepInsert(data) {
136
- if (isObject(data)) {
153
+ if (isObject2(data)) {
137
154
  if (data.hasOwnProperty("migration_time")) {
138
155
  const parsed = new Date(data.migration_time);
139
156
  data.migration_time = format(parsed, "yyyy-MM-dd HH:mm:ss");
@@ -221,12 +238,6 @@ var DB2Client = class extends knex.Client {
221
238
  _driver() {
222
239
  return odbc;
223
240
  }
224
- wrapIdentifierImpl(value) {
225
- if (value.includes("knex_migrations")) {
226
- return value.toUpperCase();
227
- }
228
- return value;
229
- }
230
241
  printDebug(message) {
231
242
  if (process.env.DEBUG === "true") {
232
243
  this.logger.debug(message);
@@ -237,13 +248,13 @@ var DB2Client = class extends knex.Client {
237
248
  async acquireRawConnection() {
238
249
  this.printDebug("acquiring raw connection");
239
250
  const connectionConfig = this.config.connection;
240
- console3.log(this._getConnectionString(connectionConfig));
251
+ console.log(this._getConnectionString(connectionConfig));
241
252
  return await this.driver.pool(this._getConnectionString(connectionConfig));
242
253
  }
243
254
  // Used to explicitly close a connection, called internally by the pool manager
244
255
  // when a connection times out or the pool is shutdown.
245
256
  async destroyRawConnection(connection) {
246
- console3.log("destroy connection");
257
+ console.log("destroy connection");
247
258
  return await connection.close();
248
259
  }
249
260
  _getConnectionString(connectionConfig) {
@@ -279,11 +290,10 @@ var DB2Client = class extends knex.Client {
279
290
  const result = await statement.execute();
280
291
  obj.response = { rows: [result.count], rowCount: result.count };
281
292
  } catch (err) {
282
- console3.error(err);
293
+ console.error(err);
283
294
  throw new Error(err);
284
295
  }
285
296
  }
286
- console3.log({ obj });
287
297
  return obj;
288
298
  }
289
299
  transaction(container, config, outerTx) {
@@ -311,13 +321,13 @@ var DB2Client = class extends knex.Client {
311
321
  return obj.output.call(runner, resp);
312
322
  switch (method) {
313
323
  case "select":
324
+ return rows;
314
325
  case "pluck":
315
- case "first": {
316
- if (method === "pluck")
317
- return rows.map(obj.pluck);
318
- return method === "first" ? rows[0] : rows;
319
- }
326
+ return rows.map(obj.pluck);
327
+ case "first":
328
+ return rows[0];
320
329
  case "insert":
330
+ return rows;
321
331
  case "del":
322
332
  case "delete":
323
333
  case "update":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bdkinc/knex-ibmi",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "Knex dialect for IBMi",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -1,5 +1,4 @@
1
1
  import Transaction from "knex/lib/execution/transaction";
2
- import * as console from "console";
3
2
 
4
3
  class IBMiTransaction extends Transaction {
5
4
  async begin(conn) {
@@ -9,7 +8,6 @@ class IBMiTransaction extends Transaction {
9
8
  }
10
9
 
11
10
  async rollback(conn) {
12
- console.log({ conn });
13
11
  const connection = await conn.connect();
14
12
  await connection.rollback();
15
13
  return connection;
package/src/index.ts CHANGED
@@ -12,7 +12,6 @@ import QueryCompiler from "./query/ibmi-querycompiler";
12
12
  class DB2Client extends knex.Client {
13
13
  constructor(config) {
14
14
  super(config);
15
-
16
15
  this.driverName = "odbc";
17
16
 
18
17
  if (this.dialect && !this.config.client) {
@@ -49,15 +48,6 @@ class DB2Client extends knex.Client {
49
48
  return odbc;
50
49
  }
51
50
 
52
- wrapIdentifierImpl(value: any) {
53
- // override default wrapper ("). we don't want to use it since
54
- // it makes identifiers case-sensitive in DB2
55
- if (value.includes("knex_migrations")) {
56
- return value.toUpperCase();
57
- }
58
- return value;
59
- }
60
-
61
51
  printDebug(message: string) {
62
52
  if (process.env.DEBUG === "true") {
63
53
  // @ts-ignore
@@ -134,7 +124,6 @@ class DB2Client extends knex.Client {
134
124
  throw new Error(err);
135
125
  }
136
126
  }
137
- console.log({ obj });
138
127
 
139
128
  return obj;
140
129
  }
@@ -165,7 +154,6 @@ class DB2Client extends knex.Client {
165
154
  }
166
155
 
167
156
  processResponse(obj: any, runner: any) {
168
- // TODO: verify correctness
169
157
  if (obj === null) return null;
170
158
 
171
159
  const resp = obj.response;
@@ -176,12 +164,13 @@ class DB2Client extends knex.Client {
176
164
 
177
165
  switch (method) {
178
166
  case "select":
167
+ return rows;
179
168
  case "pluck":
180
- case "first": {
181
- if (method === "pluck") return rows.map(obj.pluck);
182
- return method === "first" ? rows[0] : rows;
183
- }
169
+ return rows.map(obj.pluck);
170
+ case "first":
171
+ return rows[0];
184
172
  case "insert":
173
+ return rows;
185
174
  case "del":
186
175
  case "delete":
187
176
  case "update":
@@ -2,10 +2,6 @@ import SchemaCompiler from "knex/lib/schema/compiler";
2
2
  import * as console from "console";
3
3
 
4
4
  class IBMiSchemaCompiler extends SchemaCompiler {
5
- constructor(client, builder) {
6
- super(client, builder);
7
- }
8
-
9
5
  hasTable(tableName) {
10
6
  // @ts-ignore
11
7
  const formattedTable = this.client.parameter(
@@ -16,7 +12,7 @@ class IBMiSchemaCompiler extends SchemaCompiler {
16
12
  // @ts-ignore
17
13
  this.bindingsHolder,
18
14
  );
19
- const bindings = [tableName.toUpperCase()];
15
+ const bindings = [tableName];
20
16
  let sql =
21
17
  `SELECT TABLE_NAME FROM QSYS2.SYSTABLES ` +
22
18
  `WHERE TYPE = 'T' AND TABLE_NAME = ${formattedTable}`;
@@ -42,7 +38,6 @@ class IBMiSchemaCompiler extends SchemaCompiler {
42
38
  const sequence = this.builder._sequence;
43
39
  for (let i = 0, l = sequence.length; i < l; i++) {
44
40
  const query = sequence[i];
45
- console.log(query.method, query);
46
41
  this[query.method].apply(this, query.args);
47
42
  }
48
43
  // @ts-ignore
@@ -1,4 +1,5 @@
1
1
  import TableCompiler from "knex/lib/schema/tablecompiler";
2
+ import isObject from "lodash/isObject";
2
3
 
3
4
  class IBMiTableCompiler extends TableCompiler {
4
5
  createQuery(columns, ifNot, like) {
@@ -39,6 +40,48 @@ class IBMiTableCompiler extends TableCompiler {
39
40
  }
40
41
  }
41
42
 
43
+ dropUnique(columns, indexName) {
44
+ indexName = indexName
45
+ // @ts-ignore
46
+ ? this.formatter.wrap(indexName)
47
+ // @ts-ignore
48
+ : this._indexCommand('unique', this.tableNameRaw, columns);
49
+ // @ts-ignore
50
+ this.pushQuery(`drop index ${indexName}`);
51
+ }
52
+
53
+ unique(columns, indexName) {
54
+ let deferrable;
55
+ let predicate;
56
+ if (isObject(indexName)) {
57
+ ({ indexName, deferrable, predicate } = indexName);
58
+ }
59
+ if (deferrable && deferrable !== 'not deferrable') {
60
+ // @ts-ignore
61
+ this.client.logger.warn(
62
+ `IBMi: unique index \`${indexName}\` will not be deferrable ${deferrable}.`
63
+ );
64
+ }
65
+ indexName = indexName
66
+ // @ts-ignore
67
+ ? this.formatter.wrap(indexName)
68
+ // @ts-ignore
69
+ : this._indexCommand('unique', this.tableNameRaw, columns);
70
+ // @ts-ignore
71
+ columns = this.formatter.columnize(columns);
72
+
73
+ const predicateQuery = predicate
74
+ // @ts-ignore
75
+ ? ' ' + this.client.queryCompiler(predicate).where()
76
+ : '';
77
+
78
+ // @ts-ignore
79
+ this.pushQuery(
80
+ // @ts-ignore
81
+ `CREATE UNIQUE INDEX ${indexName} ON ${this.tableName()} (${columns})${predicateQuery}`
82
+ );
83
+ }
84
+
42
85
  // All of the columns to "add" for the query
43
86
  addColumns(columns, prefix) {
44
87
  // @ts-ignore