2022-10-23 11:55:47 +02:00
|
|
|
import mysql, { Connection, ConnectionConfig, FieldInfo } from 'mysql'
|
|
|
|
|
import { TABLE_reservations } from './sql'
|
|
|
|
|
|
|
|
|
|
const createConnectionConfig = async (): Promise<ConnectionConfig> => {
|
2022-11-21 18:50:59 +01:00
|
|
|
const user = process.env.MYSQL_USER
|
|
|
|
|
const password = process.env.MYSQL_PASSWORD
|
|
|
|
|
const database = process.env.MYSQL_DATABASE
|
|
|
|
|
if (!user || !password || !database) {
|
|
|
|
|
throw new DatabaseEnvironmentError(
|
|
|
|
|
'Required environment variables are missing'
|
|
|
|
|
)
|
|
|
|
|
}
|
2022-10-23 11:55:47 +02:00
|
|
|
return {
|
2022-11-21 18:50:59 +01:00
|
|
|
user,
|
|
|
|
|
password,
|
|
|
|
|
database,
|
2022-10-23 11:55:47 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let connection: Connection
|
|
|
|
|
|
|
|
|
|
export const getConnection = async (): Promise<Connection> => {
|
|
|
|
|
if (!connection) {
|
|
|
|
|
const config = await createConnectionConfig()
|
|
|
|
|
connection = mysql.createConnection(config)
|
|
|
|
|
}
|
|
|
|
|
return connection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type ResultSet<T> = T[]
|
|
|
|
|
|
|
|
|
|
export const connect = async () => {
|
|
|
|
|
return new Promise<void>((res, rej) =>
|
|
|
|
|
getConnection().then((cn) => {
|
|
|
|
|
cn.connect((err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
rej(err)
|
|
|
|
|
}
|
|
|
|
|
res()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const disconnect = async () => {
|
|
|
|
|
return new Promise<void>((res, rej) =>
|
|
|
|
|
getConnection().then((cn) => {
|
|
|
|
|
cn.end((err) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
rej(err)
|
|
|
|
|
}
|
|
|
|
|
res()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const query = async <T = unknown, V = unknown>(
|
|
|
|
|
sql: string,
|
|
|
|
|
values?: V
|
|
|
|
|
): Promise<{ results: ResultSet<T>; fields?: FieldInfo[] }> => {
|
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
|
connection.query({ sql, values }, (err, results, fields) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
rej(err)
|
|
|
|
|
}
|
|
|
|
|
res({ results, fields })
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const init = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await connect()
|
|
|
|
|
await query(TABLE_reservations)
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
console.error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-21 18:50:59 +01:00
|
|
|
|
|
|
|
|
export class DatabaseError extends Error {}
|
|
|
|
|
export class DatabaseEnvironmentError extends DatabaseError {}
|