README
pool-by-client
Glossary
- Install
- Usage
- API
- Example
- Publishing to NPM
Install
- Install
pool-by-client
package:
npm i pool-by-client
Usage
- Import into your project
const poolByClient = require('pool-by-client')
Call the poolByClient function with parameters:
provider(STRING)
: name of provider; ie: 'amazon'container(STRING)
: name of container; ie: s3 bucket namefile(STRING)
: name of file in container; ie: s3 key nameopts(OBJECT)
: options object
opts.unique(BOOLEAN)
: will filter out duplicate database pools. Defaults to false.
Returns a promise that resolves as an object with three methods:
dbByClient
dbWriteByClient
endPools
configs
poolMap
dbByClient
anddbWriteByClient
are functions that take in a clientId and returns a respective pool.endPools
is a function that ends all pools.configs
andpoolMap
objects are provided if in-depth modifications are needed to be made.
API
Supports the following clouds for config storage using the
pkgcloud
package:- filesystem (local file)
- amazon (AWS S3 Storage)
- google (Google Cloud Storage)
- azure (Azue Cloud Storage)
The package uses the parent application's
process.env
object for env variables.- filesystem env variables:
DB_CONFIGS_PATH
- Amazon env variables:
AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
- Google env variables:
GOOGLE_KEY_FILE_NAME, GOOGLE_PROJECT_ID
- Azure env variables:
AZURE_STORAGE_ACCOUNT, AZURE_STORAGE_ACCESS_KEY
- filesystem env variables:
Database Passwords are mapped from
process.env
properties to prevent passwords from being in public files.- For example you would set a variable
DATABASE_PASS
in your.env
file. In the config storage file, the db'spassword
property be set to the name of your.env
variable to reference so the package can map the password.
- For example you would set a variable
The Config File
An array of objects that contains information about each database to create a pool from.
clientDbid
: A number you want the pool to map to.name
: A string representing the client.default
: A boolean determining the default database. ThedbByClient
anddbWriteByClient
methods will point to the default database if no argument is provided.db
: An object containing the pg pool configurations.writeHost
: OPTIONAL. A string representing the host name if CQRS is enabled for read-only. IfwriteHost
property exists, thehost
will be the read-only host.ssl
: OPTIONAL. A boolean to enable SSL. Defaults to false.idleTimeoutMillis
: OPTIONAL. A number representing how long in milliseconds for a pool connection to stay alive after idle. Defaults to 10000.max
: OPTIONAL. A number representing the max size of the pool. Defaults to 10.
Examples
- Example of the
configs.json
file:
[
{
"clientDbid": 123,
"name": "exampleClient",
"db": {
"user": "user",
"password": "DATABASE_PASS",
"host": "example-host-ro.com",
"writeHost": "example-host.com",
"port": 5432,
"database": "exampleDb",
"ssl": false,
"idleTimeoutMillis": 30000,
"max": 20
}
}
]
- Example of the
.env
file:
DATABASE_PASS=password123
# PKG_CLOUD_PROVIDER=filesystem
# PKG_CLOUD_PROVIDER=google
# PKG_CLOUD_PROVIDER=azure
PKG_CLOUD_PROVIDER=amazon
PKG_CLOUD_CONTAINER=myBucket
PKG_CLOUD_FILE=dev/configs.json
- Example of usage in app:
const {
PKG_CLOUD_PROVIDER, PKG_CLOUD_CONTAINER, PKG_CLOUD_FILE,
} = process.env
const example = async () => {
const {
dbByClient,
dbWriteByClient,
endPools,
} = await poolByClient(PKG_CLOUD_PROVIDER, PKG_CLOUD_CONTAINER, PKG_CLOUD_FILE)
const result = await dbByClient(123).query('SELECT * FROM my_table')
await endPools()
}
Publishing to NPM
- IMPORTANT! Run
npm build
to update thelib
directory with the compiled files and in turn the GitHub repo. If this isn't done, there will be no changes to the package when imported! - Travis will automatically publish to NPM when a feature branch is merged to master. Make sure to update the version number in
package.json
before doing so.