0.3.0 • Published 2 months ago

@bausmeier/tractor v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

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
0.3.0

2 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.0

11 months ago