@bdkinc/knex-ibmi 0.0.1
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 +21 -0
- package/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/index.js +138 -0
- package/dist/index.mjs +103 -0
- package/package.json +64 -0
- package/src/index.ts +129 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
## 1.0.0 - 15 Feb 2019
|
|
2
|
+
- Bump up to 1.0.0 to follow semver rules
|
|
3
|
+
- Internal enhancements
|
|
4
|
+
|
|
5
|
+
## 0.1.5 - 10 Dec 2018
|
|
6
|
+
- Documentation updates
|
|
7
|
+
|
|
8
|
+
## 0.1.4 - 18 Nov 2018
|
|
9
|
+
- Support Node v6
|
|
10
|
+
|
|
11
|
+
## 0.1.3 - 17 Nov 2018
|
|
12
|
+
- Don't show debug logs by default #6
|
|
13
|
+
|
|
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
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Henry Williams
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
[](https://npmjs.org/package/knex-db2)
|
|
2
|
+
[](https://travis-ci.org/henryjw/knex-db2)
|
|
3
|
+
[](https://snyk.io/test/npm/knex-db2)
|
|
4
|
+
[](https://david-dm.org/henryjw/knex-db2)
|
|
5
|
+
[](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
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
DB2Dialect: () => DB2Dialect,
|
|
34
|
+
default: () => src_default
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
var process = __toESM(require("process"));
|
|
38
|
+
var import_knex = __toESM(require("knex"));
|
|
39
|
+
var odbc = __toESM(require("odbc"));
|
|
40
|
+
var DB2Client = class extends import_knex.default.Client {
|
|
41
|
+
constructor(config = {}) {
|
|
42
|
+
super(config);
|
|
43
|
+
this.driverName = "odbc";
|
|
44
|
+
if (this.driverName && config.connection) {
|
|
45
|
+
this.initializeDriver();
|
|
46
|
+
if (!config.pool || config.pool && config.pool.max !== 0) {
|
|
47
|
+
this.initializePool(config);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
_driver() {
|
|
52
|
+
return odbc;
|
|
53
|
+
}
|
|
54
|
+
wrapIdentifierImpl(value) {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
printDebug(message) {
|
|
58
|
+
if (process.env.DEBUG === "true") {
|
|
59
|
+
this.logger.debug(message);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
// Get a raw connection, called by the pool manager whenever a new
|
|
63
|
+
// connection needs to be added to the pool.
|
|
64
|
+
async acquireRawConnection() {
|
|
65
|
+
this.printDebug("acquiring raw connection");
|
|
66
|
+
const connectionConfig = this.config.connection;
|
|
67
|
+
return await this.driver.connect(
|
|
68
|
+
this._getConnectionString(connectionConfig)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
// Used to explicitly close a connection, called internally by the pool manager
|
|
72
|
+
// when a connection times out or the pool is shutdown.
|
|
73
|
+
async destroyRawConnection(connection) {
|
|
74
|
+
return await connection.close();
|
|
75
|
+
}
|
|
76
|
+
_getConnectionString(connectionConfig = {}) {
|
|
77
|
+
const connectionStringParams = connectionConfig.connectionStringParams || {};
|
|
78
|
+
const connectionStringExtension = Object.keys(
|
|
79
|
+
connectionStringParams
|
|
80
|
+
).reduce((result, key) => {
|
|
81
|
+
const value = connectionStringParams[key];
|
|
82
|
+
return `${result}${key}=${value};`;
|
|
83
|
+
}, "");
|
|
84
|
+
return `${`DRIVER=${connectionConfig.driver};SYSTEM=${connectionConfig.host};HOSTNAME=${connectionConfig.host};PORT=${connectionConfig.port};DATABASE=${connectionConfig.database};UID=${connectionConfig.user};PWD=${connectionConfig.password};`}${connectionStringExtension}`;
|
|
85
|
+
}
|
|
86
|
+
// Runs the query on the specified connection, providing the bindings
|
|
87
|
+
// and any other necessary prep work.
|
|
88
|
+
async _query(connection, obj) {
|
|
89
|
+
if (!obj || typeof obj == "string")
|
|
90
|
+
obj = { sql: obj };
|
|
91
|
+
const method = (obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]).toLowerCase();
|
|
92
|
+
obj.sqlMethod = method;
|
|
93
|
+
if (method === "select" || method === "first" || method === "pluck") {
|
|
94
|
+
const rows = await connection.query(obj.sql, obj.bindings);
|
|
95
|
+
if (rows) {
|
|
96
|
+
obj.response = { rows, rowCount: rows.length };
|
|
97
|
+
}
|
|
98
|
+
return obj;
|
|
99
|
+
}
|
|
100
|
+
const statement = await connection.createStatement();
|
|
101
|
+
await statement.prepare(obj.sql);
|
|
102
|
+
await statement.bind(obj.bindings);
|
|
103
|
+
obj.response = await statement.execute();
|
|
104
|
+
return obj;
|
|
105
|
+
}
|
|
106
|
+
processResponse(obj, runner) {
|
|
107
|
+
if (obj === null)
|
|
108
|
+
return null;
|
|
109
|
+
const resp = obj.response;
|
|
110
|
+
const method = obj.sqlMethod;
|
|
111
|
+
const { rows } = resp;
|
|
112
|
+
if (obj.output)
|
|
113
|
+
return obj.output.call(runner, resp);
|
|
114
|
+
switch (method) {
|
|
115
|
+
case "select":
|
|
116
|
+
case "pluck":
|
|
117
|
+
case "first": {
|
|
118
|
+
if (method === "pluck")
|
|
119
|
+
return rows.map(obj.pluck);
|
|
120
|
+
return method === "first" ? rows[0] : rows;
|
|
121
|
+
}
|
|
122
|
+
case "insert":
|
|
123
|
+
case "del":
|
|
124
|
+
case "delete":
|
|
125
|
+
case "update":
|
|
126
|
+
case "counter":
|
|
127
|
+
return resp.rowCount;
|
|
128
|
+
default:
|
|
129
|
+
return resp;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var DB2Dialect = DB2Client;
|
|
134
|
+
var src_default = DB2Client;
|
|
135
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
136
|
+
0 && (module.exports = {
|
|
137
|
+
DB2Dialect
|
|
138
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import * as process from "process";
|
|
3
|
+
import knex from "knex";
|
|
4
|
+
import * as odbc from "odbc";
|
|
5
|
+
var DB2Client = class extends knex.Client {
|
|
6
|
+
constructor(config = {}) {
|
|
7
|
+
super(config);
|
|
8
|
+
this.driverName = "odbc";
|
|
9
|
+
if (this.driverName && config.connection) {
|
|
10
|
+
this.initializeDriver();
|
|
11
|
+
if (!config.pool || config.pool && config.pool.max !== 0) {
|
|
12
|
+
this.initializePool(config);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
_driver() {
|
|
17
|
+
return odbc;
|
|
18
|
+
}
|
|
19
|
+
wrapIdentifierImpl(value) {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
printDebug(message) {
|
|
23
|
+
if (process.env.DEBUG === "true") {
|
|
24
|
+
this.logger.debug(message);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Get a raw connection, called by the pool manager whenever a new
|
|
28
|
+
// connection needs to be added to the pool.
|
|
29
|
+
async acquireRawConnection() {
|
|
30
|
+
this.printDebug("acquiring raw connection");
|
|
31
|
+
const connectionConfig = this.config.connection;
|
|
32
|
+
return await this.driver.connect(
|
|
33
|
+
this._getConnectionString(connectionConfig)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
// Used to explicitly close a connection, called internally by the pool manager
|
|
37
|
+
// when a connection times out or the pool is shutdown.
|
|
38
|
+
async destroyRawConnection(connection) {
|
|
39
|
+
return await connection.close();
|
|
40
|
+
}
|
|
41
|
+
_getConnectionString(connectionConfig = {}) {
|
|
42
|
+
const connectionStringParams = connectionConfig.connectionStringParams || {};
|
|
43
|
+
const connectionStringExtension = Object.keys(
|
|
44
|
+
connectionStringParams
|
|
45
|
+
).reduce((result, key) => {
|
|
46
|
+
const value = connectionStringParams[key];
|
|
47
|
+
return `${result}${key}=${value};`;
|
|
48
|
+
}, "");
|
|
49
|
+
return `${`DRIVER=${connectionConfig.driver};SYSTEM=${connectionConfig.host};HOSTNAME=${connectionConfig.host};PORT=${connectionConfig.port};DATABASE=${connectionConfig.database};UID=${connectionConfig.user};PWD=${connectionConfig.password};`}${connectionStringExtension}`;
|
|
50
|
+
}
|
|
51
|
+
// Runs the query on the specified connection, providing the bindings
|
|
52
|
+
// and any other necessary prep work.
|
|
53
|
+
async _query(connection, obj) {
|
|
54
|
+
if (!obj || typeof obj == "string")
|
|
55
|
+
obj = { sql: obj };
|
|
56
|
+
const method = (obj.method !== "raw" ? obj.method : obj.sql.split(" ")[0]).toLowerCase();
|
|
57
|
+
obj.sqlMethod = method;
|
|
58
|
+
if (method === "select" || method === "first" || method === "pluck") {
|
|
59
|
+
const rows = await connection.query(obj.sql, obj.bindings);
|
|
60
|
+
if (rows) {
|
|
61
|
+
obj.response = { rows, rowCount: rows.length };
|
|
62
|
+
}
|
|
63
|
+
return obj;
|
|
64
|
+
}
|
|
65
|
+
const statement = await connection.createStatement();
|
|
66
|
+
await statement.prepare(obj.sql);
|
|
67
|
+
await statement.bind(obj.bindings);
|
|
68
|
+
obj.response = await statement.execute();
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
71
|
+
processResponse(obj, runner) {
|
|
72
|
+
if (obj === null)
|
|
73
|
+
return null;
|
|
74
|
+
const resp = obj.response;
|
|
75
|
+
const method = obj.sqlMethod;
|
|
76
|
+
const { rows } = resp;
|
|
77
|
+
if (obj.output)
|
|
78
|
+
return obj.output.call(runner, resp);
|
|
79
|
+
switch (method) {
|
|
80
|
+
case "select":
|
|
81
|
+
case "pluck":
|
|
82
|
+
case "first": {
|
|
83
|
+
if (method === "pluck")
|
|
84
|
+
return rows.map(obj.pluck);
|
|
85
|
+
return method === "first" ? rows[0] : rows;
|
|
86
|
+
}
|
|
87
|
+
case "insert":
|
|
88
|
+
case "del":
|
|
89
|
+
case "delete":
|
|
90
|
+
case "update":
|
|
91
|
+
case "counter":
|
|
92
|
+
return resp.rowCount;
|
|
93
|
+
default:
|
|
94
|
+
return resp;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var DB2Dialect = DB2Client;
|
|
99
|
+
var src_default = DB2Client;
|
|
100
|
+
export {
|
|
101
|
+
DB2Dialect,
|
|
102
|
+
src_default as default
|
|
103
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bdkinc/knex-ibmi",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Knex dialect for IBMi",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./dist/index.mjs",
|
|
9
|
+
"require": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=14"
|
|
13
|
+
},
|
|
14
|
+
"engineStrict": true,
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsup src/index.ts --format esm,cjs",
|
|
17
|
+
"lint:scripts": "eslint . --ext .ts",
|
|
18
|
+
"format:scripts": "prettier . --write"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"knex",
|
|
22
|
+
"dialect",
|
|
23
|
+
"querybuilder",
|
|
24
|
+
"db2",
|
|
25
|
+
"ibm",
|
|
26
|
+
"ibmi",
|
|
27
|
+
"odbc"
|
|
28
|
+
],
|
|
29
|
+
"author": {
|
|
30
|
+
"name": "BDKinc",
|
|
31
|
+
"web": "https://github.com/bdkinc"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git://github.com/bdkinc/knex-ibmi.git"
|
|
36
|
+
},
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@faker-js/faker": "^8.0.2",
|
|
40
|
+
"@types/knex": "^0.16.1",
|
|
41
|
+
"@typescript-eslint/eslint-plugin": "^5.61.0",
|
|
42
|
+
"@typescript-eslint/parser": "^5.61.0",
|
|
43
|
+
"debug": "^4.3.4",
|
|
44
|
+
"eslint": "^8.44.0",
|
|
45
|
+
"eslint-config-prettier": "^8.8.0",
|
|
46
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
47
|
+
"prettier": "^3.0.0",
|
|
48
|
+
"ts-node": "^10.9.1",
|
|
49
|
+
"tslib": "^2.6.0",
|
|
50
|
+
"tsup": "^7.1.0",
|
|
51
|
+
"typescript": "^5.1.6"
|
|
52
|
+
},
|
|
53
|
+
"files": [
|
|
54
|
+
"src",
|
|
55
|
+
"dist",
|
|
56
|
+
"CHANGELOG.md",
|
|
57
|
+
"README.md",
|
|
58
|
+
"LICENSE"
|
|
59
|
+
],
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"knex": "^2",
|
|
62
|
+
"odbc": "^2.4.8"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +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;
|