@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.
- package/README.md +122 -150
- package/package.json +1 -1
- package/src/index.ts +129 -129
package/README.md
CHANGED
|
@@ -1,150 +1,122 @@
|
|
|
1
|
-
[](https://npmjs.org/package/knex-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
##
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
+
[](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
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;
|