This commit is contained in:
Benjamin Toby
2025-06-04 08:43:16 +01:00
parent 1364074c2c
commit 761348de08
37 changed files with 577 additions and 140 deletions
@@ -0,0 +1,8 @@
import { DATASQUIREL_LoggedInUser, UserType } from "../../../types";
type Param = {
user?: DATASQUIREL_LoggedInUser | UserType;
userId?: string | number | null;
dbSlug?: string;
};
export default function grabUserDbFullName({ dbSlug, user, userId }: Param): string;
export {};
@@ -0,0 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabUserDbFullName;
function grabUserDbFullName({ dbSlug, user, userId }) {
const finalUserId = (user === null || user === void 0 ? void 0 : user.id) || userId;
if (!finalUserId || !dbSlug)
throw new Error(`Couldn't grab full DB name. Missing parameters finalUserId || dbSlug`);
if (dbSlug.match(/[^a-zA-Z0-9-_]/)) {
throw new Error(`Invalid Database slug`);
}
return `datasquirel_user_${finalUserId}_${dbSlug}`;
}
@@ -13,6 +13,7 @@ export default function grabDirNames(param?: Param): {
tempDirName: string;
defaultTableFieldsJSONFilePath: string;
usersSchemaDir: string;
targetUserSchemaDir: string | undefined;
userSchemaMainJSONFilePath: string | undefined;
userPrivateMediaDir: string | undefined;
userPrivateExportsDir: string | undefined;
@@ -33,5 +34,7 @@ export default function grabDirNames(param?: Param): {
siteSetupFile: string;
envFile: string;
testEnvFile: string;
userPublicMediaDir: string | undefined;
userTempSQLFilePath: string | undefined;
};
export {};
+18 -5
View File
@@ -8,6 +8,9 @@ const path_1 = __importDefault(require("path"));
function grabDirNames(param) {
var _a;
const appDir = (param === null || param === void 0 ? void 0 : param.appDir) || process.env.DSQL_APP_DIR;
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR || "/static";
const finalUserId = ((_a = param === null || param === void 0 ? void 0 : param.user) === null || _a === void 0 ? void 0 : _a.id) || (param === null || param === void 0 ? void 0 : param.userId);
const publicImagesDir = path_1.default.join(STATIC_ROOT, `images`);
if (!appDir)
throw new Error("Please provide the `DSQL_APP_DIR` env variable.");
const schemasDir = process.env.DSQL_DB_SCHEMA_DIR ||
@@ -20,11 +23,15 @@ function grabDirNames(param) {
const mainShemaJSONFilePath = path_1.default.join(schemasDir, `main.json`);
const defaultTableFieldsJSONFilePath = path_1.default.join(pakageSharedDir, `data/defaultFields.json`);
const usersSchemaDir = path_1.default.join(schemasDir, `users`);
const userDirPath = ((_a = param === null || param === void 0 ? void 0 : param.user) === null || _a === void 0 ? void 0 : _a.id)
? path_1.default.join(usersSchemaDir, `user-${param.user.id}`)
: (param === null || param === void 0 ? void 0 : param.userId)
? path_1.default.join(usersSchemaDir, `user-${param.userId}`)
: undefined;
const targetUserSchemaDir = finalUserId
? path_1.default.join(usersSchemaDir, `user-${finalUserId}`)
: undefined;
const userTempSQLFilePath = targetUserSchemaDir
? path_1.default.join(targetUserSchemaDir, `tmp.sql`)
: undefined;
const userDirPath = finalUserId
? path_1.default.join(usersSchemaDir, `user-${finalUserId}`)
: undefined;
const userSchemaMainJSONFilePath = userDirPath
? path_1.default.join(userDirPath, `main.json`)
: undefined;
@@ -47,6 +54,9 @@ function grabDirNames(param) {
const userPrivateDbExportZipFilePath = userPrivateSQLExportsDir
? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbExportZipFileName)
: undefined;
const userPublicMediaDir = finalUserId
? path_1.default.join(publicImagesDir, `user-images/user-${finalUserId}`)
: undefined;
const userPrivateDbImportZipFileName = `db-export.zip`;
const userPrivateDbImportZipFilePath = userPrivateSQLExportsDir
? path_1.default.join(userPrivateSQLExportsDir, userPrivateDbImportZipFileName)
@@ -70,6 +80,7 @@ function grabDirNames(param) {
tempDirName,
defaultTableFieldsJSONFilePath,
usersSchemaDir,
targetUserSchemaDir,
userSchemaMainJSONFilePath,
userPrivateMediaDir,
userPrivateExportsDir,
@@ -90,5 +101,7 @@ function grabDirNames(param) {
siteSetupFile,
envFile,
testEnvFile,
userPublicMediaDir,
userTempSQLFilePath,
};
}
+1 -1
View File
@@ -1,5 +1,5 @@
type Param = {
dbName: string;
dbName?: string;
userId?: string | number;
};
/**
+2
View File
@@ -5,6 +5,8 @@ exports.default = grabDbFullName;
* # Grab Database Full Name
*/
function grabDbFullName({ dbName, userId }) {
if (!dbName)
throw new Error(`Database name not provided to db name parser funciton`);
const sanitizedName = dbName.replace(/[^a-z0-9\_]/g, "");
const cleanedDbName = sanitizedName.replace(/datasquirel_user_\d+_/, "");
if (!userId)