WIP: build attempted

This commit is contained in:
2024-02-22 23:42:04 +11:00
parent a1cba242e9
commit 5c8a3f56dd
5 changed files with 56 additions and 37 deletions

View File

@@ -1,39 +1,42 @@
import mysql, { PoolOptions } from "mysql2";
import mysql, { PoolOptions, Pool } from "mysql2";
import { Pool as pPool } from "mysql2/promise"
import * as fs from 'fs';
import * as appEnv from "./env";
if (typeof process.env.MYSQL_SSL_CA === 'undefined') {
throw new Error("missing MYSQL_SSL_CA")
}
let pool: Pool | undefined;
let promisePool: pPool | undefined;
if (typeof process.env.MYSQL_SSL_KEY === 'undefined') {
throw new Error("missing MYSQL_SSL_KEY")
}
if (typeof process.env.MYSQL_SSL_CERT === 'undefined') {
throw new Error("missing MYSQL_SSL_CERT")
}
const access: PoolOptions = {
host: appEnv.getMysqlHost(),
port: appEnv.getMysqlPort(),
user: appEnv.getMysqlUser(),
password: appEnv.getMysqlPassword(),
database: appEnv.getMysqlDatabase(),
waitForConnections: true,
connectionLimit: 10,
maxIdle: 10,
idleTimeout: 60000,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0,
ssl: {
ca: fs.readFileSync(appEnv.getMysqlSslCaFile()),
key: fs.readFileSync(appEnv.getMysqlSslKeyFile()),
cert: fs.readFileSync(appEnv.getMysqlSslCertFile())
export function getPool(): Pool {
const access: PoolOptions = {
host: appEnv.getMysqlHost(),
port: appEnv.getMysqlPort(),
user: appEnv.getMysqlUser(),
password: appEnv.getMysqlPassword(),
database: appEnv.getMysqlDatabase(),
waitForConnections: true,
connectionLimit: 10,
maxIdle: 10,
idleTimeout: 60000,
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0,
ssl: {
ca: fs.readFileSync(appEnv.getMysqlSslCaFile()),
key: fs.readFileSync(appEnv.getMysqlSslKeyFile()),
cert: fs.readFileSync(appEnv.getMysqlSslCertFile())
}
}
if (typeof pool === 'undefined') {
pool = mysql.createPool(access)
}
return pool
}
export const pool = mysql.createPool(access)
export const promisePool = pool.promise()
export function getPromisePool(): pPool {
if (typeof promisePool === 'undefined') {
promisePool = getPool().promise()
}
return promisePool
}

View File

@@ -1,9 +1,9 @@
import { RowDataPacket } from "mysql2";
import { promisePool } from "@/backend/db";
import { getPromisePool } from "@/backend/db";
export async function getPost(slug: string): Promise<string> {
try {
const [rows, fields] = await promisePool.query<RowDataPacket[]>(
const [rows, fields] = await getPromisePool().query<RowDataPacket[]>(
'select content from post where slug = ?', [slug])
return rows[0]['content']
} catch (e) {