1.0.27 • Published 5 years ago

@alwaystudios/as-pg v1.0.27

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

as-pg

A Postgres utilities library

https://www.npmjs.com/package/@alwaystudios/as-pg

  yarn add @alwaystudios/as-pg

Postgres (pg) helper functions

runInTransaction

Uses a connection pool to create a PG client and then starts a transaction allowing for multiple database operations within the transaction context. Rolls back on failure, commits transaction on success.

  await runInTransaction(pool)(async (client) => {
    await client.query(someUpdateQuery1)
    await client.query(someUpdateQuery2)
  })

runInPoolClient

Uses a connection pool to create a PG client.

  await runInPoolClient(pool)(async (client) => {
    await client.query(someQuery)
  })

verifyAtLeastOneRow

Verifies a query result set to ensure at least one row was affected by a query

  await runInPoolClient(pool)(async (client) => {
    await client.query(someQuery).then(verifyAtLeastOneRow('my update operation'))
  })

Poller processor

Creates a database poller process

  const onReceiveData = async <PersonRecord>(records: PersonRecord[]) => {
    console.log(records.length)
  }

  const poller = dbPollerFactory<PersonRecord>({
    pool,
    tableName: 'person',
    interval: 200,
    batchSize: 20,
    onReceiveData,
  })

  const processId = poller.schedulePoller()

  .....

  clearInterval(processId) // stop the poller

Factory props are as follows:

  {
    pool: Pool
    tableName: string
    interval: number
    batchSize: number
    onReceiveData: <T>(records: T[]) => Promise<void>
    deleteRecords?: boolean
  }

Test factories

  • testConnectionPool
  • testPgPool
  • testPgClient
  • testQueryResults
  • testRunInPoolClient
  • testRunInTransaction
1.0.27

5 years ago

1.0.26

5 years ago

1.0.25

5 years ago

1.0.24

5 years ago

1.0.22

5 years ago

1.0.23

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago