@bausmeier/tractor
v0.3.0
Published
A simple wrapper around `pg.Pool` for managing transactions.
Downloads
23
Readme
Tr~ans~actor
A simple wrapper around pg.Pool
for managing transactions.
Usage
Create a connection pool:
import {Pool} from 'pg'
const pool = new Pool({
// Connection options go here.
})
pool.on('connect', (client) => {
client.on('error', () => {
// Squash the error. It will surface when the client is used.
})
})
pool.on('error', (err) => {
console.error('Pool error', err)
})
Create a transactor from the pool:
import createTransactor from '@bausmeier/tractor'
const db = createTransactor(pool)
Execute some queries within a transaction:
type ExampleRow = {
name: string
}
const examples = await db.runInTransaction(
async (client) => {
await client.query('INSERT INTO examples (name) VALUES ($1)', ['test'])
const {rows} = await client.query<ExampleRow>('SELECT name FROM examples')
return rows
},
{
isolationLevel: 'SERIALIZABLE',
}
)
Publishing
rm tsconfig.tsbuildinfo
rm -r build/
yarn run build
yarn publish --access=public