-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.server.ts
69 lines (63 loc) · 1.92 KB
/
db.server.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { exec } from 'child_process'
import { promisify } from 'util'
import { remember } from '@epic-web/remember'
import { PrismaClient } from '@prisma/client'
export * as sql from '@prisma/client/sql'
import chalk from 'chalk'
export const prisma = remember('prisma', () => {
// NOTE: if you change anything in this function you'll need to restart
// the dev server to see your changes.
// Feel free to change this log threshold to something that makes sense for you
const logThreshold = 20
const client = new PrismaClient({
log: [
{ level: 'query', emit: 'event' },
{ level: 'error', emit: 'stdout' },
{ level: 'warn', emit: 'stdout' },
],
})
client.$on('query', async (e) => {
if (e.duration < logThreshold) return
const color =
e.duration < logThreshold * 1.1
? 'green'
: e.duration < logThreshold * 1.2
? 'blue'
: e.duration < logThreshold * 1.3
? 'yellow'
: e.duration < logThreshold * 1.4
? 'redBright'
: 'red'
const dur = chalk[color](`${e.duration}ms`)
console.info(`prisma:query - ${dur} - ${e.query}`)
})
void client.$connect()
return client
})
const execAsync = promisify(exec)
export async function backupDB() {
try {
const { stdout, stderr } = await execAsync('./other/backup_db.sh')
if (stderr) {
console.error('Failed to create db backup:', stderr)
throw new Error('DB Backup script encountered an error')
}
console.log('DB backup output:', stdout)
} catch (error) {
console.error('Failed to execute DB backup script:', error)
throw error
}
}
export async function restoreDB() {
try {
const { stdout, stderr } = await execAsync('./other/restore_db.sh')
if (stderr) {
console.error('Failed to restore db backup:', stderr)
throw new Error('DB Restore script encountered an error')
}
console.log('DB restore output:', stdout)
} catch (error) {
console.error('Failed to execute DB restore script:', error)
throw error
}
}