yeps-pg
v1.1.1
Published
YEPS PostgreSQL client
Downloads
2
Maintainers
Readme
YEPS PostgreSQL
YEPS PostgreSQL client
How to install
npm i -S yeps-pg
How to use
Docker
If you use docker you can start PostgreSQL:
npm run test:db:start
And stop it:
npm run test:db:stop
Config
config/default.json
{
"pg": {
"host": "127.0.0.1",
"port": 5432,
"user": "user",
"password": "password",
"database": "database"
}
}
Middleware
const App = require('yeps');
const app = new App();
const error = require('yeps-error');
const logger = require('yeps-logger');
const pg = require('yeps-pg');
app.all([
error(),
logger(),
pg(),
]);
app.then(async ctx => {
const result = await ctx.pg.query('SELECT * FROM users;');
});
And with connection:
const App = require('yeps');
const app = new App();
const error = require('yeps-error');
const logger = require('yeps-logger');
const pg = require('yeps-pg');
app.all([
error(),
logger(),
pg(),
]);
app.then(async ctx => {
const client = await ctx.pg.connect();
const result = await client.query('SELECT * FROM users;');
client.release();
});
In module
const pool = require('yeps-pg/pool');
const logger = require('yeps-logger/logger');
async () => {
try {
const result = await pool.query('SELECT * FROM users;');
} catch (error) {
logger.error(error);
}
};
And with connection:
const pool = require('yeps-pg/pool');
const logger = require('yeps-logger/logger');
async () => {
try {
const client = await ctx.pg.connect();
const result = await client.query('SELECT * FROM users;');
client.release();
} catch (error) {
logger.error(error);
}
};
Transactions
const App = require('yeps');
const app = new App();
const error = require('yeps-error');
const logger = require('yeps-logger');
const pg = require('yeps-pg');
app.all([
error(),
logger(),
pg(),
]);
app.then(async ctx => {
const client = await ctx.pg.connect();
try {
await client.query('BEGIN');
await client.query('DELETE FROM users;');
await client.query('COMMIT');
} catch (e) {
await client.query('ROLLBACK');
} finally {
client.release();
}
});
In module:
const pool = require('yeps-pg/pool');
const logger = require('yeps-logger/logger');
async () => {
const client = await pool.connect();
try {
await client.query('BEGIN');
await client.query('DELETE FROM users;');
await client.query('COMMIT');
} catch (error) {
logger.error(error);
await client.query('ROLLBACK');
} finally {
client.release();
}
};
YEPS documentation
Dependencies:
- node-postgres - PostgreSQL node client
- config - node.js config