Repository navigation
Expand file tree
/
Copy pathMongoObjectView.ts
More file actions
50 lines (39 loc) · 1.62 KB
/
Copy pathMongoObjectView.ts
File metadata and controls
50 lines (39 loc) · 1.62 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
import { AbstractMongoView } from './AbstractMongoView.ts';
import type { IObjectStorage, IEventLocker, Identifier } from '../interfaces/index.ts';
import { MongoObjectStorage } from './MongoObjectStorage.ts';
import { assertString } from '../utils/assert.ts';
/**
* MongoDB-backed object view with restore locking and last-processed-event tracking
*/
export class MongoObjectView<TRecord> extends AbstractMongoView implements IObjectStorage<TRecord>, IEventLocker {
readonly #mongoObjectStorage: MongoObjectStorage<TRecord>;
constructor(options: ConstructorParameters<typeof AbstractMongoView>[0] & {
tableNamePrefix: string
}) {
assertString(options?.tableNamePrefix, 'tableNamePrefix');
assertString(options?.schemaVersion, 'schemaVersion');
super(options);
this.#mongoObjectStorage = new MongoObjectStorage<TRecord>({
viewModelMongoDb: options.viewModelMongoDb,
viewModelMongoDbFactory: options.viewModelMongoDbFactory,
tableName: `${options.tableNamePrefix}_${options.schemaVersion}`
});
}
async get(id: Identifier): Promise<TRecord | undefined> {
if (!this.ready)
await this.once('ready');
return this.#mongoObjectStorage.get(id);
}
async create(id: Identifier, data: TRecord) {
await this.#mongoObjectStorage.create(id, data);
}
async update(id: Identifier, update: (r: TRecord) => TRecord) {
await this.#mongoObjectStorage.update(id, update);
}
async updateEnforcingNew(id: Identifier, update: (r?: TRecord) => TRecord) {
await this.#mongoObjectStorage.updateEnforcingNew(id, update);
}
async delete(id: Identifier): Promise<boolean> {
return this.#mongoObjectStorage.delete(id);
}
}