-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSqliteObjectView.ts
More file actions
60 lines (47 loc) · 1.95 KB
/
SqliteObjectView.ts
File metadata and controls
60 lines (47 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { AbstractSqliteView } from './AbstractSqliteView.ts';
import type { IObjectStorage, IEventLocker } from '../interfaces/index.ts';
import { SqliteObjectStorage } from './SqliteObjectStorage.ts';
import type { Database } from 'better-sqlite3';
import { assertString } from '../utils/assert.ts';
/**
* SQLite-backed object view with restore locking and last-processed-event tracking
*/
export class SqliteObjectView<TRecord> extends AbstractSqliteView implements IObjectStorage<TRecord>, IEventLocker {
#sqliteObjectStorage: SqliteObjectStorage<TRecord>;
constructor(options: ConstructorParameters<typeof AbstractSqliteView>[0] & {
tableNamePrefix: string
}) {
assertString(options?.tableNamePrefix, 'options.tableNamePrefix');
assertString(options?.schemaVersion, 'options.schemaVersion');
super(options);
this.#sqliteObjectStorage = new SqliteObjectStorage<TRecord>({
viewModelSqliteDb: options.viewModelSqliteDb,
viewModelSqliteDbFactory: options.viewModelSqliteDbFactory,
tableName: `${options.tableNamePrefix}_${options.schemaVersion}`
});
}
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
protected initialize(db: Database): Promise<void> | void {
// No need to initialize the table here, it's done in SqliteObjectStorage
}
async get(id: string): Promise<TRecord | undefined> {
if (!this.ready)
await this.once('ready');
return this.#sqliteObjectStorage.get(id);
}
getSync(id: string) {
return this.#sqliteObjectStorage.getSync(id);
}
async create(id: string, data: TRecord) {
await this.#sqliteObjectStorage.create(id, data);
}
async update(id: string, update: (r: TRecord) => TRecord) {
await this.#sqliteObjectStorage.update(id, update);
}
async updateEnforcingNew(id: string, update: (r?: TRecord) => TRecord) {
await this.#sqliteObjectStorage.updateEnforcingNew(id, update);
}
async delete(id: string): Promise<boolean> {
return this.#sqliteObjectStorage.delete(id);
}
}