Skip to content

Commit 909a285

Browse files
committed
feat
1 parent 2424054 commit 909a285

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class MongoStorageAdapter implements StorageAdapter {
132132
_mongoOptions: Object;
133133
_onchange: any;
134134
_stream: any;
135+
_clientLogEvents: ?Array<any>;
135136
// Public
136137
connectionPromise: ?Promise<any>;
137138
database: any;
@@ -154,6 +155,7 @@ export class MongoStorageAdapter implements StorageAdapter {
154155
this.enableSchemaHooks = !!mongoOptions.enableSchemaHooks;
155156
this.schemaCacheTtl = mongoOptions.schemaCacheTtl;
156157
this.disableIndexFieldValidation = !!mongoOptions.disableIndexFieldValidation;
158+
this._clientLogEvents = mongoOptions.clientLogEvents;
157159
// Remove Parse Server-specific options that should not be passed to MongoDB client
158160
// Note: We only delete from this._mongoOptions, not from the original mongoOptions object,
159161
// because other components (like DatabaseController) need access to these options
@@ -162,6 +164,7 @@ export class MongoStorageAdapter implements StorageAdapter {
162164
'schemaCacheTtl',
163165
'maxTimeMS',
164166
'disableIndexFieldValidation',
167+
'clientLogEvents',
165168
'createIndexUserUsername',
166169
'createIndexUserUsernameCaseInsensitive',
167170
'createIndexUserEmail',
@@ -203,6 +206,31 @@ export class MongoStorageAdapter implements StorageAdapter {
203206
client.on('close', () => {
204207
delete this.connectionPromise;
205208
});
209+
210+
// Set up client event logging if configured
211+
if (this._clientLogEvents && Array.isArray(this._clientLogEvents)) {
212+
this._clientLogEvents.forEach(eventConfig => {
213+
client.on(eventConfig.name, event => {
214+
let logData = {};
215+
if (!eventConfig.keys || eventConfig.keys.length === 0) {
216+
logData = event;
217+
} else {
218+
eventConfig.keys.forEach(keyPath => {
219+
const keyParts = keyPath.split('.');
220+
let value = event;
221+
keyParts.forEach(part => {
222+
value = value ? value[part] : undefined;
223+
});
224+
logData[keyPath] = value;
225+
});
226+
}
227+
228+
const logMessage = `MongoDB client event ${eventConfig.name}: ${JSON.stringify(logData)}`;
229+
logger[eventConfig.logLevel](logMessage);
230+
});
231+
});
232+
}
233+
206234
this.client = client;
207235
this.database = database;
208236
})

src/Options/Definitions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,12 @@ module.exports.DatabaseOptions = {
11171117
'The MongoDB driver option to specify the amount of time in milliseconds to wait for a connection attempt to finish before trying the next address when using the autoSelectFamily option. If set to a positive integer less than 10, the value 10 is used instead.',
11181118
action: parsers.numberParser('autoSelectFamilyAttemptTimeout'),
11191119
},
1120+
clientLogEvents: {
1121+
env: 'PARSE_SERVER_DATABASE_CLIENT_LOG_EVENTS',
1122+
help:
1123+
"An array of MongoDB client event configurations to enable logging of specific events. Each configuration object should contain:<br><ul><li>`name` (the event name, e.g., 'topologyDescriptionChanged', 'serverDescriptionChanged', 'connectionPoolCleared', 'connectionPoolReady')</li><li>`keys` (optional array of dot-notation paths to extract specific data from the event object; if not provided or empty, the entire event object will be logged)</li><li>`logLevel` (the log level to use for this event: 'error', 'warn', 'info', 'debug', etc.).</li></ul>",
1124+
action: parsers.arrayParser,
1125+
},
11201126
compressors: {
11211127
env: 'PARSE_SERVER_DATABASE_COMPRESSORS',
11221128
help:

src/Options/docs.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Options/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,8 @@ export interface DatabaseOptions {
725725
createIndexRoleName: ?boolean;
726726
/* Set to `true` to disable validation of index fields. When disabled, indexes can be created even if the fields do not exist in the schema. This can be useful when creating indexes on fields that will be added later. */
727727
disableIndexFieldValidation: ?boolean;
728+
/* An array of MongoDB client event configurations to enable logging of specific events. Each configuration object should contain:<br><ul><li>`name` (the event name, e.g., 'topologyDescriptionChanged', 'serverDescriptionChanged', 'connectionPoolCleared', 'connectionPoolReady')</li><li>`keys` (optional array of dot-notation paths to extract specific data from the event object; if not provided or empty, the entire event object will be logged)</li><li>`logLevel` (the log level to use for this event: 'error', 'warn', 'info', 'debug', etc.).</li></ul> */
729+
clientLogEvents: ?(any[]);
728730
}
729731

730732
export interface AuthAdapter {

0 commit comments

Comments
 (0)