0.1.0 • Published 2 years ago
pg-query-tag v0.1.0
pg-query-tag
Install
npm install pg
npm install pg-query-tag
get help with typescript
npm install -D @types/pg
Usage
init query tag
set global pool then you can use query tag everywhere
import {Pool} from 'pg';
import {query, setPool, transaction} from 'pg-query-tag';
const pool = new Pool({
connectionString: 'postgres://postgres:postgres@localhost:5432/postgres',
})
// setPool before use query
setPool(pool);
// or set pool and schema
setPool(pool, 'public');
or just create a query tag
import {createQuery} from 'pg-query-tag';
const query = createQuery('postgres://postgres:postgres@localhost:5432/postgres')
const id = 1;
const result = await query`select * from users where id = ${id}`;
console.log(result.rows)
await query.client.end()
use transaction
//...
const id = 1;
const name = 'user1';
const result = await transaction(async (query) => {
await query`insert into users (id,name) values (${id},${name})`;
return await query`select * from users where id = ${id}`;
});
console.log(result.rows)