@remotezygote/database
TypeScript icon, indicating that this package has built-in type declarations

1.2.12 • Public • Published

Database

import { query, withDatabaseClient, listen } from '@remotezygote/database'

const getUserData = async email => {
  const { rows } = await query('SELECT * FROM users WHERE email = $1', [email])
  return rows && row[0]
}

const multipleQueriesWithClient = async updates => {
  const { email, data } = updates
  return await withDatabaseClient(async client => {
    const { rows } = await client.query(
      'SELECT * FROM users WHERE email = $1',
      [email]
    )
    await client.query('INSERT INTO user_log (email, item) VALUES ($1, $2)', [
      email,
      'updated'
    ])
  })
}

const queryWithTransaction = async updates => {
  const { email, data } = updates
  const { commit } = await withTransaction(
    async client => {
      const { rows } = await client.query(
        'SELECT * FROM users WHERE email = $1',
        [email]
      )
      await client.query('INSERT INTO user_log (email, item) VALUES ($1, $2)', [
        email,
        'updated'
      ])
    },
    { autoCommit: false }
  )
  return await commit()
}

const queryWithAutoCommit = async updates => {
  const { email, data } = updates
  return await withTransaction(async client => {
    const { rows } = await client.query(
      'SELECT * FROM users WHERE email = $1',
      [email]
    )
    await client.query('INSERT INTO user_log (email, item) VALUES ($1, $2)', [
      email,
      'updated'
    ])
  })
}

const queryWithRollback = async updates => {
  const { email, data } = updates
  const { rollback } = await withTransaction(
    async client => {
      const { rows } = await client.query(
        'SELECT * FROM users WHERE email = $1',
        [email]
      )
      await client.query('INSERT INTO user_log (email, item) VALUES ($1, $2)', [
        email,
        'updated'
      ])
    },
    { autoCommit: false }
  )
  return await rollback()
}

const queryWithAutoRollback = async updates => {
  const { email, data } = updates
  return await withTransaction(
    async client => {
      const { rows } = await client.query(
        'SELECT * FROM users WHERE email = $1',
        [email]
      )
      await client.query('INSERT INTO user_log (email, item) VALUES ($1, $2)', [
        email,
        'updated'
      ])
    },
    { autoRollback: true }
  )
}

const processJob = async job => {
  const queue = queues[job.queue]

  if (queue) {
    await queue.add(job.name, job.data)
  }
}

export const listenForJobs = async () => {
  await listen('jobs', processJob)
}

Configuration

The only configuration needed is the connection information to Postgres, via environment variable.

Environment variables

DATABASE_URL - This library uses only the connection URL method for connection configuration. More info

Readme

Keywords

none

Package Sidebar

Install

npm i @remotezygote/database

Weekly Downloads

171

Version

1.2.12

License

MIT

Unpacked Size

31.3 kB

Total Files

9

Last publish

Collaborators

  • remotezygote