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

snatalenko / node-cqrs / 21717407497

05 Feb 2026 03:26PM UTC coverage: 84.53% (-9.9%) from 94.396%
21717407497

Pull #28

github

web-flow
Merge 025edb883 into 828e39903
Pull Request #28: TypeScript and event dispatching pipeline refactoring

611 of 939 branches covered (65.07%)

819 of 934 new or added lines in 65 files covered. (87.69%)

59 existing lines in 13 files now uncovered.

1213 of 1435 relevant lines covered (84.53%)

28.39 hits per line

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

89.47
/src/sqlite/AbstractSqliteAccessor.ts
1
import type { IContainer } from '../interfaces/index.ts';
2
import { Lock } from '../utils/index.ts';
8✔
3
import type { Database } from 'better-sqlite3';
4

5
/**
6
 * Abstract base class for accessing a SQLite database.
7
 *
8
 * Manages the database connection lifecycle, ensuring initialization via `assertDb`.
9
 * Supports providing a database instance directly or a factory function for lazy initialization.
10
 *
11
 * Subclasses must implement the `initialize` method for specific setup tasks.
12
 */
13
export abstract class AbstractSqliteAccessor {
8✔
14

15
        protected db: Database | undefined;
16
        #dbFactory: (() => Promise<Database> | Database) | undefined;
17
        #initLocker = new Lock();
98✔
18
        #initialized = false;
98✔
19

20
        constructor(c: Partial<Pick<IContainer, 'viewModelSqliteDb' | 'viewModelSqliteDbFactory'>>) {
21
                if (!c.viewModelSqliteDb && !c.viewModelSqliteDbFactory)
98!
NEW
22
                        throw new TypeError('either viewModelSqliteDb or viewModelSqliteDbFactory argument required');
×
23

24
                this.db = c.viewModelSqliteDb;
98✔
25
                this.#dbFactory = c.viewModelSqliteDbFactory;
98✔
26
        }
27

28
        protected abstract initialize(db: Database): Promise<void> | void;
29

30
        /**
31
         * Ensures that the database connection is initialized.
32
         * Uses a lock to prevent race conditions during concurrent initialization attempts.
33
         * If the database is not already initialized, it creates the database connection
34
         * using the provided factory and calls the `initialize` method.
35
         *
36
         * This method is idempotent and safe to call multiple times.
37
         */
38
        async assertConnection() {
39
                if (this.#initialized)
228✔
40
                        return;
164✔
41

42
                try {
64✔
43
                        await this.#initLocker.acquire();
64✔
44
                        if (this.#initialized)
64✔
45
                                return;
2✔
46

47
                        if (!this.db)
62!
NEW
48
                                this.db = await this.#dbFactory!();
×
49

50
                        await this.initialize(this.db);
62✔
51

52
                        this.#initialized = true;
62✔
53
                }
54
                finally {
55
                        this.#initLocker.release();
64✔
56
                }
57
        }
58
}
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