Adding database debug log for database queries
This commit is contained in:
parent
1d5c75b252
commit
5edf0eac68
1 changed files with 5 additions and 1 deletions
|
|
@ -1,10 +1,12 @@
|
|||
import { resolve } from 'path'
|
||||
import sqlite from 'sqlite3'
|
||||
import { asyncLocalStorage } from '../logger'
|
||||
import { CREATE_TABLE_reservations } from './sql'
|
||||
|
||||
export const run = async (sql: string, params?: unknown) => {
|
||||
const db = new sqlite.Database(resolve('autobaan_db'))
|
||||
await new Promise<void>((res, rej) => {
|
||||
asyncLocalStorage.getStore()?.debug(`<database> run ${sql} (${params})`)
|
||||
db.run(sql, params, (err) => {
|
||||
if (err) rej(err)
|
||||
res()
|
||||
|
|
@ -15,11 +17,13 @@ export const run = async (sql: string, params?: unknown) => {
|
|||
|
||||
export const all = async <T>(sql: string, params?: unknown) => {
|
||||
const db = new sqlite.Database(resolve('autobaan_db'))
|
||||
const rows = await new Promise<T[]>((res, rej) =>
|
||||
const rows = await new Promise<T[]>((res, rej) => {
|
||||
asyncLocalStorage.getStore()?.debug(`<database> all ${sql} (${params})`)
|
||||
db.all(sql, params, (err, rows) => {
|
||||
if (err) rej(err)
|
||||
res(rows)
|
||||
})
|
||||
}
|
||||
)
|
||||
db.close()
|
||||
return rows
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue