egg-pg
PostgreSQL plugin for Egg.js
NOTE: This plugin just for integrate pg into Egg.js, more documentation please visit https://node-postgres.com/.
Install
$ npm i @eggplugin/pg --save
Configuration
exports.pg = {
enable: true,
package: '@eggplugin/pg',
};
see config/config.default.js for more detail.
Simple database instance
exports.pg = {
client: {
user: '',
host: '',
database: '',
password: ''
},
app: true,
agent: false,
};
Usage:
(async () => {
const pool = app.pg;
const client = await pool.connect();
try {
const res = await client.query('SELECT * FROM users WHERE id = $1', [1]);
console.log(res.rows[0]);
} finally {
client.release();
}
const { rows } = await pool.query('SELECT * FROM users WHERE id = $1', [1]);
console.log('user:', rows[0]);
}).catch(console.error);
Multiple database instance
exports.pg = {
default: {
port: 5432,
max: 5,
},
clients: {
db1: {
user: '',
host: '',
database: '',
password: ''
},
db2: {
user: '',
host: '',
database: '',
password: ''
},
},
app: true,
agent: false,
};
Usage:
(async () => {
const pool1 = app.pg.get('db1');
const pool2 = app.pg.get('db2');
const result1 = await pool1.query('SELECT NOW()');
const result2 = await pool2.query('SELECT NOW()');
const client1 = await pool1.connect();
const client2 = await pool2.connect();
}).catch(console.error);
Questions & Suggestions
Please open an issue here.
License
MIT