@lingua/pg-query
v0.0.1
Published
Postgres query building utility
Downloads
1
Readme
Postgres query building utility
Install
npm install --save git+ssh://[email protected]/lingua-service/pg-query.git
Setup
const pg = require('pg-query');
const winston = require('winston');
const config = {
host: 'localhost',
port: 5432,
database: 'db_name',
user: 'username',
password: 'secretpassword',
maxClients: 10,
idleTimeoutMillis: 30000,
};
const logger = new winston.Logger({
transports: [
new winston.transports.Console()
]
});
pg.init(config, logger);
Use
const pg = require('pg-query');
// Raw query
pg.query('select * from some_table')
.then((results) => {
console.log(results);
});
// Select query builder
const selectQuery = pg.select()
.from('users')
.where('name', '=', 'bob')
.limit(20)
.offset(10);
selectQuery.execute()
.then((results) => {
console.log(results);
});
// Insert query builder
const insertQuery = pg.insert()
.into('users')
.values({
name: 'bob',
age: 23
});
insertQuery.execute()
.then((result) => {
console.log(result);
});
// Update query builder
const updateQuery = pg.update('users')
.set('age', 24)
.where('name', '=', 'bob');
updateQuery.execute()
.then((result) => {
console.log(result);
});