This commit is contained in:
Benjamin Toby 2025-02-12 12:12:11 +01:00
parent ca65327527
commit e089f84ef4
13 changed files with 34 additions and 73 deletions

View File

@ -31,12 +31,8 @@ function get(_a) {
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } = process.env;
if ((DSQL_DB_HOST === null || DSQL_DB_HOST === void 0 ? void 0 : DSQL_DB_HOST.match(/./)) &&
(DSQL_DB_USERNAME === null || DSQL_DB_USERNAME === void 0 ? void 0 : DSQL_DB_USERNAME.match(/./)) &&
(DSQL_DB_PASSWORD === null || DSQL_DB_PASSWORD === void 0 ? void 0 : DSQL_DB_PASSWORD.match(/./)) &&
(DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) &&
global.DSQL_USE_LOCAL) {
const { DSQL_DB_NAME } = process.env;
if ((DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) && global.DSQL_USE_LOCAL) {
let dbSchema;
try {
const localDbSchemaPath = path_1.default.resolve(process.cwd(), "dsql.schema.json");

View File

@ -9,8 +9,12 @@ type Param = {
queryValuesArray?: (string | number)[];
tableName?: string;
};
type Return = {
result: any;
error?: string;
};
/**
* # Run DSQL users queries
*/
export default function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName, debug, dbContext, }: Param): Promise<any>;
export default function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName, debug, dbContext, }: Param): Promise<Return>;
export {};

View File

@ -106,7 +106,7 @@ function runQuery(_a) {
tableSchema,
});
if (!(result === null || result === void 0 ? void 0 : result.insertId)) {
error = new Error("Couldn't insert data");
error = "Couldn't insert data";
}
break;
case "update":
@ -136,16 +136,16 @@ function runQuery(_a) {
}
}
}
catch (error) {
catch (err) {
(0, serverError_1.default)({
component: "functions/backend/runQuery",
message: error.message,
message: err.message,
});
if (debug && global.DSQL_USE_LOCAL) {
console.log("runQuery:error", error.message);
console.log("runQuery:error", err.message);
}
result = null;
error = error.message;
error = err.message;
}
return { result, error };
});

View File

