PostgresUpdater
installation
npm i --save postgresupdater
usage
const pg = require('postgresupdater')(host, port, user, password, database);
pg.waitForInit().then(async () => {
console.log(pg.getVersion()); //prints out the current database version
const data = await pg.query('SELECT * FROM "SomeTable" WHERE ID = $1', [10]); // get entry with id 10
pg.stop(); // end database connection
});
constructor arguments:
postgresupdater(
host, //host to connect to
port, //port of the postgres server
user, //username
password, //password
database, //database to connect to
schema, //schema to use (default 'public')
recreate //if true, deletes current schema, creates an empty one and runs all update scripts on that empty schema (default: false)
)
updates
every time the module gets initialized, it searches for updates in ./updates/ in the current working directory. update files have to be named xxxx.sql with the name consisting of only numbers. all updates will be executed in ascending order. note that updates with leading 0 will not be executed since this module uses an incrementing number to find updates.
0.sql
-- first update, initializing tables
CREATE TABLE "SomeTable" (
"ID" SERIAL,
"Value" CHARACTER(3) NOT NULL
);
1.sql
-- insert some data
INSERT INTO "SomeTable" ("Value") VALUES ('asd');
INSERT INTO "SomeTable" ("Value") VALUES ('fgh');
INSERT INTO "SomeTable" ("Value") VALUES ('jkl');
After starting this example, the database will have one table with 3 rows of data by default