Updates
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { execSync, ExecSyncOptions } from "child_process";
|
||||
import os from "os";
|
||||
|
||||
export type ExportMariaDBDatabaseParam = {
|
||||
dbFullName: string;
|
||||
targetFilePath: string;
|
||||
mariadbUser?: string;
|
||||
mariadbHost?: string;
|
||||
mariadbPass?: string;
|
||||
};
|
||||
|
||||
export default function exportMariadbDatabase({
|
||||
dbFullName,
|
||||
targetFilePath,
|
||||
mariadbHost,
|
||||
mariadbPass,
|
||||
mariadbUser,
|
||||
}: ExportMariaDBDatabaseParam) {
|
||||
const mysqlDumpPath = os.platform().match(/win/i)
|
||||
? "'" +
|
||||
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump.exe" +
|
||||
"'"
|
||||
: "mysqldump";
|
||||
|
||||
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
|
||||
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
|
||||
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
|
||||
|
||||
const cmd = `${mysqlDumpPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} > ${targetFilePath}`;
|
||||
|
||||
let execSyncOptions: ExecSyncOptions = {
|
||||
encoding: "utf-8",
|
||||
};
|
||||
|
||||
const dumpDb = execSync(cmd, execSyncOptions);
|
||||
|
||||
return dumpDb;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import datasquirel from "@moduletrace/datasquirel";
|
||||
import { execSync, ExecSyncOptions } from "child_process";
|
||||
import os from "os";
|
||||
|
||||
export type ExportMariaDBDatabaseParam = {
|
||||
dbFullName: string;
|
||||
targetFilePath: string;
|
||||
mariadbUser?: string;
|
||||
mariadbHost?: string;
|
||||
mariadbPass?: string;
|
||||
};
|
||||
|
||||
export default async function importMariadbDatabase({
|
||||
dbFullName,
|
||||
targetFilePath,
|
||||
mariadbHost,
|
||||
mariadbPass,
|
||||
mariadbUser,
|
||||
}: ExportMariaDBDatabaseParam) {
|
||||
const mysqlPath = os.platform().match(/win/i)
|
||||
? "'" +
|
||||
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysql.exe" +
|
||||
"'"
|
||||
: "mysql";
|
||||
|
||||
const finalMariadbUser = mariadbUser || process.env.DSQL_DB_USERNAME;
|
||||
const finalMariadbHost = mariadbHost || process.env.DSQL_DB_HOST;
|
||||
const finalMariadbPass = mariadbPass || process.env.DSQL_DB_PASSWORD;
|
||||
|
||||
await datasquirel.utils.connDbHandler(
|
||||
global.DSQL_DB_CONN,
|
||||
`CREATE DATABASE IF NOT EXISTS ${dbFullName}`
|
||||
);
|
||||
|
||||
const cmd = `${mysqlPath} -u ${finalMariadbUser} -h ${finalMariadbHost} -p${finalMariadbPass} ${dbFullName} < ${targetFilePath}`;
|
||||
|
||||
let execSyncOptions: ExecSyncOptions = {
|
||||
encoding: "utf-8",
|
||||
};
|
||||
|
||||
const importDb = execSync(cmd, execSyncOptions);
|
||||
|
||||
return importDb;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import { DATASQUIREL_LoggedInUser, UserType } from "../../../types";
|
||||
import path from "path";
|
||||
|
||||
type Param = {
|
||||
user?: DATASQUIREL_LoggedInUser | UserType;
|
||||
userId?: string | number | null;
|
||||
};
|
||||
export default function grabDirNames(param?: Param) {
|
||||
const appDir = process.env.DSQL_APP_DIR;
|
||||
const schemasDir = process.env.DSQL_DB_SCHEMA_DIR;
|
||||
const tempDirName = ".tmp";
|
||||
|
||||
if (!appDir)
|
||||
throw new Error("Please provide the `DSQL_APP_DIR` env variable.");
|
||||
|
||||
if (!schemasDir)
|
||||
throw new Error(
|
||||
"Please provide the `DSQL_DB_SCHEMA_DIR` env variable."
|
||||
);
|
||||
|
||||
const pakageSharedDir = path.join(appDir, `package-shared`);
|
||||
|
||||
const mainDbTypeDefFile = path.join(pakageSharedDir, `types/dsql.ts`);
|
||||
const mainShemaJSONFilePath = path.join(schemasDir, `main.json`);
|
||||
const defaultTableFieldsJSONFilePath = path.join(
|
||||
pakageSharedDir,
|
||||
`data/defaultFields.json`
|
||||
);
|
||||
|
||||
const usersSchemaDir = path.join(schemasDir, `users`);
|
||||
|
||||
const userDirPath = param?.user?.id
|
||||
? path.join(usersSchemaDir, `user-${param.user.id}`)
|
||||
: param?.userId
|
||||
? path.join(usersSchemaDir, `user-${param.userId}`)
|
||||
: undefined;
|
||||
const userSchemaMainJSONFilePath = userDirPath
|
||||
? path.join(userDirPath, `main.json`)
|
||||
: undefined;
|
||||
const userPrivateMediaDir = userDirPath
|
||||
? path.join(userDirPath, `media`)
|
||||
: undefined;
|
||||
const userPrivateExportsDir = userDirPath
|
||||
? path.join(userDirPath, `export`)
|
||||
: undefined;
|
||||
const userPrivateSQLExportsDir = userPrivateExportsDir
|
||||
? path.join(userPrivateExportsDir, `sql`)
|
||||
: undefined;
|
||||
const userPrivateTempSQLExportsDir = userPrivateSQLExportsDir
|
||||
? path.join(userPrivateSQLExportsDir, tempDirName)
|
||||
: undefined;
|
||||
const userPrivateTempJSONSchemaFilePath = userPrivateTempSQLExportsDir
|
||||
? path.join(userPrivateTempSQLExportsDir, `schema.json`)
|
||||
: undefined;
|
||||
const userPrivateDbExportZipFileName = `db-export.zip`;
|
||||
const userPrivateDbExportZipFilePath = userPrivateSQLExportsDir
|
||||
? path.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
|
||||
: undefined;
|
||||
|
||||
const userPrivateDbImportZipFileName = `db-export.zip`;
|
||||
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
|
||||
? path.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
|
||||
: undefined;
|
||||
|
||||
return {
|
||||
schemasDir,
|
||||
userDirPath,
|
||||
mainShemaJSONFilePath,
|
||||
mainDbTypeDefFile,
|
||||
tempDirName,
|
||||
defaultTableFieldsJSONFilePath,
|
||||
usersSchemaDir,
|
||||
userSchemaMainJSONFilePath,
|
||||
userPrivateMediaDir,
|
||||
userPrivateExportsDir,
|
||||
userPrivateSQLExportsDir,
|
||||
userPrivateTempSQLExportsDir,
|
||||
userPrivateTempJSONSchemaFilePath,
|
||||
userPrivateDbExportZipFileName,
|
||||
userPrivateDbExportZipFilePath,
|
||||
userPrivateDbImportZipFileName,
|
||||
userPrivateDbImportZipFilePath,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
type Param = {
|
||||
str: string;
|
||||
userId: string | number;
|
||||
};
|
||||
export default function replaceDatasquirelDbName({
|
||||
str,
|
||||
userId,
|
||||
}: Param): string {
|
||||
const dbNamePrefix = process.env.DSQL_USER_DB_PREFIX;
|
||||
const userNameRegex = new RegExp(`${dbNamePrefix}\\d+_`, "g");
|
||||
const newPrefix = `${dbNamePrefix}${userId}_`;
|
||||
|
||||
return str.replace(userNameRegex, newPrefix);
|
||||
}
|
||||
@@ -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,8 @@ export default async function dsqlCrud<
|
||||
const GET_RES = await get({
|
||||
query: queryObject?.string || "",
|
||||
queryValues: queryObject?.values || [],
|
||||
debug,
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
return { ...GET_RES, queryObject };
|
||||
@@ -43,6 +46,7 @@ export default async function dsqlCrud<
|
||||
table,
|
||||
data: finalData,
|
||||
},
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
case "update":
|
||||
@@ -56,6 +60,7 @@ export default async function dsqlCrud<
|
||||
identifierValue: String(finalId),
|
||||
data: finalData,
|
||||
},
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
case "delete":
|
||||
@@ -66,6 +71,7 @@ export default async function dsqlCrud<
|
||||
identifierColumnName: "id",
|
||||
identifierValue: String(finalId),
|
||||
},
|
||||
forceLocal: true,
|
||||
});
|
||||
|
||||
default:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../functions/dsql/sql/sql-generator";
|
||||
import {
|
||||
DsqlCrudQueryObject,
|
||||
@@ -30,12 +31,14 @@ export default async function dsqlMethodCrud<
|
||||
addUser,
|
||||
user,
|
||||
extraData,
|
||||
transform,
|
||||
transformData,
|
||||
existingData,
|
||||
body,
|
||||
query,
|
||||
targetId,
|
||||
sanitize,
|
||||
transformQuery,
|
||||
debug,
|
||||
}: DsqlMethodCrudParam<T>): Promise<CRUDResponseObject<P>> {
|
||||
let result: CRUDResponseObject = {
|
||||
success: false,
|
||||
@@ -51,23 +54,28 @@ export default async function dsqlMethodCrud<
|
||||
let PAGE = 1;
|
||||
let OFFSET = (PAGE - 1) * LIMIT;
|
||||
|
||||
if (finalQuery) {
|
||||
Object.keys(finalQuery).forEach((key) => {
|
||||
const value = finalQuery[key];
|
||||
if (method == "GET") {
|
||||
const newFinalQuery = _.cloneDeep(
|
||||
finalQuery || ({} as DsqlCrudQueryObject<T>)
|
||||
);
|
||||
Object.keys(newFinalQuery).forEach((key) => {
|
||||
const value = newFinalQuery[key];
|
||||
if (typeof value == "string" && value.match(/^\{|^\[/)) {
|
||||
finalQuery[key] = EJSON.stringify(value);
|
||||
newFinalQuery[key] = EJSON.stringify(value);
|
||||
}
|
||||
if (value == "true") {
|
||||
finalQuery[key] = true;
|
||||
newFinalQuery[key] = true;
|
||||
}
|
||||
if (value == "false") {
|
||||
finalQuery[key] = false;
|
||||
newFinalQuery[key] = false;
|
||||
}
|
||||
});
|
||||
|
||||
if (finalQuery.limit) LIMIT = numberfy(finalQuery.limit);
|
||||
if (finalQuery.page) PAGE = numberfy(finalQuery.page);
|
||||
if (newFinalQuery.limit) LIMIT = numberfy(newFinalQuery.limit);
|
||||
if (newFinalQuery.page) PAGE = numberfy(newFinalQuery.page);
|
||||
OFFSET = (PAGE - 1) * LIMIT;
|
||||
|
||||
finalQuery = newFinalQuery;
|
||||
}
|
||||
|
||||
let finalData = finalBody
|
||||
@@ -75,46 +83,65 @@ export default async function dsqlMethodCrud<
|
||||
...finalBody,
|
||||
...extraData,
|
||||
} as T)
|
||||
: undefined;
|
||||
: ({} as T);
|
||||
|
||||
if (finalData && user?.id && addUser) {
|
||||
if (user?.id && addUser) {
|
||||
finalData = {
|
||||
...finalData,
|
||||
[addUser.field]: String(user.id),
|
||||
};
|
||||
} as T;
|
||||
}
|
||||
|
||||
if (transform && finalData) {
|
||||
finalData = await transform({
|
||||
if (transformData) {
|
||||
if (debug) {
|
||||
console.log("DEBUG:::transforming Data ...");
|
||||
}
|
||||
|
||||
finalData = (await transformData({
|
||||
data: finalData,
|
||||
existingData: existingData,
|
||||
user,
|
||||
reqMethod: method,
|
||||
})) as T;
|
||||
}
|
||||
|
||||
if (transformQuery) {
|
||||
if (debug) {
|
||||
console.log("DEBUG:::transforming Query ...");
|
||||
}
|
||||
|
||||
finalQuery = await transformQuery({
|
||||
query: finalQuery || {},
|
||||
user,
|
||||
reqMethod: method,
|
||||
});
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
console.log("DEBUG:::finalQuery", finalQuery);
|
||||
console.log("DEBUG:::finalData", finalData);
|
||||
}
|
||||
|
||||
switch (method) {
|
||||
case "GET":
|
||||
const GET_RESULT = await dsqlCrud({
|
||||
action: "get",
|
||||
table: tableName,
|
||||
query: finalQuery
|
||||
? ({
|
||||
...finalQuery,
|
||||
query: {
|
||||
...finalQuery.query,
|
||||
...(user?.id && addUser
|
||||
? {
|
||||
[addUser.field]: {
|
||||
value: String(user.id),
|
||||
},
|
||||
}
|
||||
: undefined),
|
||||
},
|
||||
limit: LIMIT,
|
||||
offset: OFFSET,
|
||||
} as any)
|
||||
: undefined,
|
||||
query: {
|
||||
...finalQuery,
|
||||
query: {
|
||||
...finalQuery?.query,
|
||||
...(user?.id && addUser
|
||||
? {
|
||||
[addUser.field]: {
|
||||
value: String(user.id),
|
||||
},
|
||||
}
|
||||
: undefined),
|
||||
},
|
||||
limit: LIMIT,
|
||||
offset: OFFSET,
|
||||
} as any,
|
||||
sanitize,
|
||||
});
|
||||
|
||||
@@ -131,7 +158,10 @@ export default async function dsqlMethodCrud<
|
||||
const POST_RESULT = await dsqlCrud({
|
||||
action: "insert",
|
||||
table: tableName,
|
||||
data: finalData,
|
||||
data:
|
||||
finalData && Object.keys(finalData)?.[0]
|
||||
? finalData
|
||||
: undefined,
|
||||
sanitize,
|
||||
});
|
||||
result = {
|
||||
@@ -146,7 +176,10 @@ export default async function dsqlMethodCrud<
|
||||
const PUT_RESULT = await dsqlCrud({
|
||||
action: "update",
|
||||
table: tableName,
|
||||
data: finalData,
|
||||
data:
|
||||
finalData && Object.keys(finalData)?.[0]
|
||||
? finalData
|
||||
: undefined,
|
||||
targetId,
|
||||
sanitize,
|
||||
});
|
||||
|
||||
@@ -56,6 +56,7 @@ export default async function connDbHandler<ReturnType = any>(
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.log(`connDbHandler Error: ${error.message}`);
|
||||
console.log(conn?.config());
|
||||
return null;
|
||||
} finally {
|
||||
conn?.end();
|
||||
|
||||
Reference in New Issue
Block a user