npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

knex-dynamic-connection

v3.2.0

Published

Adds support for dynamically returning connection config for knex queries

Downloads

124,348

Readme

Knex Dynamic Connection

This module is meant to patch knex and add support for defining dynamic connection configuration.

gh-workflow-image npm-image

Table of contents

Why you need it?

Knex.js doesn't have inbuilt support for read/write connections. One can create two seperate instances of knex for read and write, but that still doesn't completely solve the problem. For example:

const Knex = require('knex')

const writeConfig = {
  client: 'pg',
  connection: {
  }
}
const writer = Knex(writeConfig)

const readConfig = {
  client: 'pg',
  connection: {
  }
}
const reader = Knex(readConfig)

Now, if you want to use multiple servers for read operations, you cannot do that, since knex.js allows only one connection server and will pool connections within that server.

Following is not possible

const readConfig = {
  client: 'pg',
  connection: [
    {
      host: ''
    },
    {
      host: ''
    }
  ]
}

With the help of this module, you can make knex create a connection using the dynamic config for every query.

npm i knex-dynamic-connection
const Knex = require('knex')
const { patchKnex } = require('knex-dynamic-connection')

const readConfig = {
  client: 'pg',
  connection: {},
  replicas: [
    {
      host: '',
    },
    {
      host: '',
    }
  ],
}

const knex = Knex(readConfig)
let roundRobinCounter = 0

patchKnex(knex, (originalConfig) => {
  const node = roundRobinCounter++ % originalConfig.replicas.length
  return originalConfig.replicas[node]
})

The patchKnex method overwrites the acquireRawConnection on all the dialects and make them fetch the config from your callback vs reading it from a static source.

Is it reliable?

Yes!

  1. I have copied the code of acquireRawConnection from the knex codebase and have just made one line of change to read the config from a different source.
  2. I have written tests for select, insert, transactions and schema methods.
  3. The code is tested against mssql, mysql, mysql2 and pg.
  4. The connection is still managed inside the pool, so don't worry about any extra maintaince overhead.

How does it actually work?

Knex.js makes use of tarn.js for managing pool resources and everytime pool needs a connection, knexjs calls acquireRawConnection on the dialect in use.

The dialect creates a new connection to the database server, but uses static configuration. I have just added a patch, which will rely on your closure to return the config vs using the same static config all the time.

Will this work in the future?

I am using Knex.js to write the ORM of https://adonisjs.com/ and will keep a close eye on the releases of Knex to make sure that I incorporate any changes made of the underlying code and keep this module upto date. If knex.js team plans to re-write the entire codebase (which is less likely to happen), then I will pitch this change to be a first class citizen.

Can I help?

Yes, there are currently no tests for oracle. It will be great, if you can help set it up using docker