Skip to content

Commit 6db5dbe

Browse files
feat: add event hooks
1 parent 4f7cbca commit 6db5dbe

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this library will be documented in this file.
55
## 1.0.4
66

77
- Delegation function is called when a connection to MongoDB is required instead of retaining the database reference ([issue #11](https://github.com/VilledeMontreal/express-idempotency-mongo-adapter/issues/11))
8+
- Add events for before and after database usage
89

910
## 1.0.3
1011

src/adapter.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
7373
? options.collectionPrefix
7474
: COLLECTION_PREFIX,
7575
ttl: options.ttl ? options.ttl : TTL,
76+
// Events
77+
onBeforeDbUse: options.onBeforeDbUse,
78+
onAfterDbUse: options.onAfterDbUse
7679
};
7780
}
7881

@@ -219,6 +222,8 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
219222
idempotencyResource: IdempotencyResource
220223
): Promise<void> {
221224
await this.checkForInitialization();
225+
226+
await this._options?.onBeforeDbUse?.call(this);
222227
const db: Db = await this._options.delegate();
223228

224229
const collection = db.collection(this.getStoreCollectionName());
@@ -227,6 +232,7 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
227232
createdAt: new Date(),
228233
schemaVersion: SCHEMA_VERSION,
229234
});
235+
await this._options?.onAfterDbUse?.call(this);
230236
}
231237

232238
/**
@@ -237,6 +243,7 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
237243
idempotencyResource: IdempotencyResource
238244
): Promise<void> {
239245
await this.checkForInitialization();
246+
await this._options?.onBeforeDbUse?.call(this);
240247
const db: Db = await this._options.delegate();
241248

242249
const newResource = {
@@ -249,6 +256,7 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
249256
{ idempotencyKey: idempotencyResource.idempotencyKey },
250257
newResource
251258
);
259+
await this._options?.onAfterDbUse?.call(this);
252260
}
253261

254262
/**
@@ -257,9 +265,11 @@ export class MongoAdapter implements IIdempotencyDataAdapter {
257265
*/
258266
public async delete(idempotencyKey: string): Promise<void> {
259267
await this.checkForInitialization();
268+
await this._options?.onBeforeDbUse?.call(this);
260269
const db: Db = await this._options.delegate();
261270
const collection = db.collection(this.getStoreCollectionName());
262271
await collection.deleteOne({ idempotencyKey });
272+
await this._options?.onAfterDbUse?.call(this);
263273
}
264274
}
265275

src/adapterOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export interface AdapterOptions {
2929
useDelegation?: boolean;
3030
delegate?: () => Promise<mongodb.Db>;
3131

32+
// Hooks for events
33+
onBeforeDbUse?: () => Promise<void>;
34+
onAfterDbUse?: () => Promise<void>;
35+
3236
// The prefix used to create collections in the mongo database
3337
// Default value is 'idempotency'
3438
collectionPrefix?: string;

0 commit comments

Comments
 (0)