-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
39 lines (32 loc) · 1.24 KB
/
db.ts
File metadata and controls
39 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { VerbaString } from '../../src'
import logger from './log'
import { sleep } from './util'
const MOCK_DB_INTRA_LOADING_MESSAGES: VerbaString[] = [
f => `DB version: ${f.cyan('42.0.0')}.`,
f => `DB client healthy?: ${f.green('YES')}`,
f => `DB network latency: ${f.yellow('45ms')}`,
]
// Test logger child-ification
const log = logger.child({ code: 'CONNECT_DB' })
export const connectToDb = async () => {
// Test starting spinner (string-type options)
const spinner = log.spinner('Connecting to DB.')
// Test logging whilst a spinner is active (TTY outlet interruption)
let i = 0
// Log some info every 0.5s
const interval = setInterval(() => {
log.info(MOCK_DB_INTRA_LOADING_MESSAGES[i])
i++
}, 500)
// We do the loop for 1.9s to ensure that we only do max 3 logs, which is how many
// have been prepared in the log message array, i.e. floor(1.9 / 0.5) = 3.
// Interestingly, as of writing, having this as 2s still only results in 3 0.5s loops
// whereas with Bun, it results in 4 0.5s loops (causing a failure).
setTimeout(() => {
clearInterval(interval)
}, 1900)
await sleep(2)
// Test stopping spinner
spinner.clear()
log.success(c => `Connected to DB at ${c.cyan(c.underline('127.0.0.1:5432'))}.`)
}