• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

timgit / pg-boss / 9949043480

16 Jul 2024 01:09AM UTC coverage: 94.41% (-5.6%) from 100.0%
9949043480

Pull #425

github

web-flow
Merge 6b98fbe61 into f1c1636ca
Pull Request #425: v10

466 of 546 branches covered (85.35%)

351 of 365 new or added lines in 10 files covered. (96.16%)

40 existing lines in 5 files now uncovered.

912 of 966 relevant lines covered (94.41%)

803.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/db.js
1
const EventEmitter = require('events')
4✔
2
const pg = require('pg')
4✔
3
const { advisoryLock } = require('./plans')
4✔
4

5
class Db extends EventEmitter {
6
  constructor (config) {
7
    super()
560✔
8

9
    config.application_name = config.application_name || 'pgboss'
560✔
10

11
    this.config = config
560✔
12
  }
13

14
  events = {
560✔
15
    error: 'error'
16
  }
17

18
  async open () {
19
    this.pool = new pg.Pool(this.config)
559✔
20
    this.pool.on('error', error => this.emit('error', error))
559✔
21
    this.opened = true
559✔
22
  }
23

24
  async close () {
25
    if (!this.pool.ending) {
371!
26
      this.opened = false
371✔
27
      await this.pool.end()
371✔
28
    }
29
  }
30

31
  async executeSql (text, values) {
32
    if (this.opened) {
27,607✔
33
      if (this.config.debug === true) {
27,606!
NEW
34
        console.log(`${new Date().toISOString()}: DEBUG SQL`)
×
NEW
35
        console.log(text)
×
36

NEW
37
        if (values) {
×
NEW
38
          console.log(`${new Date().toISOString()}: DEBUG VALUES`)
×
NEW
39
          console.log(values)
×
40
        }
41
      }
42

43
      return await this.pool.query(text, values)
27,606✔
44
    }
45
  }
46

47
  async lock ({ timeout = 30, key } = {}) {
27!
48
    const lockedClient = await this.pool.connect()
27✔
49

50
    const query = `
27✔
51
        BEGIN;
52
        SET LOCAL lock_timeout = '${timeout}s';
53
        SET LOCAL idle_in_transaction_session_timeout = '${timeout}s';
54
        ${advisoryLock(this.config.schema, key)};
55
    `
56

57
    await lockedClient.query(query)
27✔
58

59
    const locker = {
27✔
60
      unlock: async function () {
61
        try {
27✔
62
          await lockedClient.query('COMMIT')
27✔
63
        } finally {
64
          lockedClient.release()
27✔
65
        }
66
      }
67
    }
68

69
    return locker
27✔
70
  }
71
}
72

73
module.exports = Db
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc