pg-fusion
TypeScript icon, indicating that this package has built-in type declarations

0.3.0 • Public • Published

pg-fusion

npm Codecov CircleCI

This package combines the features of multiple libraries into a single unified interface to get your PostgreSQL-backed application up and running quickly. Especially useful for projects that do not want to use ORMs, for whatever reason. This library provides an interface that roughly combines the interfaces of the following libraries:

Features include:

  • Automatic pool/client/transaction management
  • A SQL query builder that automatically parametrizes values
  • Integrated migration runner (backed by node-pg-migrate)
  • Test helpers for clearing databases and unit testing SQL queries

Usage

import { Database, sql } from 'pg-fusion'

async function getSongs(db: Database) {
  // Will be parametrized, to prevent SQL injection attacks
  const name = 'Take On Me'

  const songs = await db.query(sql`
    SELECT * FROM song
    WHERE song.name = ${name}
  `)

  return songs
}

async function run() {
  const db = new Database(/* normal pg.Pool options */)

  const songs = await getSongs(db)
  console.log(songs)

  await db.close()
}
import 'pg-fusion/testutils/extend-expect'

test('getSongs', () => {
  const db = new Database(...)
  const querySpy = jest.spyOn(db, 'query')
  await getSongs(db)

  expect(querySpy).toHaveBeenCalledWith(
    expect.sqlMatching({
      // Ignores whitespace differences
      text: 'SELECT * FROM song WHERE song.name = $1',
      values: ['Take On Me'],
    })
  )

  // Equivalently
  expect(querySpy.mock.calls[0][0]).toMatchSql({
    text: 'SELECT * FROM song WHERE song.name = $1',
    values: ['Take On Me'],
  })
})

Contributing

Build

yarn build

Lint

yarn lint .

If you have pre-commit installed, run pre-commit install to run linting and other checks before committing.

Run unit tests

yarn test

Run end-to-end tests

  1. Run a Postgres server in Docker

    docker-compose up -d
  2. yarn test:e2e

Package Sidebar

Install

npm i pg-fusion

Weekly Downloads

3

Version

0.3.0

License

MIT

Unpacked Size

228 kB

Total Files

43

Last publish

Collaborators

  • brandonchinn178