From 850323b9695e94366c7ad540a284dee2a3c67ab8 Mon Sep 17 00:00:00 2001 From: Saikumar Date: Mon, 25 Nov 2024 16:09:52 +0530 Subject: [PATCH] feat(mongodb): add support for collection prefix in ErrsoleMongoDB --- lib/index.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 9a3fa12..4bc4ebf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -63,22 +63,31 @@ class ErrsoleMongoDB extends EventEmitter { */ constructor (uri, dbNameOrOptions, options = {}) { super(); + this.name = packageJSON.name; this.version = packageJSON.version || '0.0.0'; + const { collectionPrefix, ...connectionOptions } = typeof dbNameOrOptions === 'object' ? dbNameOrOptions : options; + this.uri = uri; this.dbName = typeof dbNameOrOptions === 'string' ? dbNameOrOptions : undefined; - this.connectionOptions = typeof dbNameOrOptions === 'object' ? dbNameOrOptions : options; + this.connectionOptions = connectionOptions; + + const prefix = collectionPrefix + ? `${collectionPrefix.toLowerCase().replace(/[^a-z0-9]/g, '')}_` + : 'errsole_'; this.client = new MongoClient(this.uri, this.connectionOptions); - this.logsCollectionName = 'errsole_logs'; - this.notificationsCollectionName = 'errsole_notifications'; - this.usersCollectionName = 'errsole_users'; - this.configCollectionName = 'errsole_config'; + this.logsCollectionName = `${prefix}logs`; + this.notificationsCollectionName = `${prefix}notifications`; + this.usersCollectionName = `${prefix}users`; + this.configCollectionName = `${prefix}config`; + this.isConnectionInProgress = true; this.pendingLogs = []; this.batchSize = 100; this.flushInterval = 1000; + this.init(); }