-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathSyncConfig.ts
More file actions
131 lines (116 loc) · 4.47 KB
/
Copy pathSyncConfig.ts
File metadata and controls
131 lines (116 loc) · 4.47 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { BucketDataSource, BucketSource, CreateSourceParams, ParameterIndexLookupCreator } from './BucketSource.js';
import { CompatibilityContext } from './compatibility.js';
import { YamlError } from './errors.js';
import { SqlEventDescriptor } from './events/SqlEventDescriptor.js';
import { HydratedSyncRules } from './HydratedSyncRules.js';
import { SourceTableInterface } from './SourceTableInterface.js';
import { TablePattern } from './TablePattern.js';
import { SqliteInputValue, SqliteRow, SqliteValue } from './types.js';
import { applyRowContext } from './utils.js';
/**
* A class describing how the sync process has been configured (i.e. which buckets and parameters to create and how to
* resolve buckets for connections).
*/
export abstract class SyncConfig {
bucketDataSources: BucketDataSource[] = [];
bucketParameterLookupSources: ParameterIndexLookupCreator[] = [];
bucketSources: BucketSource[] = [];
compatibility: CompatibilityContext = CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY;
/**
* If not defined, the storage module picks the latest stable version.
*
* Only supported storage versions can be set here when parsing from yaml.
*/
storageVersion: number | undefined;
eventDescriptors: SqlEventDescriptor[] = [];
/**
* The (YAML-based) source contents from which these sync rules have been derived.
*/
content: string;
constructor(content: string) {
this.content = content;
}
/**
* Hydrate the sync rule definitions with persisted state into runnable sync rules.
*
* Note: versionedBucketIds is not checked here: It is set at a higher level based
* on the storage version of the persisted sync rules, and used in hydrationState.
*
* @param params.hydrationState Transforms bucket ids based on persisted state.
*/
hydrate(params: CreateSourceParams): HydratedSyncRules {
return new HydratedSyncRules({
definition: this,
createParams: params,
bucketDataSources: this.bucketDataSources,
bucketParameterIndexLookupCreators: this.bucketParameterLookupSources,
eventDescriptors: this.eventDescriptors,
compatibility: this.compatibility
});
}
applyRowContext<MaybeToast extends undefined = never>(
source: SqliteRow<SqliteInputValue | MaybeToast>
): SqliteRow<SqliteValue | MaybeToast> {
return applyRowContext(source, this.compatibility);
}
protected writeSourceTables(sourceTables: Map<String, TablePattern>): void {
for (const bucket of this.bucketDataSources) {
for (const r of bucket.getSourceTables()) {
const key = `${r.connectionTag}.${r.schema}.${r.tablePattern}`;
sourceTables.set(key, r);
}
}
for (const bucket of this.bucketParameterLookupSources) {
for (const r of bucket.getSourceTables()) {
const key = `${r.connectionTag}.${r.schema}.${r.tablePattern}`;
sourceTables.set(key, r);
}
}
for (const event of this.eventDescriptors) {
for (const r of event.getSourceTables()) {
const key = `${r.connectionTag}.${r.schema}.${r.tablePattern}`;
sourceTables.set(key, r);
}
}
}
getSourceTables(): TablePattern[] {
const sourceTables = new Map<String, TablePattern>();
this.writeSourceTables(sourceTables);
return [...sourceTables.values()];
}
getEventTables(): TablePattern[] {
const eventTables = new Map<String, TablePattern>();
if (this.eventDescriptors) {
for (const event of this.eventDescriptors) {
for (const r of event.getSourceTables()) {
const key = `${r.connectionTag}.${r.schema}.${r.tablePattern}`;
eventTables.set(key, r);
}
}
}
return [...eventTables.values()];
}
tableTriggersEvent(table: SourceTableInterface): boolean {
return this.eventDescriptors.some((bucket) => bucket.tableTriggersEvent(table));
}
tableSyncsData(table: SourceTableInterface): boolean {
return this.bucketDataSources.some((b) => b.tableSyncsData(table));
}
tableSyncsParameters(table: SourceTableInterface): boolean {
return this.bucketParameterLookupSources.some((b) => b.tableSyncsParameters(table));
}
debugGetOutputTables() {
let result: Record<string, any[]> = {};
for (let bucket of this.bucketDataSources) {
bucket.debugWriteOutputTables(result);
}
return result;
}
debugRepresentation() {
return this.bucketSources.map((rules) => rules.debugRepresentation());
}
}
export interface SyncConfigWithErrors {
config: SyncConfig;
errors: YamlError[];
}