76 lines
2.0 KiB
TypeScript
76 lines
2.0 KiB
TypeScript
|
import mysql, { ServerlessMysql } from "serverless-mysql";
|
||
|
|
||
|
type Param = {
|
||
|
/**
|
||
|
* Read Only?
|
||
|
*/
|
||
|
ro?: boolean;
|
||
|
/**
|
||
|
* Full Access?
|
||
|
*/
|
||
|
fa?: boolean;
|
||
|
/**
|
||
|
* No Database Connection
|
||
|
*/
|
||
|
noDb?: boolean;
|
||
|
/**
|
||
|
* Is this a local connection?
|
||
|
*/
|
||
|
local?: boolean;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* # Grab General CONNECTION for DSQL
|
||
|
*/
|
||
|
export default function grabDSQLConnection(param?: Param): ServerlessMysql {
|
||
|
if (param?.ro) {
|
||
|
return (
|
||
|
DSQL_READ_ONLY_DB_CONN ||
|
||
|
mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_READ_ONLY_USERNAME,
|
||
|
password: process.env.DSQL_DB_READ_ONLY_PASSWORD,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
},
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (param?.fa) {
|
||
|
return (
|
||
|
global.DSQL_FULL_ACCESS_DB_CONN ||
|
||
|
mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_FULL_ACCESS_USERNAME,
|
||
|
password: process.env.DSQL_DB_FULL_ACCESS_PASSWORD,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
},
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
return (
|
||
|
global.DSQL_DB_CONN ||
|
||
|
mysql({
|
||
|
config: {
|
||
|
host: process.env.DSQL_DB_HOST,
|
||
|
user: process.env.DSQL_DB_USERNAME,
|
||
|
password: process.env.DSQL_DB_PASSWORD,
|
||
|
database: param?.noDb ? undefined : process.env.DSQL_DB_NAME,
|
||
|
port: process.env.DSQL_DB_PORT
|
||
|
? Number(process.env.DSQL_DB_PORT)
|
||
|
: undefined,
|
||
|
charset: "utf8mb4",
|
||
|
},
|
||
|
})
|
||
|
);
|
||
|
}
|