@ -22,42 +22,22 @@ const conn_db_handler_1 = __importDefault(require("../../utils/db/conn-db-handle
*/
function varReadOnlyDatabaseDbHandler(_a) {
return __awaiter(this, arguments, void 0, function* ({ queryString, queryValuesArray, tableSchema, }) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = yield (0, conn_db_handler_1.default)(DB_CONN, queryString, queryValuesArray);
////////////////////////////////////////
}
catch (error) {
////////////////////////////////////////
(0, serverError_1.default)({
component: "varReadOnlyDatabaseDbHandler",
message: error.message,
noMail: true,
});
/**
* Return error
*/
return error.message;
}
finally {
DB_CONN === null || DB_CONN === void 0 ? void 0 : DB_CONN.end();
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
const unparsedResults = results;
const parsedResults = yield (0, parseDbResults_1.default)({

View File

@ -1349,5 +1349,6 @@ export type DsqlCrudParam<T extends {
targetId?: string | number;
query?: DsqlCrudQueryObject<T>;
sanitize?: (data?: T) => T;
debug?: boolean;
};
export {};

View File

@ -4,6 +4,6 @@ export default function dsqlCrud<T extends {
[key: string]: any;
} = {
[key: string]: any;
}>({ action, data, table, targetId, query, sanitize, }: DsqlCrudParam<T>): Promise<(PostReturn & {
}>({ action, data, table, targetId, query, sanitize, debug, }: DsqlCrudParam<T>): Promise<(PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
}) | null>;

View File

@ -17,7 +17,7 @@ const get_1 = __importDefault(require("../../actions/get"));
const post_1 = __importDefault(require("../../actions/post"));
const sql_generator_1 = __importDefault(require("../../functions/dsql/sql/sql-generator"));
function dsqlCrud(_a) {
return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetId, query, sanitize, }) {
return __awaiter(this, arguments, void 0, function* ({ action, data, table, targetId, query, sanitize, debug, }) {
const finalData = sanitize ? sanitize(data) : data;
const finalId = targetId;
let queryObject;
@ -30,6 +30,7 @@ function dsqlCrud(_a) {
const GET_RES = yield (0, get_1.default)({
query: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.string) || "",
queryValues: (queryObject === null || queryObject === void 0 ? void 0 : queryObject.values) || [],
debug,
});
return Object.assign(Object.assign({}, GET_RES), { queryObject });
case "insert":

View File

@ -46,16 +46,9 @@ export default async function get<
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } =
process.env;
const { DSQL_DB_NAME } = process.env;
if (
DSQL_DB_HOST?.match(/./) &&
DSQL_DB_USERNAME?.match(/./) &&
DSQL_DB_PASSWORD?.match(/./) &&
DSQL_DB_NAME?.match(/./) &&
global.DSQL_USE_LOCAL
) {
if (DSQL_DB_NAME?.match(/./) && global.DSQL_USE_LOCAL) {
let dbSchema: DSQL_DatabaseSchemaType | undefined;
try {

View File

@ -20,6 +20,11 @@ type Param = {
tableName?: string;
};
type Return = {
result: any;
error?: string;
};
/**
* # Run DSQL users queries
*/
@ -32,7 +37,7 @@ export default async function runQuery({
tableName,
debug,
dbContext,
}: Param): Promise<any> {
}: Param): Promise<Return> {
/**
* Declare variables
*
@ -40,7 +45,7 @@ export default async function runQuery({
*/
let result: any;
let error: any;
let error: string | undefined;
let tableSchema: DSQL_TableSchemaType | undefined;
if (dbSchema) {
@ -127,7 +132,7 @@ export default async function runQuery({
});
if (!result?.insertId) {
error = new Error("Couldn't insert data");
error = "Couldn't insert data";
}
break;
@ -162,18 +167,18 @@ export default async function runQuery({
break;
}
}
} catch (error: any) {
} catch (err: any) {
serverError({
component: "functions/backend/runQuery",
message: error.message,
message: err.message,
});
if (debug && global.DSQL_USE_LOCAL) {
console.log("runQuery:error", error.message);
console.log("runQuery:error", err.message);
}
result = null;
error = error.message;
error = err.message;
}
return { result, error };

View File

@ -17,46 +17,24 @@ export default async function varReadOnlyDatabaseDbHandler({
queryValuesArray,
tableSchema,
}: Param) {
/**
* Declare variables
*
* @description Declare "results" variable
*/
let results;
const DB_CONN = global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
/**
* Fetch from db
*
* @description Fetch data from db if no cache
*/
try {
results = await connDbHandler(DB_CONN, queryString, queryValuesArray);
////////////////////////////////////////
} catch (error: any) {
////////////////////////////////////////
serverError({
component: "varReadOnlyDatabaseDbHandler",
message: error.message,
noMail: true,
});
/**
* Return error
*/
return error.message;
} finally {
DB_CONN?.end();
}
/**
* Return results
*
* @description Return results add to cache if "req" param is passed
*/
if (results) {
const unparsedResults = results;
const parsedResults = await parseDbResults({

View File

@ -1521,4 +1521,5 @@ export type DsqlCrudParam<
targetId?: string | number;
query?: DsqlCrudQueryObject<T>;
sanitize?: (data?: T) => T;
debug?: boolean;
};

View File

@ -12,6 +12,7 @@ export default async function dsqlCrud<
targetId,
query,
sanitize,
debug,
}: DsqlCrudParam<T>): Promise<
| (PostReturn & {
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
@ -32,6 +33,7 @@ export default async function dsqlCrud<
const GET_RES = await get({
query: queryObject?.string || "",
queryValues: queryObject?.values || [],
debug,
});
return { ...GET_RES, queryObject };

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "4.0.7",
"version": "4.0.8",
"description": "Cloud-based SQL data management tool",
"main": "dist/index.js",
"bin": {