This commit is contained in:
Tutur33
2023-11-24 23:58:26 +01:00
parent 25395c0ee1
commit 938ad9d309
4191 changed files with 41 additions and 518781 deletions
-9
View File
@@ -1,9 +0,0 @@
# The MIT License
Copyright 2021 Harminder Virk, contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-114
View File
@@ -1,114 +0,0 @@
# Knex Dynamic Connection
This module is meant to patch knex and add support for defining dynamic connection configuration.
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![](https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript)
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of contents
- [Why you need it?](#why-you-need-it)
- [Is it reliable?](#is-it-reliable)
- [How does it actually work?](#how-does-it-actually-work)
- [Will this work in the future?](#will-this-work-in-the-future)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
## 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:
```js
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**
```js
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.
```sh
npm i knex-dynamic-connection
```
```js
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](https://github.com/vincit/tarn.js/) for managing pool resources and everytime pool needs a connection, knexjs calls [acquireRawConnection](https://github.com/tgriesser/knex/blob/master/lib/client.js#L258) 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
[gh-workflow-image]: https://img.shields.io/github/actions/workflow/status/thetutlage/knex-dynamic-connection/test.yml?style=for-the-badge
[gh-workflow-url]: https://github.com/thetutlage/knex-dynamic-connection/actions/workflows/test.yml "Github action"
[npm-image]: https://img.shields.io/npm/v/knex-dynamic-connection.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/knex-dynamic-connection "npm"
-95
View File
@@ -1,95 +0,0 @@
{
"name": "knex-dynamic-connection",
"version": "3.1.1",
"description": "Adds support for dynamically returning connection config for knex queries",
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"test:mysql": "DIALECT=mysql node japaFile.js",
"test:mysql2": "DIALECT=mysql2 node japaFile.js",
"test:mssql": "DIALECT=mssql node japaFile.js",
"test:pg": "DIALECT=pg node japaFile.js",
"lint": "eslint . --ext=.ts",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
"commit": "git-cz",
"release": "np",
"version": "npm run build",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json thetutlage/knex-dynamic-connection",
"format": "prettier --write ."
},
"engines": {
"node": ">=14.0.0"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/thetutlage/knex-dynamic-connection.git"
},
"keywords": [
"knex",
"db"
],
"author": "virk",
"license": "MIT",
"bugs": {
"url": "https://github.com/thetutlage/knex-dynamic-connection/issues"
},
"homepage": "https://github.com/thetutlage/knex-dynamic-connection#readme",
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@types/node": "^20.8.7",
"del-cli": "^5.1.0",
"doctoc": "^2.2.1",
"dotenv": "^16.3.1",
"eslint": "^8.51.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-adonis": "^2.1.1",
"eslint-plugin-prettier": "^5.0.1",
"github-label-sync": "^2.3.1",
"husky": "^8.0.3",
"japa": "^4.0.0",
"mrm": "^4.1.22",
"mysql": "^2.18.1",
"mysql2": "^3.6.2",
"np": "^8.0.4",
"pg": "^8.11.3",
"prettier": "^3.0.3",
"tedious": "^16.5.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"nyc": {
"exclude": [
"test"
],
"extension": [
".ts"
]
},
"main": "build/index.js",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
],
"husky": {
"hooks": {
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md",
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"
}
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"dependencies": {
"debug": "^4.3.4",
"knex": "^3.0.1"
},
"np": {
"contents": ".",
"anyBranch": false
}
}