@bausmeier/tractor
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

Transactor

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

Readme

Keywords

none

Package Sidebar

Install

npm i @bausmeier/tractor

Weekly Downloads

1

Version

0.3.0

License

MIT

Unpacked Size

7.24 kB

Total Files

6

Last publish

Collaborators

  • bausmeier