/
DFMigrationExt.ts
57 lines (49 loc) · 1.64 KB
/
DFMigrationExt.ts
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
import { DFBaseExtension } from "./DFBaseExtension.js";
import { EntityWithMetadata, SafeEntity, UpdateValue } from "../types/types.js";
import { DFWriteTransaction } from "../DFWriteTransaction.js";
export interface DFMigrationExtConfig<Entity extends SafeEntity<Entity>> {
version: number;
migrateEntity: (
currentEntityVersion: number,
entity: EntityWithMetadata,
) =>
| Partial<Record<keyof Entity, UpdateValue>>
| Promise<Partial<Record<keyof Entity, UpdateValue>>>;
}
export class DFMigrationExt<
Entity extends SafeEntity<Entity>,
> extends DFBaseExtension<Entity> {
public constructor(public readonly config: DFMigrationExtConfig<Entity>) {
super();
}
public onInsert(
entity: EntityWithMetadata,
transaction: DFWriteTransaction,
): void | Promise<void> {
// store version of this item
transaction.primaryUpdateOperation.updateValues._v = this.config.version;
}
public entityRequiresMigration(entity: EntityWithMetadata): boolean {
const currentEntityVersion = (entity._v || 0) as number;
return currentEntityVersion < this.config.version;
}
public async migrateEntity(
entity: EntityWithMetadata,
transaction: DFWriteTransaction,
): Promise<void> {
const currentEntityVersion = (entity._v || 0) as number;
const migrationUpdates = await this.config.migrateEntity(
currentEntityVersion,
entity,
);
// append any user requested updates to our migration transaction
Object.assign(
transaction.primaryUpdateOperation.updateValues,
migrationUpdates,
{
// record updated version
_v: this.config.version,
},
);
}
}