pool-by-client v1.0.14
pool-by-client
Glossary
- Install
- Usage
- API
- Example
- Publishing to NPM
Install
- Install
pool-by-clientpackage:
npm i pool-by-clientUsage
- 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:
dbByClientdbWriteByClientendPoolsconfigspoolMap
dbByClientanddbWriteByClientare functions that take in a clientId and returns a respective pool.endPoolsis a function that ends all pools.configsandpoolMapobjects are provided if in-depth modifications are needed to be made.
API
Supports the following clouds for config storage using the
pkgcloudpackage:- filesystem (local file)
- amazon (AWS S3 Storage)
- google (Google Cloud Storage)
- azure (Azue Cloud Storage)
The package uses the parent application's
process.envobject 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.envproperties to prevent passwords from being in public files.- For example you would set a variable
DATABASE_PASSin your.envfile. In the config storage file, the db'spasswordproperty be set to the name of your.envvariable 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. ThedbByClientanddbWriteByClientmethods 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. IfwriteHostproperty exists, thehostwill 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.jsonfile:
[
{
"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
.envfile:
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 buildto update thelibdirectory 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.jsonbefore doing so.