Updates
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import grabDSQLConnection from "../../utils/grab-dsql-connection";
|
||||
import { DSQL_TableSchemaType } from "../../types";
|
||||
import { DSQL_TableSchemaType, PostInsertReturn } from "../../types";
|
||||
import { Connection, ConnectionConfig } from "mariadb";
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
|
||||
type Param<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
query: string;
|
||||
@@ -18,7 +19,7 @@ type Param<T extends { [k: string]: any } = { [k: string]: any }> = {
|
||||
* @requires DSQL_DB_CONN - Gobal Variable for Datasquirel Database
|
||||
*/
|
||||
export default async function dbHandler<
|
||||
T extends { [k: string]: any } = { [k: string]: any }
|
||||
T extends { [k: string]: any } = PostInsertReturn
|
||||
>({
|
||||
query,
|
||||
values,
|
||||
@@ -26,21 +27,23 @@ export default async function dbHandler<
|
||||
database,
|
||||
config,
|
||||
}: Param<T>): Promise<T[] | T | null> {
|
||||
let CONNECTION: Connection | undefined;
|
||||
let CONNECTION: ServerlessMysql | undefined;
|
||||
let results: T[] | T | null;
|
||||
|
||||
try {
|
||||
CONNECTION = await grabDSQLConnection({ database, config });
|
||||
|
||||
if (query && values) {
|
||||
const queryResults = await CONNECTION.query(query, values);
|
||||
results = queryResults[0];
|
||||
const queryResults = (await CONNECTION.query(query, values)) as
|
||||
| T
|
||||
| T[];
|
||||
results = queryResults;
|
||||
} else {
|
||||
const queryResults = await CONNECTION.query(query);
|
||||
results = queryResults[0];
|
||||
const queryResults = (await CONNECTION.query(query)) as T | T[];
|
||||
results = queryResults;
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log("Connection Info =>", CONNECTION?.info);
|
||||
console.log("Connection Info =>", CONNECTION?.getConfig());
|
||||
|
||||
if (
|
||||
error.message &&
|
||||
|
||||
@@ -16,7 +16,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const [result] = await CONNECTION.query(
|
||||
const result = await CONNECTION.query(
|
||||
"SELECT id,first_name,last_name FROM users LIMIT 3"
|
||||
);
|
||||
console.log("Connection Query Success =>", result);
|
||||
|
||||
@@ -21,7 +21,7 @@ import grabDSQLConnection from "../utils/grab-dsql-connection";
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
try {
|
||||
const [result] = await CONNECTION.query("SHOW DATABASES");
|
||||
const result = await CONNECTION.query("SHOW DATABASES");
|
||||
|
||||
const parsedResults = JSON.parse(JSON.stringify(result));
|
||||
|
||||
|
||||
@@ -17,9 +17,9 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
const CONNECTION = await grabDSQLConnection();
|
||||
|
||||
try {
|
||||
const [result] = await CONNECTION.query(
|
||||
const result = (await CONNECTION.query(
|
||||
"SELECT user,host,ssl_type FROM mysql.user"
|
||||
);
|
||||
)) as any[];
|
||||
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
const user = result[i];
|
||||
@@ -38,7 +38,7 @@ import grabSQLKeyName from "../utils/grab-sql-key-name";
|
||||
continue;
|
||||
}
|
||||
|
||||
const [addUserSSL] = await CONNECTION.query(
|
||||
const addUserSSL = await CONNECTION.query(
|
||||
`ALTER USER '${User}'@'${Host}'`
|
||||
);
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ import fs from "fs";
|
||||
import grabDirNames from "./names/grab-dir-names";
|
||||
import type { ConnectionConfig } from "mariadb";
|
||||
import path from "path";
|
||||
import serverlessMysql from "serverless-mysql";
|
||||
import { ConnectionOptions } from "tls";
|
||||
|
||||
type Return = ConnectionConfig["ssl"] | undefined;
|
||||
// type Return = ConnectionConfig["ssl"] | undefined;
|
||||
|
||||
/**
|
||||
* # Grab SSL
|
||||
*/
|
||||
export default function grabDbSSL(): Return {
|
||||
export default function grabDbSSL(): any {
|
||||
let maxscaleSSLCaCertFileFinal;
|
||||
|
||||
try {
|
||||
|
||||
@@ -43,10 +43,10 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
let queryErrorArray: DSQLErrorObject[] = [];
|
||||
|
||||
if (typeof query == "string") {
|
||||
const [results] = await finalConnection.query(
|
||||
const results = (await finalConnection.query(
|
||||
trimQuery(query),
|
||||
values
|
||||
);
|
||||
)) as any;
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
@@ -74,7 +74,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
queryObj.values
|
||||
);
|
||||
|
||||
const results = queryObjRes[0];
|
||||
const results = queryObjRes;
|
||||
|
||||
if (debug) {
|
||||
debugLog({
|
||||
@@ -106,7 +106,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
};
|
||||
}
|
||||
|
||||
return resArray;
|
||||
return resArray as ReturnType;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import mysql from "mysql";
|
||||
|
||||
/**
|
||||
* # End MYSQL Connection
|
||||
*/
|
||||
function endConnection(connection: mysql.Connection) {
|
||||
if (connection.state !== "disconnected") {
|
||||
connection.end((err) => {
|
||||
console.log(err?.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default endConnection;
|
||||
@@ -1,37 +1,43 @@
|
||||
import { ConnectionConfig } from "mariadb";
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
import { DsqlConnectionParam } from "../types";
|
||||
import serverlessMysql from "serverless-mysql";
|
||||
import { ConnectionOptions } from "tls";
|
||||
import _ from "lodash";
|
||||
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default function grabDSQLConnectionConfig(
|
||||
param?: DsqlConnectionParam
|
||||
): ConnectionConfig {
|
||||
): serverlessMysql.Config {
|
||||
const CONN_TIMEOUT = 10000;
|
||||
|
||||
const config: ConnectionConfig = {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database:
|
||||
param?.database ||
|
||||
(param?.noDb ? undefined : process.env.DSQL_DB_NAME),
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
bigIntAsNumber: true,
|
||||
supportBigNumbers: true,
|
||||
bigNumberStrings: false,
|
||||
dateStrings: true,
|
||||
metaAsArray: true,
|
||||
socketTimeout: CONN_TIMEOUT,
|
||||
connectTimeout: CONN_TIMEOUT,
|
||||
compress: true,
|
||||
...param?.config,
|
||||
const config: serverlessMysql.Config = {
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database:
|
||||
param?.database ||
|
||||
(param?.noDb ? undefined : process.env.DSQL_DB_NAME),
|
||||
port: process.env.DSQL_DB_PORT
|
||||
? Number(process.env.DSQL_DB_PORT)
|
||||
: undefined,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
connectTimeout: CONN_TIMEOUT,
|
||||
dateStrings: true,
|
||||
compress: true,
|
||||
decimalNumbers: true,
|
||||
// supportBigNumbers: true,
|
||||
// bigNumberStrings: false,
|
||||
// bigIntAsNumber: true,
|
||||
// metaAsArray: true,
|
||||
// socketTimeout: CONN_TIMEOUT,
|
||||
// ...param?.config,
|
||||
},
|
||||
};
|
||||
|
||||
return config;
|
||||
return _.merge(config, param?.config);
|
||||
}
|
||||
|
||||
@@ -2,17 +2,19 @@ import mariadb, { Connection, ConnectionConfig } from "mariadb";
|
||||
import grabDbSSL from "./backend/grabDbSSL";
|
||||
import { DsqlConnectionParam } from "../types";
|
||||
import grabDSQLConnectionConfig from "./grab-dsql-connection-config";
|
||||
import serverlessMysql, { ServerlessMysql } from "serverless-mysql";
|
||||
|
||||
/**
|
||||
* # Grab General CONNECTION for DSQL
|
||||
*/
|
||||
export default async function grabDSQLConnection(
|
||||
param?: DsqlConnectionParam
|
||||
): Promise<Connection> {
|
||||
): Promise<ServerlessMysql> {
|
||||
const config = grabDSQLConnectionConfig(param);
|
||||
|
||||
try {
|
||||
return await mariadb.createConnection(config);
|
||||
const sql = serverlessMysql(config);
|
||||
return sql;
|
||||
} catch (error) {
|
||||
console.log(`Error Grabbing DSQL Connection =>`, config);
|
||||
throw error;
|
||||
|
||||
Reference in New Issue
Block a user