@bdkinc/knex-ibmi 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. package/README.md +122 -150
  2. package/package.json +1 -1
  3. package/src/index.ts +129 -129
package/README.md CHANGED
@@ -1,150 +1,122 @@
1
- [![npm version](http://img.shields.io/npm/v/knex-db2.svg)](https://npmjs.org/package/knex-db2)
2
- [![Build Status](https://travis-ci.org/henryjw/knex-db2.svg?branch=master)](https://travis-ci.org/henryjw/knex-db2)
3
- [![Known Vulnerabilities](https://snyk.io/test/npm/knex-db2/badge.svg)](https://snyk.io/test/npm/knex-db2)
4
- [![dependencies Status](https://david-dm.org/henryjw/knex-db2/status.svg)](https://david-dm.org/henryjw/knex-db2)
5
- [![devDependencies Status](https://david-dm.org/henryjw/knex-db2/dev-status.svg)](https://david-dm.org/henryjw/knex-db2?type=dev)
6
-
7
- **Disclaimer: this library is in early stages of development. Use at your own risk. Please submit an issue for any bugs encounter or any questions you have.**
8
-
9
- ## Description
10
-
11
- This is an external dialect for [knex](https://github.com/tgriesser/knex). This library is only tested on IBMi. Here are the IBM OSS Docs https://ibmi-oss-docs.readthedocs.io/en/latest/odbc/README.html
12
-
13
- ## Limitations
14
-
15
- Currently this dialect has limited functionality compared to the Knex built-in dialects. Below are some of the limitations:
16
-
17
- - No transaction support
18
- - No streaming support
19
- - Possibly other missing functionality
20
-
21
- ## Supported functionality
22
-
23
- - Query building
24
- - Query execution (see [Limitations](#Limitations))
25
-
26
- ## Installing
27
-
28
- `npm install https://github.com/bdkinc/knex-ibmi`
29
-
30
- Requires Node v14 or higher.
31
-
32
- ## Dependencies
33
-
34
- `npm install odbc` see [ODBC dependencies](#odbc-dependencies) if you run into any issues
35
-
36
- `npm install knex`
37
-
38
- ## Usage
39
- This library is written in typescript and compiled to both commonjs and esm.
40
-
41
- ```javascript
42
- const Knex = require("knex");
43
- const { Db2Dialect } = require("knex-ibmi");
44
-
45
- const knex = Knex({
46
- client: Db2Dialect,
47
- connection: {
48
- host: "localhost",
49
- database: "knextest",
50
- port: 50000,
51
- user: "db2inst1",
52
- password: "db2inst1-pwd",
53
- driver: "IBM i Access ODBC Driver",
54
- connectionStringParams: {
55
- ALLOWPROCCALLS: 1,
56
- CMT: 0,
57
- },
58
- },
59
- pool: {
60
- min: 2,
61
- max: 10,
62
- },
63
- });
64
-
65
- const query = knex.select("*").from("table1").where("x", "y");
66
-
67
- query
68
- .then((result) => console.log(result))
69
- .catch((err) => console.error(err))
70
- .finally(() => process.exit());
71
- ```
72
-
73
- or as ESM
74
-
75
- ```javascript
76
- import knex from "knex";
77
- import { Db2Dialect } from "knex-ibmi";
78
-
79
- const knex = Knex({
80
- client: Db2Dialect,
81
- connection: {
82
- host: "localhost",
83
- database: "knextest",
84
- port: 50000,
85
- user: "db2inst1",
86
- password: "db2inst1-pwd",
87
- driver: "IBM i Access ODBC Driver",
88
- connectionStringParams: {
89
- ALLOWPROCCALLS: 1,
90
- CMT: 0,
91
- },
92
- },
93
- pool: {
94
- min: 2,
95
- max: 10,
96
- },
97
- });
98
-
99
- const query = knex.select("*").from("table1").where("x", "y");
100
-
101
- query
102
- .then((result) => console.log(result))
103
- .catch((err) => console.error(err))
104
- .finally(() => process.exit());
105
- ```
106
-
107
- ## ODBC dependencies
108
-
109
- - make: `sudo apt install make`
110
- - g++: `sudo apt install g++`
111
- - unix odbc: `sudo apt-get install unixodbc unixodbc-dev`
112
-
113
- ## Configuring your driver
114
-
115
- If you don't know the name of your installed driver, then look in look in `odbcinst.ini`. You can find the full path of the file by running `odbcinst -j`.
116
- There you should see an entry like the one below:
117
-
118
- ```
119
- [IBM i Access ODBC Driver 64-bit] <= driver name enclosed in square brackets
120
- Description=IBM i Access for Linux 64-bit ODBC Driver
121
- Driver=/opt/ibm/iaccess/lib64/libcwbodbc.so
122
- Setup=/opt/ibm/iaccess/lib64/libcwbodbcs.so
123
- Threading=0
124
- DontDLClose=1
125
- UsageCount=1
126
- ```
127
-
128
- If that still doesn't work, then unixodbc is probably looking for the config files in the wrong directory. A common case is that the configs are in `/etc` but your system expects them to be somewhere else. In such case, override the path unixodbc looks in via the `ODBCSYSINI` and `ODBCINI` environment variables.
129
- E.g., `ODBCINI=/etc ODBCSYSINI=/etc`.
130
-
131
- ## Installing default driver
132
-
133
- ### Download driver
134
-
135
- https://github.com/ibmdb/node-ibm_db#-download-clidriver-based-on-your-platform--architecture-from-the-below-ibm-hosted-url
136
-
137
- ### Install driver
138
-
139
- - Extract downloaded file. This will create a `clidriver` folder with the driver contents
140
- - Copy this folder to wherever your system keeps drivers. If you're not sure where to put it, just copy it to `/opt/ibm`.
141
- - Add the configuration your `/etc/odbcinst.ini` file. Below is what the contents of the file should look like if your odbc path is `/opt`
142
-
143
- ```
144
- [IBM Cli Driver]
145
- Description=IBM CLI Driver for Linux 64-bit
146
- Driver=/opt/ibm/clidriver/lib/libdb2.soSetup=libdb2.so.1
147
- hreading=0
148
- DontDLClose=1
149
- UsageCount=1
150
- ```
1
+ [![npm version](http://img.shields.io/npm/v/knex-db2.svg)](https://npmjs.org/package/@bdkinc/knex-ibmi)
2
+
3
+ **Disclaimer: this library is in early stages of development. Use at your own risk. Please submit an issue for any bugs encounter or any questions you have.**
4
+
5
+ ## Description
6
+
7
+ This is an external dialect for [knex](https://github.com/tgriesser/knex). This library uses the ODBC driver and is only tested on IBMi. Here are the IBM OSS Docs https://ibmi-oss-docs.readthedocs.io/en/latest/odbc/README.html
8
+
9
+ ## Limitations
10
+
11
+ Currently this dialect has limited functionality compared to the Knex built-in dialects. Below are some of the limitations:
12
+
13
+ - No transaction support
14
+ - No streaming support
15
+ - Possibly other missing functionality
16
+
17
+ ## Supported functionality
18
+
19
+ - Query building
20
+ - Query execution (see [Limitations](#Limitations))
21
+
22
+ ## Installing
23
+
24
+ `npm install @bdkinc/knex-ibmi`
25
+
26
+ Requires Node v14 or higher.
27
+
28
+ ## Dependencies
29
+
30
+ `npm install odbc` see [IBM ODBC Docs for dependencies](https://ibmi-oss-docs.readthedocs.io/en/latest/odbc/README.html) if you run into any issues
31
+
32
+ `npm install knex`
33
+
34
+ ## Usage
35
+
36
+ This library is written in typescript and compiled to both commonjs and esm.
37
+
38
+ ```javascript
39
+ const knex = require("knex");
40
+ const { Db2Dialect } = require("knex-ibmi");
41
+
42
+ const db = knex({
43
+ client: Db2Dialect,
44
+ connection: {
45
+ host: "localhost",
46
+ database: "knextest",
47
+ port: 50000,
48
+ user: "<user>",
49
+ password: "<password>",
50
+ driver: "IBM i Access ODBC Driver",
51
+ connectionStringParams: {
52
+ ALLOWPROCCALLS: 1,
53
+ CMT: 0,
54
+ },
55
+ },
56
+ pool: {
57
+ min: 2,
58
+ max: 10,
59
+ },
60
+ });
61
+
62
+ const query = db.select("*").from("table").where({ foo: "bar" });
63
+
64
+ query
65
+ .then((result) => console.log(result))
66
+ .catch((err) => console.error(err))
67
+ .finally(() => process.exit());
68
+ ```
69
+
70
+ or as ESM
71
+
72
+ ```javascript
73
+ import knex from "knex";
74
+ import { Db2Dialect } from "knex-ibmi";
75
+
76
+ const db = knex({
77
+ client: Db2Dialect,
78
+ connection: {
79
+ host: "localhost",
80
+ database: "knextest",
81
+ port: 50000,
82
+ user: "<user>",
83
+ password: "<password>",
84
+ driver: "IBM i Access ODBC Driver",
85
+ connectionStringParams: {
86
+ ALLOWPROCCALLS: 1,
87
+ CMT: 0,
88
+ },
89
+ },
90
+ pool: {
91
+ min: 2,
92
+ max: 10,
93
+ },
94
+ });
95
+
96
+ try {
97
+ const data = await db.select("*").from("table").where({ foo: "bar" });
98
+ console.log(data);
99
+ } catch (err) {
100
+ throw new Error(err);
101
+ } finally {
102
+ process.exit();
103
+ }
104
+ ```
105
+
106
+ ## Configuring your driver
107
+
108
+ If you don't know the name of your installed driver, then look in look in `odbcinst.ini`. You can find the full path of the file by running `odbcinst -j`.
109
+ There you should see an entry like the one below:
110
+
111
+ ```
112
+ [IBM i Access ODBC Driver 64-bit] <= driver name enclosed in square brackets
113
+ Description=IBM i Access for Linux 64-bit ODBC Driver
114
+ Driver=/opt/ibm/iaccess/lib64/libcwbodbc.so
115
+ Setup=/opt/ibm/iaccess/lib64/libcwbodbcs.so
116
+ Threading=0
117
+ DontDLClose=1
118
+ UsageCount=1
119
+ ```
120
+
121
+ If that still doesn't work, then unixodbc is probably looking for the config files in the wrong directory. A common case is that the configs are in `/etc` but your system expects them to be somewhere else. In such case, override the path unixodbc looks in via the `ODBCSYSINI` and `ODBCINI` environment variables.
122
+ E.g., `ODBCINI=/etc ODBCSYSINI=/etc`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bdkinc/knex-ibmi",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
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,129 +1,129 @@
1
- import * as process from "process";
2
- import { Connection } from "odbc";
3
- import knex from "knex";
4
- import * as odbc from "odbc";
5
- import * as console from "console";
6
-
7
- class DB2Client extends knex.Client {
8
- constructor(config) {
9
- super(config);
10
- this.driverName = "odbc";
11
-
12
- if (this.driverName && config.connection) {
13
- this.initializeDriver();
14
- if (!config.pool || (config.pool && config.pool.max !== 0)) {
15
- this.initializePool(config);
16
- }
17
- }
18
- }
19
-
20
- _driver() {
21
- return odbc;
22
- }
23
-
24
- wrapIdentifierImpl(value: any) {
25
- // override default wrapper ("). we don't want to use it since
26
- // it makes identifiers case-sensitive in DB2
27
- return value;
28
- }
29
-
30
- printDebug(message: string) {
31
- if (process.env.DEBUG === "true") {
32
- // @ts-ignore
33
- this.logger.debug(message);
34
- }
35
- }
36
-
37
- // Get a raw connection, called by the pool manager whenever a new
38
- // connection needs to be added to the pool.
39
- async acquireRawConnection() {
40
- this.printDebug("acquiring raw connection");
41
- const connectionConfig = this.config.connection;
42
- return await this.driver.connect(
43
- this._getConnectionString(connectionConfig),
44
- );
45
- }
46
-
47
- // Used to explicitly close a connection, called internally by the pool manager
48
- // when a connection times out or the pool is shutdown.
49
- async destroyRawConnection(connection: Connection) {
50
- return await connection.close();
51
- }
52
-
53
- _getConnectionString(connectionConfig) {
54
- const connectionStringParams =
55
- connectionConfig.connectionStringParams || {};
56
- const connectionStringExtension = Object.keys(
57
- connectionStringParams,
58
- ).reduce((result, key) => {
59
- const value = connectionStringParams[key];
60
- return `${result}${key}=${value};`;
61
- }, "");
62
-
63
- return `${
64
- `DRIVER=${connectionConfig.driver};SYSTEM=${connectionConfig.host};HOSTNAME=${connectionConfig.host};` +
65
- `PORT=${connectionConfig.port};DATABASE=${connectionConfig.database};` +
66
- `UID=${connectionConfig.user};PWD=${connectionConfig.password};`
67
- }${connectionStringExtension}`;
68
- }
69
-
70
- // Runs the query on the specified connection, providing the bindings
71
- // and any other necessary prep work.
72
- async _query(connection: Connection, obj: any) {
73
- console.log({ obj });
74
- // TODO: verify correctness
75
- if (!obj || typeof obj == "string") obj = { sql: obj };
76
- const method = (
77
- obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]
78
- ).toLowerCase();
79
- obj.sqlMethod = method;
80
-
81
- // Different functions are used since query() doesn't return # of rows affected,
82
- // which is needed for queries that modify the database
83
- if (method === "select" || method === "first" || method === "pluck") {
84
- const rows: any = await connection.query(obj.sql, obj.bindings);
85
- if (rows) {
86
- obj.response = { rows, rowCount: rows.length };
87
- }
88
- return obj;
89
- }
90
-
91
- const statement = await connection.createStatement();
92
- await statement.prepare(obj.sql);
93
- await statement.bind(obj.bindings);
94
- obj.response = await statement.execute();
95
-
96
- return obj;
97
- }
98
-
99
- processResponse(obj: any, runner: any) {
100
- // TODO: verify correctness
101
- if (obj === null) return null;
102
-
103
- const resp = obj.response;
104
- const method = obj.sqlMethod;
105
- const { rows } = resp;
106
-
107
- if (obj.output) return obj.output.call(runner, resp);
108
-
109
- switch (method) {
110
- case "select":
111
- case "pluck":
112
- case "first": {
113
- if (method === "pluck") return rows.map(obj.pluck);
114
- return method === "first" ? rows[0] : rows;
115
- }
116
- case "insert":
117
- case "del":
118
- case "delete":
119
- case "update":
120
- case "counter":
121
- return resp.rowCount;
122
- default:
123
- return resp;
124
- }
125
- }
126
- }
127
-
128
- export const DB2Dialect = DB2Client;
129
- export default DB2Client;
1
+ import * as process from "process";
2
+ import { Connection } from "odbc";
3
+ import knex from "knex";
4
+ import * as odbc from "odbc";
5
+ import * as console from "console";
6
+
7
+ class DB2Client extends knex.Client {
8
+ constructor(config) {
9
+ super(config);
10
+ this.driverName = "odbc";
11
+
12
+ if (this.driverName && config.connection) {
13
+ this.initializeDriver();
14
+ if (!config.pool || (config.pool && config.pool.max !== 0)) {
15
+ this.initializePool(config);
16
+ }
17
+ }
18
+ }
19
+
20
+ _driver() {
21
+ return odbc;
22
+ }
23
+
24
+ wrapIdentifierImpl(value: any) {
25
+ // override default wrapper ("). we don't want to use it since
26
+ // it makes identifiers case-sensitive in DB2
27
+ return value;
28
+ }
29
+
30
+ printDebug(message: string) {
31
+ if (process.env.DEBUG === "true") {
32
+ // @ts-ignore
33
+ this.logger.debug(message);
34
+ }
35
+ }
36
+
37
+ // Get a raw connection, called by the pool manager whenever a new
38
+ // connection needs to be added to the pool.
39
+ async acquireRawConnection() {
40
+ this.printDebug("acquiring raw connection");
41
+ const connectionConfig = this.config.connection;
42
+ return await this.driver.connect(
43
+ this._getConnectionString(connectionConfig),
44
+ );
45
+ }
46
+
47
+ // Used to explicitly close a connection, called internally by the pool manager
48
+ // when a connection times out or the pool is shutdown.
49
+ async destroyRawConnection(connection: Connection) {
50
+ return await connection.close();
51
+ }
52
+
53
+ _getConnectionString(connectionConfig) {
54
+ const connectionStringParams =
55
+ connectionConfig.connectionStringParams || {};
56
+ const connectionStringExtension = Object.keys(
57
+ connectionStringParams,
58
+ ).reduce((result, key) => {
59
+ const value = connectionStringParams[key];
60
+ return `${result}${key}=${value};`;
61
+ }, "");
62
+
63
+ return `${
64
+ `DRIVER=${connectionConfig.driver};SYSTEM=${connectionConfig.host};HOSTNAME=${connectionConfig.host};` +
65
+ `PORT=${connectionConfig.port};DATABASE=${connectionConfig.database};` +
66
+ `UID=${connectionConfig.user};PWD=${connectionConfig.password};`
67
+ }${connectionStringExtension}`;
68
+ }
69
+
70
+ // Runs the query on the specified connection, providing the bindings
71
+ // and any other necessary prep work.
72
+ async _query(connection: Connection, obj: any) {
73
+ console.log({ obj });
74
+ // TODO: verify correctness
75
+ if (!obj || typeof obj == "string") obj = { sql: obj };
76
+ const method = (
77
+ obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]
78
+ ).toLowerCase();
79
+ obj.sqlMethod = method;
80
+
81
+ // Different functions are used since query() doesn't return # of rows affected,
82
+ // which is needed for queries that modify the database
83
+ if (method === "select" || method === "first" || method === "pluck") {
84
+ const rows: any = await connection.query(obj.sql, obj.bindings);
85
+ if (rows) {
86
+ obj.response = { rows, rowCount: rows.length };
87
+ }
88
+ return obj;
89
+ }
90
+
91
+ const statement = await connection.createStatement();
92
+ await statement.prepare(obj.sql);
93
+ await statement.bind(obj.bindings);
94
+ obj.response = await statement.execute();
95
+
96
+ return obj;
97
+ }
98
+
99
+ processResponse(obj: any, runner: any) {
100
+ // TODO: verify correctness
101
+ if (obj === null) return null;
102
+
103
+ const resp = obj.response;
104
+ const method = obj.sqlMethod;
105
+ const { rows } = resp;
106
+
107
+ if (obj.output) return obj.output.call(runner, resp);
108
+
109
+ switch (method) {
110
+ case "select":
111
+ case "pluck":
112
+ case "first": {
113
+ if (method === "pluck") return rows.map(obj.pluck);
114
+ return method === "first" ? rows[0] : rows;
115
+ }
116
+ case "insert":
117
+ case "del":
118
+ case "delete":
119
+ case "update":
120
+ case "counter":
121
+ return resp.rowCount;
122
+ default:
123
+ return resp;
124
+ }
125
+ }
126
+ }
127
+
128
+ export const DB2Dialect = DB2Client;
129
+ export default DB2Client;