Updates
This commit is contained in:
@@ -8,6 +8,7 @@ type Param<T extends { [key: string]: any } = { [key: string]: any }> = {
|
||||
genObject?: ServerQueryParam<T>;
|
||||
tableName: string;
|
||||
dbFullName?: string;
|
||||
count?: boolean;
|
||||
};
|
||||
|
||||
type Return = {
|
||||
@@ -21,7 +22,7 @@ type Return = {
|
||||
*/
|
||||
export default function sqlGenerator<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
>({ tableName, genObject, dbFullName }: Param<T>): Return {
|
||||
>({ tableName, genObject, dbFullName, count }: Param<T>): Return {
|
||||
const finalQuery = genObject?.query ? genObject.query : undefined;
|
||||
|
||||
const queryKeys = finalQuery ? Object.keys(finalQuery) : undefined;
|
||||
@@ -162,7 +163,10 @@ export default function sqlGenerator<
|
||||
|
||||
let queryString = (() => {
|
||||
let str = "SELECT";
|
||||
if (genObject?.selectFields?.[0]) {
|
||||
|
||||
if (count) {
|
||||
str += ` COUNT(*)`;
|
||||
} else if (genObject?.selectFields?.[0]) {
|
||||
if (genObject.join) {
|
||||
str += ` ${genObject.selectFields
|
||||
?.map((fld) => `${finalDbName}${tableName}.${fld}`)
|
||||
@@ -178,7 +182,7 @@ export default function sqlGenerator<
|
||||
}
|
||||
}
|
||||
|
||||
if (genObject?.join) {
|
||||
if (genObject?.join && !count) {
|
||||
const existingJoinTableNames: string[] = [tableName];
|
||||
|
||||
str +=
|
||||
@@ -264,15 +268,16 @@ export default function sqlGenerator<
|
||||
queryString += ` WHERE ${sqlSearhString.join(` ${stringOperator} `)}`;
|
||||
}
|
||||
|
||||
if (genObject?.order)
|
||||
if (genObject?.order && !count)
|
||||
queryString += ` ORDER BY ${
|
||||
genObject.join
|
||||
? `${finalDbName}${tableName}.${String(genObject.order.field)}`
|
||||
: String(genObject.order.field)
|
||||
} ${genObject.order.strategy}`;
|
||||
|
||||
if (genObject?.limit) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset) queryString += ` OFFSET ${genObject.offset}`;
|
||||
if (genObject?.limit && !count) queryString += ` LIMIT ${genObject.limit}`;
|
||||
if (genObject?.offset && !count)
|
||||
queryString += ` OFFSET ${genObject.offset}`;
|
||||
|
||||
return {
|
||||
string: queryString,
|
||||
|
||||
@@ -75,15 +75,17 @@ export default async function createDbFromSchema({
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
|
||||
const targetTableSchema = tables.find(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (
|
||||
!tables.filter((_table) => _table.tableName === TABLE_NAME)[0]
|
||||
) {
|
||||
const oldTableFilteredArray = tables.filter(
|
||||
if (!targetTableSchema) {
|
||||
const oldTable = tables.find(
|
||||
(_table) =>
|
||||
_table.tableNameOld &&
|
||||
_table.tableNameOld === TABLE_NAME
|
||||
@@ -93,10 +95,10 @@ export default async function createDbFromSchema({
|
||||
* @description Check if this table has been recently renamed. Rename
|
||||
* table id true. Drop table if false
|
||||
*/
|
||||
if (oldTableFilteredArray && oldTableFilteredArray[0]) {
|
||||
if (oldTable) {
|
||||
console.log("Renaming Table");
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``,
|
||||
queryString: `RENAME TABLE \`${dbFullName}\`.\`${oldTable.tableNameOld}\` TO \`${oldTable.tableName}\``,
|
||||
});
|
||||
} else {
|
||||
console.log(`Dropping Table from ${dbFullName}`);
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
import type { RequestOptions } from "https";
|
||||
import { DSQL_DATASQUIREL_PROCESS_QUEUE } from "./dsql";
|
||||
import {
|
||||
DSQL_DATASQUIREL_PROCESS_QUEUE,
|
||||
DSQL_DATASQUIREL_USER_DATABASES,
|
||||
} from "./dsql";
|
||||
|
||||
import { Editor } from "tinymce";
|
||||
export type DSQL_DatabaseFullName = string;
|
||||
@@ -1542,6 +1545,7 @@ export type DsqlCrudParam<
|
||||
query?: DsqlCrudQueryObject<T>;
|
||||
sanitize?: (data?: T) => T;
|
||||
debug?: boolean;
|
||||
count?: boolean;
|
||||
};
|
||||
|
||||
export type ErrorCallback = (title: string, error: Error, data?: any) => void;
|
||||
@@ -1600,6 +1604,7 @@ export type PagePropsType = {
|
||||
user?: UserType | null;
|
||||
pageUrl?: string | null;
|
||||
query?: any;
|
||||
databases?: DSQL_DATASQUIREL_USER_DATABASES[] | null;
|
||||
};
|
||||
|
||||
export type APIResponseObject<T extends any = any> = {
|
||||
|
||||
@@ -71,12 +71,21 @@ export default function grabDirNames(param?: Param) {
|
||||
"docker/services/mariadb/load-balancer/config/template/nginx.conf"
|
||||
);
|
||||
|
||||
const dockerComposeFile = path.join(appDir, "docker-compose.yml");
|
||||
let dockerComposeFile = path.join(appDir, "docker-compose.yml");
|
||||
let dockerComposeFileAlt = path.join(appDir, "docker-compose.yaml");
|
||||
const testDockerComposeFile = path.join(appDir, "test.docker-compose.yml");
|
||||
const testDockerComposeFileAlt = path.join(
|
||||
appDir,
|
||||
"test.docker-compose.yaml"
|
||||
);
|
||||
const extraDockerComposeFile = path.join(
|
||||
appDir,
|
||||
"extra.docker-compose.yml"
|
||||
);
|
||||
const extraDockerComposeFileAlt = path.join(
|
||||
appDir,
|
||||
"extra.docker-compose.yaml"
|
||||
);
|
||||
|
||||
const siteSetupFile = path.join(appDir, "site-setup.json");
|
||||
|
||||
@@ -104,8 +113,11 @@ export default function grabDirNames(param?: Param) {
|
||||
userPrivateDbImportZipFilePath,
|
||||
dbNginxLoadBalancerConfigFile,
|
||||
dockerComposeFile,
|
||||
dockerComposeFileAlt,
|
||||
testDockerComposeFile,
|
||||
testDockerComposeFileAlt,
|
||||
extraDockerComposeFile,
|
||||
extraDockerComposeFileAlt,
|
||||
siteSetupFile,
|
||||
envFile,
|
||||
testEnvFile,
|
||||
|
||||
@@ -2,6 +2,7 @@ import get from "../../actions/get";
|
||||
import post from "../../actions/post";
|
||||
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
|
||||
import { DsqlCrudParam, PostReturn } from "../../types";
|
||||
import connDbHandler, { ConnDBHandlerQueryObject } from "../db/conn-db-handler";
|
||||
|
||||
export default async function dsqlCrud<
|
||||
T extends { [key: string]: any } = { [key: string]: any }
|
||||
@@ -15,9 +16,11 @@ export default async function dsqlCrud<
|
||||
debug,
|
||||
targetField,
|
||||
targetId,
|
||||
count,
|
||||
}: DsqlCrudParam<T>): Promise<
|
||||
| (PostReturn & {
|
||||
queryObject?: ReturnType<Awaited<typeof sqlGenerator>>;
|
||||
count?: number;
|
||||
})
|
||||
| null
|
||||
> {
|
||||
@@ -31,14 +34,43 @@ export default async function dsqlCrud<
|
||||
genObject: query,
|
||||
});
|
||||
|
||||
const GET_RES = await get({
|
||||
query: queryObject?.string || "",
|
||||
queryValues: queryObject?.values || [],
|
||||
debug,
|
||||
forceLocal: true,
|
||||
});
|
||||
const DB_CONN =
|
||||
global.DSQL_READ_ONLY_DB_CONN || global.DSQL_DB_CONN;
|
||||
|
||||
return { ...GET_RES, queryObject };
|
||||
const connQueries: ConnDBHandlerQueryObject[] = [
|
||||
{
|
||||
query: queryObject?.string,
|
||||
values: queryObject?.values || [],
|
||||
},
|
||||
];
|
||||
|
||||
if (count) {
|
||||
const countQueryObject = sqlGenerator({
|
||||
tableName: table,
|
||||
genObject: query,
|
||||
count: true,
|
||||
});
|
||||
|
||||
connQueries.push({
|
||||
query: countQueryObject.string,
|
||||
values: countQueryObject.values,
|
||||
});
|
||||
}
|
||||
|
||||
const res = await connDbHandler(DB_CONN, connQueries);
|
||||
|
||||
const isSuccess = Array.isArray(res) && Array.isArray(res[0]);
|
||||
|
||||
return {
|
||||
success: isSuccess,
|
||||
payload: isSuccess ? res[0] : null,
|
||||
error: isSuccess ? undefined : res?.error,
|
||||
queryObject,
|
||||
count:
|
||||
isSuccess && res[1]?.[0]?.["COUNT(*)"]
|
||||
? res[1][0]["COUNT(*)"]
|
||||
: undefined,
|
||||
};
|
||||
|
||||
case "insert":
|
||||
return await post({
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ServerlessMysql } from "serverless-mysql";
|
||||
import debugLog from "../logging/debug-log";
|
||||
|
||||
type QueryObject = {
|
||||
export type ConnDBHandlerQueryObject = {
|
||||
query: string;
|
||||
values?: (string | number | undefined)[];
|
||||
};
|
||||
@@ -19,13 +19,13 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
*/
|
||||
conn?: ServerlessMysql,
|
||||
/**
|
||||
* String Or `QueryObject` Array
|
||||
* String Or `ConnDBHandlerQueryObject` Array
|
||||
*/
|
||||
query?: QueryObject["query"] | QueryObject[],
|
||||
query?: ConnDBHandlerQueryObject["query"] | ConnDBHandlerQueryObject[],
|
||||
/**
|
||||
* Array of Values to Sanitize and Inject
|
||||
*/
|
||||
values?: QueryObject["values"],
|
||||
values?: ConnDBHandlerQueryObject["values"],
|
||||
debug?: boolean
|
||||
): Promise<Return<ReturnType>> {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user