• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

VaclavObornik / mongodash / 19208307003

09 Nov 2025 12:15PM UTC coverage: 97.765%. First build
19208307003

Pull #416

github

web-flow
Merge 4602433ff into 4f63e6aa3
Pull Request #416: WIP add post-commit hook functionality to withTransaction

161 of 169 branches covered (95.27%)

Branch coverage included in aggregate %.

28 of 29 new or added lines in 5 files covered. (96.55%)

364 of 368 relevant lines covered (98.91%)

510.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.29
/src/withTransaction.ts
1
'use strict';
2

3
import { ClientSession, TransactionOptions } from 'mongodb';
4
import { getMongoClient } from './getMongoClient';
31✔
5
import { OnError } from './OnError';
6
import { OnInfo } from './OnInfo';
7

8
export type InitOptions = {
9
    onError: OnError;
10
    onInfo: OnInfo;
11
};
12

13
export type PostCommitHook = () => void | Promise<void>;
14

15
const postCommitHooks = new WeakMap<ClientSession, PostCommitHook[]>();
31✔
16
let _onError: OnError;
17
let _onInfo: OnInfo;
18

19
export function init(options: InitOptions): void {
31✔
20
    _onError = options.onError;
30✔
21
    _onInfo = options.onInfo;
30✔
22
}
23

24
export function registerPostCommitHook(session: ClientSession, hook: PostCommitHook): void {
31✔
25
    if (!session.inTransaction()) {
12✔
26
        throw new Error('It is not possible to register a post commit hook without active transaction.');
2✔
27
    }
28

29
    const hooks = postCommitHooks.get(session);
10✔
30

31
    if (!hooks) {
10!
NEW
32
        throw new Error("Post-commit hooks can be registered only for sessions created by mongodash's withTransaction function.");
×
33
    }
34

35
    hooks.push(hook);
10✔
36
}
37

38
export async function withTransaction<T>(callback: (session: ClientSession) => Promise<T>, options?: TransactionOptions): Promise<T> {
31✔
39
    const clientSession = getMongoClient().startSession();
9✔
40

41
    postCommitHooks.set(clientSession, []);
9✔
42

43
    try {
9✔
44
        let returnValue: T;
45

46
        await clientSession.withTransaction(async () => {
9✔
47
            returnValue = await callback(clientSession);
9✔
48
        }, options);
49

50
        const hooks = postCommitHooks.get(clientSession)!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
7✔
51

52
        if (hooks.length > 0) {
7✔
53
            const results = await Promise.allSettled(hooks.map((hook) => hook()));
9✔
54
            results.forEach((result) => {
6✔
55
                if (result.status === 'rejected') {
9✔
56
                    _onError(result.reason);
1✔
57
                }
58
            });
59
        }
60

61
        return returnValue!; // eslint-disable-line @typescript-eslint/no-non-null-assertion
7✔
62
    } finally {
63
        postCommitHooks.delete(clientSession);
9✔
64
        await clientSession.endSession();
9✔
65
    }
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc