From 3ba92dec04d9b605aefa38da2df3660d676366f1 Mon Sep 17 00:00:00 2001 From: devlikepro Date: Thu, 5 Mar 2026 11:43:28 +0700 Subject: [PATCH] [core] WAHA_NAMESPACE and WAHA_SESSION_NAMESPACE --- src/config.ts | 12 ++++++++++++ src/core/manager.core.ts | 3 ++- src/core/storage/LocalStoreCore.ts | 22 ++++++++++++++++------ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/config.ts b/src/config.ts index d538d035..b8a2cfb1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -5,3 +5,15 @@ export function getEngineName(): string { // If not set - use WEBJS return process.env.WHATSAPP_DEFAULT_ENGINE || WAHAEngine.WEBJS; } + +export function getNamespace(): string { + // Controls the prefix used for the main database and folder (non-session data) + // If not set - falls back to engine name for backward compatibility + return (process.env.WAHA_NAMESPACE || getEngineName()).toLowerCase(); +} + +export function getSessionNamespace(): string { + // Controls the prefix used for session databases and folders + // If not set - falls back to engine name for backward compatibility + return (process.env.WAHA_SESSION_NAMESPACE || getEngineName()).toLowerCase(); +} diff --git a/src/core/manager.core.ts b/src/core/manager.core.ts index 4d59fad6..7bcee4c5 100644 --- a/src/core/manager.core.ts +++ b/src/core/manager.core.ts @@ -25,6 +25,7 @@ import { PinoLogger } from 'nestjs-pino'; import { Observable, retry, share } from 'rxjs'; import { map } from 'rxjs/operators'; +import { getNamespace, getSessionNamespace } from '../config'; import { WhatsappConfigService } from '../config.service'; import { WAHAEngine, @@ -107,7 +108,7 @@ export class SessionManagerCore extends SessionManager implements OnModuleInit { }), ); - this.store = new LocalStoreCore(engineName.toLowerCase()); + this.store = new LocalStoreCore(getNamespace(), getSessionNamespace()); this.sessionAuthRepository = new LocalSessionAuthRepository(this.store); this.clearStorage().catch((error) => { this.log.error({ error }, 'Error while clearing storage'); diff --git a/src/core/storage/LocalStoreCore.ts b/src/core/storage/LocalStoreCore.ts index 6f3014bc..3eba0218 100644 --- a/src/core/storage/LocalStoreCore.ts +++ b/src/core/storage/LocalStoreCore.ts @@ -10,15 +10,18 @@ export class LocalStoreCore extends LocalStore { protected readonly baseDirectory: string = process.env.WAHA_LOCAL_STORE_BASE_DIR || './.sessions'; - private readonly engine: string; + private readonly namespace: string; + private readonly sessionNamespace: string; private knex: Knex.Knex; - constructor(engine: string) { + constructor(namespace: string, sessionNamespace: string) { super(); - this.engine = engine; + this.namespace = namespace; + this.sessionNamespace = sessionNamespace; } async init(sessionName?: string) { + await fs.mkdir(this.getMainDirectory(), { recursive: true }); await fs.mkdir(this.getEngineDirectory(), { recursive: true }); if (!this.knex) { this.knex = this.buildKnex(); @@ -40,11 +43,18 @@ export class LocalStoreCore extends LocalStore { return path.resolve(this.baseDirectory); } + /** + * Get the directory where the main WAHA database (SQLite) is stored + */ + getMainDirectory() { + return safeJoin(this.baseDirectory, this.namespace); + } + /** * Get the directory where the engine sessions are stored */ getEngineDirectory() { - return safeJoin(this.baseDirectory, this.engine); + return safeJoin(this.baseDirectory, this.sessionNamespace); } getSessionDirectory(name: string): string { @@ -67,8 +77,8 @@ export class LocalStoreCore extends LocalStore { } buildKnex(): Knex.Knex { - const engineDir = this.getEngineDirectory(); - const database = path.join(engineDir, 'waha.sqlite3'); + const mainDir = this.getMainDirectory(); + const database = path.join(mainDir, 'waha.sqlite3'); return Knex({ client: KNEX_SQLITE_CLIENT, connection: { filename: database },