Skip to content

Instantly share code, notes, and snippets.

@odykyi
Created September 26, 2017 14:53
Show Gist options
  • Save odykyi/cb397696f399bed486ab7dda70714d88 to your computer and use it in GitHub Desktop.
Save odykyi/cb397696f399bed486ab7dda70714d88 to your computer and use it in GitHub Desktop.
a.js
const promise = require('bluebird'); // or any other Promise/A+ compatible library;
const options = {
promiseLib: promise // overriding the default (ES6 Promise);
};
const pgp = require('pg-promise')(options);
const cn = {
host: 'localhost', // 'localhost' is the default;
port: 5432, // 5432 is the default;
database: 'postgres',
user: 'LOGIN',
password: 'PASS'
};
const db = pgp(cn);
db.any(`CREATE TABLE article (
article_id bigserial primary key,
article_name varchar(20) NOT NULL,
article_desc text NOT NULL
);`)
.then(data => {
return db.any(`
INSERT INTO article (article_id, article_name, article_desc)
VALUES (1, 'djfjjdfs', 'fsdfsd')
`)
})
.then(data => {
console.log('DATA:', data); // print data;
})
.catch(error => {
console.log('ERROR:', error); // print the error;
})
.finally(() => {
// If we do not close the connection pool when exiting the application,
// it may take 30 seconds (poolIdleTimeout) before the process terminates,
// waiting for the connection to expire in the pool.
// But if you normally just kill the process, then it doesn't matter.
pgp.end(); // For immediate app exit, shutting down all connection pools
// See API: http://vitaly-t.github.io/pg-promise/module-pg-promise.html#~end
// See also:
// https://github.com/vitaly-t/pg-promise#library-de-initialization
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment