README
pg-boot
This package is a implementation for a common task like applying DB changes when application boots up by versioning the changes.
- Allows you to apply programatic changes.
- Version is a simple integer.
- All changes are applied in transaction so if any one change fails everything is rolled back.
- Can be used with multiple instance of microservices as all changes are applied under advisory lock.
Getting Started
- Install using
npm i pg-boot
- All done, Start using it!!.
Examples/Code snippets
A complete example can be found at here
- Initialize
const bootType = require('pg-boot');
const pgp = require('pg-promise')({ schema: 'Boot' });
const defaultConectionString = 'postgres://postgres:@localhost:5432/QUEUE';
const writeConfigParams = {
connectionString: defaultConectionString,
application_name: 'Example1-Queue-Writer',
max: 2, //2 Writer
};
const pgWriter = pgp(writeConfigParams);
const instance = new bootType.PgBoot('ProductName');
- Upgrade Handller
async function UpgradeHandler(transaction, dbVersion) {
switch (dbVersion) {
case -1: //Green field(Nothing is present in DB) first time install
await transaction.none('CREATE TABLE "V1" ();');
break;
case 0: //Version zero was already present
await transaction.none('ALTER TABLE "V1" RENAME TO "V2";');
break;
}
};
instance
.checkVersion(pgWriter, 0, UpgradeHandler)
.then(console.log)
.catch(console.error)
.finally(() => {
//Simulate Next version upgrade (1)
instance
.checkVersion(pgWriter, 1, UpgradeHandler)
.then(console.log)
.catch(console.error);
});
Built with
- Authors :heart: love :heart: for Open Source.
- pg-promise.
Contributions
- New ideas/techniques are welcomed.
- Raise a Pull Request.
Current Version:
0.0.3[Beta]
License
This project is contrubution to public domain and completely free for use, view LICENSE.md file for details.
Changelog
v0.0.3 :
- Even if the same version exists reconcilecallback is invoked once per instance.