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

duluca / document-ts / bfe60346-8149-4515-a54f-d9b99ee82fc3

03 Aug 2023 02:36AM UTC coverage: 89.952% (-0.4%) from 90.315%
bfe60346-8149-4515-a54f-d9b99ee82fc3

push

circleci

web-flow
Merge pull request #61 from duluca/config-tweaks

Config tweaks

119 of 152 branches covered (78.29%)

Branch coverage included in aggregate %.

5 of 5 new or added lines in 3 files covered. (100.0%)

624 of 674 relevant lines covered (92.58%)

59.94 hits per line

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

92.56
/src/database.ts
1
import { readFileSync } from 'fs'
1✔
2

1✔
3
import { Db, MongoClient, MongoClientOptions, MongoError } from 'mongodb'
1✔
4

1✔
5
let dbInstance: Db | null
1✔
6
let mongoClient: MongoClient | null
1✔
7
let _connectionStatus = false
1✔
8

1✔
9
export async function connect(
1✔
10
  mongoUri: string,
30✔
11
  isProduction = false,
30✔
12
  connectionRetryWait = 5,
30✔
13
  connectionRetryMax = 10,
30✔
14
  certFileUri?: string,
30✔
15
  overrideOptions?: MongoClientOptions
30✔
16
) {
30✔
17
  let mongoOptions: MongoClientOptions = {}
30✔
18
  console.log(`Connecting to database... Prod mode: ${isProduction}.`)
30✔
19

30✔
20
  if (certFileUri) {
30✔
21
    const certFileBuf = [readFileSync(certFileUri)]
1✔
22

1✔
23
    mongoOptions = Object.assign(mongoOptions, {
1✔
24
      ssl: true,
1✔
25
      sslValidate: true,
1✔
26
      sslCA: certFileBuf,
1✔
27
      poolSize: 1,
1✔
28
    })
1✔
29
  }
1✔
30

29✔
31
  let retryAttempt = 0
29✔
32
  let lastException: MongoError | null = null
29✔
33

29✔
34
  if (!connectionRetryMax) {
30!
35
    connectionRetryMax = 1
×
36
  }
×
37

29✔
38
  if (overrideOptions) {
30✔
39
    Object.assign(mongoOptions, overrideOptions)
1✔
40
  }
1✔
41

29✔
42
  while (retryAttempt < connectionRetryMax && !dbInstance) {
30✔
43
    try {
30✔
44
      mongoClient = await MongoClient.connect(mongoUri, mongoOptions)
30✔
45
      dbInstance = mongoClient.db()
28✔
46
      _connectionStatus = true
28✔
47
    } catch (ex: unknown) {
28✔
48
      _connectionStatus = false
2✔
49
      retryAttempt++
2✔
50
      if (ex instanceof Error) {
2✔
51
        console.log(ex.message)
2✔
52
      }
2✔
53
      if (ex instanceof MongoError) {
2!
54
        lastException = ex
×
55
      }
×
56
      if (connectionRetryWait) {
2✔
57
        console.log(`${retryAttempt}: Retrying in ${connectionRetryWait}s...`)
2✔
58
        await sleep(connectionRetryWait)
2✔
59
      }
2✔
60
    }
2✔
61
  }
30✔
62

29✔
63
  if (!dbInstance) {
30✔
64
    if (!lastException) {
1✔
65
      throw new Error(
1✔
66
        'Unable to connect to the database, please verify that your configuration is correct'
1✔
67
      )
1✔
68
    }
1✔
69
    throw lastException
×
70
  }
×
71
}
30✔
72

1✔
73
function sleep(seconds: number) {
2✔
74
  return new Promise((resolve) => setTimeout(resolve, seconds * 1000))
2✔
75
}
2✔
76

1✔
77
export async function close(force = false) {
1✔
78
  if (mongoClient) {
32✔
79
    await mongoClient.close(force)
28✔
80
    dbInstance = null
28✔
81
    mongoClient = null
28✔
82
    _connectionStatus = false
28✔
83
  }
28✔
84
}
32✔
85

1✔
86
export function connectionStatus() {
2✔
87
  if (mongoClient) {
2✔
88
    return _connectionStatus
1✔
89
  }
1✔
90
  return false
1✔
91
}
1✔
92

1✔
93
export function getDbInstance(): Db {
287✔
94
  if (!dbInstance) {
287✔
95
    throw new Error('Database is not yet instantiated')
1✔
96
  }
1✔
97
  return dbInstance
286✔
98
}
286✔
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

© 2026 Coveralls, Inc