-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBucketDefinitionMapping.ts
More file actions
72 lines (59 loc) · 2.7 KB
/
Copy pathBucketDefinitionMapping.ts
File metadata and controls
72 lines (59 loc) · 2.7 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
61
62
63
64
65
66
67
68
69
70
71
72
import { ServiceAssertionError } from '@powersync/lib-services-framework';
import { BucketDataSource, ParameterIndexLookupCreator, SyncConfigWithErrors } from '@powersync/service-sync-rules';
import { SyncRuleDocument } from './models.js';
export type BucketDefinitionId = string;
export type ParameterIndexId = string;
export class BucketDefinitionMapping {
static fromSyncRules(doc: Pick<SyncRuleDocument, 'rule_mapping'>): BucketDefinitionMapping {
return new BucketDefinitionMapping(doc.rule_mapping?.definitions ?? {}, doc.rule_mapping?.parameter_indexes ?? {});
}
static fromParsedSyncRules(syncRules: SyncConfigWithErrors): BucketDefinitionMapping {
const definitionNames = syncRules.config.bucketDataSources.map((source) => source.uniqueName).sort();
const parameterKeys = syncRules.config.bucketParameterLookupSources
.map((source) => `${source.defaultLookupScope.lookupName}#${source.defaultLookupScope.queryId}`)
.sort();
const definitions: Record<string, BucketDefinitionId> = {};
const parameterLookups: Record<string, ParameterIndexId> = {};
for (const [index, uniqueName] of definitionNames.entries()) {
definitions[uniqueName] = (index + 1).toString(16);
}
for (const [index, key] of parameterKeys.entries()) {
parameterLookups[key] = (index + 1).toString(16);
}
return new BucketDefinitionMapping(definitions, parameterLookups);
}
constructor(
private definitions: Record<string, BucketDefinitionId> = {},
private parameterLookupMapping: Record<string, ParameterIndexId> = {}
) {}
bucketSourceId(source: BucketDataSource): BucketDefinitionId {
const defId = this.definitions[source.uniqueName];
if (defId == null) {
throw new ServiceAssertionError(`No mapping found for bucket source ${source.uniqueName}`);
}
return defId;
}
allBucketDefinitionIds(): BucketDefinitionId[] {
return Object.values(this.definitions);
}
allParameterIndexIds(): ParameterIndexId[] {
return Object.values(this.parameterLookupMapping);
}
parameterLookupId(source: ParameterIndexLookupCreator): ParameterIndexId {
const key = this.parameterLookupKey(source.defaultLookupScope.lookupName, source.defaultLookupScope.queryId);
const defId = this.parameterLookupMapping[key];
if (defId == null) {
throw new ServiceAssertionError(`No mapping found for parameter lookup source ${key}`);
}
return defId;
}
private parameterLookupKey(lookupName: string, queryId: string) {
return `${lookupName}#${queryId}`;
}
serialize(): NonNullable<SyncRuleDocument['rule_mapping']> {
return {
definitions: { ...this.definitions },
parameter_indexes: { ...this.parameterLookupMapping }
};
}
}