1.2.19 • Published 11 months ago
@remotezygote/database v1.2.19
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
1.2.18
11 months ago
1.2.19
11 months ago
1.2.16-alpha.0
12 months ago
1.2.15-alpha.0
12 months ago
1.2.18-alpha.5
11 months ago
1.2.18-alpha.0
11 months ago
1.2.18-alpha.1
11 months ago
1.2.18-alpha.2
11 months ago
1.2.18-alpha.3
11 months ago
1.2.13
12 months ago
1.2.16
12 months ago
1.2.17
12 months ago
1.2.14
12 months ago
1.2.15
12 months ago
1.2.12
1 year ago
1.2.10
1 year ago
1.2.11
1 year ago
1.2.9
1 year ago
1.2.8
2 years ago
1.2.7
2 years ago
1.2.6
2 years ago
1.2.5
2 years ago
1.2.4
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.2.0
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago