correzione in connection.ts
This commit is contained in:
parent
c5db715e4c
commit
71ed95251c
1 changed files with 28 additions and 18 deletions
|
|
@ -34,7 +34,8 @@ function readNumberEnv(name: string, fallback: number): number {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const poolConfig: PoolOptions = {
|
function createPoolConfig(): PoolOptions {
|
||||||
|
return {
|
||||||
host: readEnv("MYSQL_HOST", "127.0.0.1"),
|
host: readEnv("MYSQL_HOST", "127.0.0.1"),
|
||||||
port: readNumberEnv("MYSQL_PORT", 3306),
|
port: readNumberEnv("MYSQL_PORT", 3306),
|
||||||
user: readEnv("MYSQL_USER"),
|
user: readEnv("MYSQL_USER"),
|
||||||
|
|
@ -46,15 +47,24 @@ const poolConfig: PoolOptions = {
|
||||||
namedPlaceholders: true,
|
namedPlaceholders: true,
|
||||||
timezone: "Z",
|
timezone: "Z",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const db = globalThis.magRicambiMysqlPool ?? mysql.createPool(poolConfig);
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== "production") {
|
|
||||||
globalThis.magRicambiMysqlPool = db;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPool(): Pool {
|
||||||
|
if (!globalThis.magRicambiMysqlPool) {
|
||||||
|
globalThis.magRicambiMysqlPool = mysql.createPool(createPoolConfig());
|
||||||
|
}
|
||||||
|
|
||||||
|
return globalThis.magRicambiMysqlPool;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const db = new Proxy({} as Pool, {
|
||||||
|
get(_target, property, receiver) {
|
||||||
|
return Reflect.get(getPool(), property, receiver);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
export async function getDbConnection(): Promise<PoolConnection> {
|
export async function getDbConnection(): Promise<PoolConnection> {
|
||||||
return db.getConnection();
|
return getPool().getConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function testDbConnection(): Promise<void> {
|
export async function testDbConnection(): Promise<void> {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue