如何将KnexJS与数据库Oracle连接?

时间:2017-01-17 19:53:13

标签: node.js oracle knex.js

我有连接,但我不知道它是否存在连接键 odbc ...但即使我扔掉它,下面通知的错误仍然存​​在

const knex = require('knex');

// connection database
const dbmdlog = knex({
  client: 'oracle',
  connection: {
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'mydb',
    odbc: 'MYDB'
  }
});

module.exports = dbmdlogp;

我在collun中做了一个简单的选择 但总是返回错误:

  Unhandled rejection TypeError: _this2.driver.connect is not a function
        at /var/www/html/myapp/node_modules/knex/lib/dialects/oracle/index.js:143:21
        at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)
        at Promise._resolveFromExecutor (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:481:18)
        at new Promise (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:77:14)
        at Client_Oracle.acquireRawConnection (/var/www/html/myapp/node_modules/knex/lib/dialects/oracle/index.js:142:12)
        at Object.create (/var/www/html/myapp/node_modules/knex/lib/client.js:231:16)
        at Pool._createResource (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:326:17)
        at Pool.dispense [as _dispense] (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:314:12)
        at Pool.acquire (/var/www/html/myapp/node_modules/generic-pool/lib/generic-pool.js:392:8)
        at /var/www/html/myapp/node_modules/knex/lib/client.js:281:19
        at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)
        at Promise._resolveFromExecutor (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:481:18)
        at new Promise (/var/www/html/myapp/node_modules/bluebird/js/release/promise.js:77:14)
        at Client_Oracle.acquireConnection (/var/www/html/myapp/node_modules/knex/lib/client.js:272:12)

        at /var/www/html/myapp/node_modules/knex/lib/runner.js:200:30
     at Promise._execute (/var/www/html/myapp/node_modules/bluebird/js/release/debuggability.js:299:9)

2 个答案:

答案 0 :(得分:3)

knex和OracleDB的组合工作正常。以下是package.json中的包:"knex": "0.13.0", "oracledb": "1.13.1"

和knex打电话:

var conn = knex({
  client: 'oracledb',
  connection: {
    host: config.oracle.host,
    user: config.oracle.user,
    password: config.oracle.password,
    database: config.oracle.database,
  }
});

答案 1 :(得分:1)

以防有人需要传递连接字符串。但是请注意,连接字符串中的详细信息需要与您自己的环境相匹配。

const oracledb = require("oracledb");
oracledb.initOracleClient({ libDir: "C:\\oracle\\instantclient_12_1" });

const knex = require("knex")({
    client: "oracledb",
    connection: {
        user: "YOUR_USER",
        password: "YOUR_PASSWORD",
        connectString: "(DESCRIPTION=(CONNECT_TIMEOUT=10)(RETRY_COUNT=3)(SOURCE_ROUTE=yes)(ADDRESS_LIST=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=tcp)(HOST=your2.domain.com)(PORT=11529))(ADDRESS=(PROTOCOL=tcp)(HOST=your2.domain.com)(PORT=11529)))(ADDRESS_LIST=(FAILOVER=on)(LOAD_BALANCE=off)(ADDRESS=(PROTOCOL=tcp)(HOST=your3.domain.com)(port=1521))(ADDRESS=(PROTOCOL=tcp)(HOST=your4.domain.com)(port=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=YOUR_SERVICE.your5.domain.com)))",
        requestTimeout: 100
    },
    fetchAsString: ["number", "clob"]
});

knex.select().from("TABLE1").asCallback(function(err, rows){
    if(err)
        console.log(err);
    else
        console.table(rows);
});