簡體   English   中英

將 Knex 與自己的 pg 連接池一起使用

[英]Using Knex with own pg connection pool

我想使用 knex 作為查詢構建器,但我的項目已經處理了自己的連接池。

我希望我可以做這樣的事情:

const { Client } = require('pg')

const client = new Client()
await client.connect()

const knex = require('knex')({
    client: 'pg',
    connection: client,
})

有沒有辦法為 knex 提供 pg 客戶端對象,而不是讓它管理自己的連接池?

解決方法

我認為有一種方法可以做到這一點,但只能通過特定查詢來完成。 為此,您可以使用接受連接池實例作為參數的connection(pool)方法。

在我的情況下,我需要 knex 而不傳遞連接參數,然后在建立連接時我將連接實例保存在我的對象中。 然后當我必須使用 knex 作為客戶端時,我在查詢構建中傳遞了連接實例。

代碼示例

這是一個代碼示例:

const knex = require('knex')({
   client: 'pg' // postgreSQL or whatever
});

const poolConnection = await new Client().connect(); // or any other method to create your connection here

// when you have to query your db
const results = await knex('users')
                .connection(poolConnection) // here pass the connection
                .where({
                    email: 'test@tester.com'
                })
                .select('id', 'email', 'createdAt')
                .offset(0)
                .limit(1)
                .first();

使用分子的用例

例如,我將它與提供數據庫適配器的分子一起使用,該數據庫適配器本身已經使用了 SQL 客戶端,因此 knex 將建立到我的數據庫的額外連接。 在我的微服務中檢索連接后,我以與上述相同的方式在 knex 內部使用了該連接。

"use strict";
const DbService = require("moleculer-db");
const SQLAdapter = require("moleculer-db-adapter-sequelize");
const Sequelize = require("sequelize");

// here requiring knex without an actual connection
const knex = require("knex")({
    client: "pg"
});


module.exports = {
    name: "users",
    // implementing moleculer ORM
    mixins: [DbService],
    adapter: new SQLAdapter(process.env.POSTGRECONNECTIONSTRING),
    model: {
        name: "user",
        define: {
            id: {
                type: Sequelize.INTEGER,
                primaryKey: true,
                autoIncrement: true
            },
            email: Sequelize.STRING,
            password: Sequelize.STRING,
        }
    },
    actions: {
        findByIdRaw: {
            params: {
                id: "number"
            },
            handler(ctx) {
                const { id } = ctx.params;
                // use the connection pool instance
                return knex("users")
                    .connection(this.connection)
                    .where({
                        id
                    })
                    .select("id", "email", "createdAt")
                    .offset(0)
                    .limit(1)
                    .first();
            }
        }
    },
    started() {
        // getting the connection from the adapter
        return this.adapter.db.connectionManager.getConnection()
            .then((connection) => {
                // saving connection
                this.connection = connection;
                return Promise.resolve();
            });
    }
};

相關文檔

相關鏈接

否。除非您編寫自己的自定義方言並覆蓋連接獲取功能。 此處描述了編寫自定義方言https://github.com/tgriesser/knex/blob/master/CONTRIBUTING.md#i-would-like-to-add-support-for-new-dialect-to-knex-is-可能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM