@powersync/service-core 1.21.0 → 1.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/dist/api/RouteAPI.d.ts +2 -2
  3. package/dist/api/diagnostics.d.ts +12 -1
  4. package/dist/api/diagnostics.js +34 -22
  5. package/dist/api/diagnostics.js.map +1 -1
  6. package/dist/auth/RemoteJWKSCollector.js +3 -2
  7. package/dist/auth/RemoteJWKSCollector.js.map +1 -1
  8. package/dist/entry/commands/compact-action.js +1 -1
  9. package/dist/entry/commands/compact-action.js.map +1 -1
  10. package/dist/modules/AbstractModule.d.ts +6 -2
  11. package/dist/modules/AbstractModule.js.map +1 -1
  12. package/dist/replication/AbstractReplicator.d.ts +2 -2
  13. package/dist/replication/AbstractReplicator.js +24 -23
  14. package/dist/replication/AbstractReplicator.js.map +1 -1
  15. package/dist/replication/RelationCache.d.ts +9 -2
  16. package/dist/replication/RelationCache.js +21 -2
  17. package/dist/replication/RelationCache.js.map +1 -1
  18. package/dist/replication/ReplicationModule.d.ts +3 -3
  19. package/dist/replication/ReplicationModule.js.map +1 -1
  20. package/dist/routes/configure-fastify.d.ts +12 -12
  21. package/dist/routes/configure-fastify.js +3 -1
  22. package/dist/routes/configure-fastify.js.map +1 -1
  23. package/dist/routes/endpoints/admin.d.ts +24 -24
  24. package/dist/routes/endpoints/admin.js +49 -35
  25. package/dist/routes/endpoints/admin.js.map +1 -1
  26. package/dist/routes/endpoints/checkpointing.js +1 -1
  27. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  28. package/dist/routes/endpoints/socket-route.js +1 -1
  29. package/dist/routes/endpoints/socket-route.js.map +1 -1
  30. package/dist/routes/endpoints/sync-rules.js +22 -13
  31. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  32. package/dist/routes/endpoints/sync-stream.js +1 -1
  33. package/dist/routes/endpoints/sync-stream.js.map +1 -1
  34. package/dist/routes/route-register.d.ts +2 -0
  35. package/dist/routes/route-register.js +65 -3
  36. package/dist/routes/route-register.js.map +1 -1
  37. package/dist/runner/teardown.js +8 -8
  38. package/dist/runner/teardown.js.map +1 -1
  39. package/dist/storage/BucketStorageBatch.d.ts +29 -6
  40. package/dist/storage/BucketStorageBatch.js.map +1 -1
  41. package/dist/storage/BucketStorageFactory.d.ts +26 -27
  42. package/dist/storage/BucketStorageFactory.js +5 -19
  43. package/dist/storage/BucketStorageFactory.js.map +1 -1
  44. package/dist/storage/ParsedSyncConfigSet.d.ts +23 -0
  45. package/dist/storage/ParsedSyncConfigSet.js +2 -0
  46. package/dist/storage/ParsedSyncConfigSet.js.map +1 -0
  47. package/dist/storage/PersistedReplicationStream.d.ts +32 -0
  48. package/dist/storage/PersistedReplicationStream.js +26 -0
  49. package/dist/storage/PersistedReplicationStream.js.map +1 -0
  50. package/dist/storage/PersistedSyncConfigContent.d.ts +60 -0
  51. package/dist/storage/PersistedSyncConfigContent.js +123 -0
  52. package/dist/storage/PersistedSyncConfigContent.js.map +1 -0
  53. package/dist/storage/PersistedSyncConfigStatus.d.ts +13 -0
  54. package/dist/storage/PersistedSyncConfigStatus.js +2 -0
  55. package/dist/storage/PersistedSyncConfigStatus.js.map +1 -0
  56. package/dist/storage/SourceEntity.d.ts +8 -1
  57. package/dist/storage/SourceTable.d.ts +37 -10
  58. package/dist/storage/SourceTable.js +50 -14
  59. package/dist/storage/SourceTable.js.map +1 -1
  60. package/dist/storage/SyncRulesBucketStorage.d.ts +80 -24
  61. package/dist/storage/SyncRulesBucketStorage.js.map +1 -1
  62. package/dist/storage/implementation/BucketDefinitionMapping.d.ts +117 -0
  63. package/dist/storage/implementation/BucketDefinitionMapping.js +277 -0
  64. package/dist/storage/implementation/BucketDefinitionMapping.js.map +1 -0
  65. package/dist/storage/implementation/IncrementalReprocessingCompatibility.d.ts +6 -0
  66. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js +31 -0
  67. package/dist/storage/implementation/IncrementalReprocessingCompatibility.js.map +1 -0
  68. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.d.ts +17 -0
  69. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js +62 -0
  70. package/dist/storage/implementation/IncrementalReprocessingSyncConfigLog.js.map +1 -0
  71. package/dist/storage/implementation/ReplicationStreamStorageIds.d.ts +15 -0
  72. package/dist/storage/implementation/ReplicationStreamStorageIds.js +20 -0
  73. package/dist/storage/implementation/ReplicationStreamStorageIds.js.map +1 -0
  74. package/dist/storage/storage-index.d.ts +8 -1
  75. package/dist/storage/storage-index.js +8 -1
  76. package/dist/storage/storage-index.js.map +1 -1
  77. package/dist/sync/BucketChecksumState.d.ts +4 -4
  78. package/dist/sync/BucketChecksumState.js +1 -1
  79. package/dist/sync/BucketChecksumState.js.map +1 -1
  80. package/dist/sync/sync.d.ts +2 -2
  81. package/dist/sync/sync.js +5 -0
  82. package/dist/sync/sync.js.map +1 -1
  83. package/dist/tracing/PerformanceTracer.d.ts +17 -1
  84. package/dist/tracing/PerformanceTracer.js +3 -0
  85. package/dist/tracing/PerformanceTracer.js.map +1 -1
  86. package/dist/util/checkpointing.js +1 -1
  87. package/dist/util/checkpointing.js.map +1 -1
  88. package/dist/util/config/types.d.ts +1 -1
  89. package/dist/util/lsn.d.ts +4 -0
  90. package/dist/util/lsn.js +10 -0
  91. package/dist/util/lsn.js.map +1 -1
  92. package/dist/util/util-index.d.ts +1 -0
  93. package/dist/util/util-index.js +1 -0
  94. package/dist/util/util-index.js.map +1 -1
  95. package/dist/util/utils.d.ts +5 -0
  96. package/dist/util/utils.js +7 -0
  97. package/dist/util/utils.js.map +1 -1
  98. package/package.json +6 -7
  99. package/src/api/RouteAPI.ts +2 -2
  100. package/src/api/diagnostics.ts +45 -28
  101. package/src/auth/RemoteJWKSCollector.ts +3 -1
  102. package/src/entry/commands/compact-action.ts +1 -1
  103. package/src/modules/AbstractModule.ts +6 -2
  104. package/src/replication/AbstractReplicator.ts +27 -26
  105. package/src/replication/RelationCache.ts +23 -4
  106. package/src/replication/ReplicationModule.ts +2 -3
  107. package/src/routes/configure-fastify.ts +8 -1
  108. package/src/routes/endpoints/admin.ts +75 -49
  109. package/src/routes/endpoints/checkpointing.ts +1 -1
  110. package/src/routes/endpoints/socket-route.ts +1 -1
  111. package/src/routes/endpoints/sync-rules.ts +23 -13
  112. package/src/routes/endpoints/sync-stream.ts +1 -1
  113. package/src/routes/route-register.ts +73 -4
  114. package/src/runner/teardown.ts +8 -8
  115. package/src/storage/BucketStorageBatch.ts +32 -7
  116. package/src/storage/BucketStorageFactory.ts +36 -40
  117. package/src/storage/ParsedSyncConfigSet.ts +25 -0
  118. package/src/storage/PersistedReplicationStream.ts +55 -0
  119. package/src/storage/PersistedSyncConfigContent.ts +188 -0
  120. package/src/storage/PersistedSyncConfigStatus.ts +14 -0
  121. package/src/storage/SourceEntity.ts +9 -1
  122. package/src/storage/SourceTable.ts +72 -21
  123. package/src/storage/SyncRulesBucketStorage.ts +87 -26
  124. package/src/storage/implementation/BucketDefinitionMapping.ts +436 -0
  125. package/src/storage/implementation/IncrementalReprocessingCompatibility.ts +41 -0
  126. package/src/storage/implementation/IncrementalReprocessingSyncConfigLog.ts +109 -0
  127. package/src/storage/implementation/ReplicationStreamStorageIds.ts +21 -0
  128. package/src/storage/storage-index.ts +9 -1
  129. package/src/sync/BucketChecksumState.ts +5 -5
  130. package/src/sync/sync.ts +8 -3
  131. package/src/tracing/PerformanceTracer.ts +24 -1
  132. package/src/util/checkpointing.ts +1 -1
  133. package/src/util/config/types.ts +1 -1
  134. package/src/util/lsn.ts +9 -0
  135. package/src/util/util-index.ts +1 -0
  136. package/src/util/utils.ts +8 -0
  137. package/test/src/auth.test.ts +11 -0
  138. package/test/src/diagnostics.test.ts +64 -33
  139. package/test/src/module-loader.test.ts +33 -1
  140. package/test/src/routes/admin.test.ts +180 -18
  141. package/test/src/routes/error-handler.integration.test.ts +275 -0
  142. package/test/src/routes/mocks.ts +6 -2
  143. package/test/src/routes/stream.test.ts +15 -4
  144. package/test/src/storage/SourceTable.test.ts +89 -0
  145. package/test/src/sync/BucketChecksumState.test.ts +25 -17
  146. package/tsconfig.json +1 -4
  147. package/tsconfig.tsbuildinfo +1 -1
  148. package/dist/storage/PersistedSyncRulesContent.d.ts +0 -59
  149. package/dist/storage/PersistedSyncRulesContent.js +0 -100
  150. package/dist/storage/PersistedSyncRulesContent.js.map +0 -1
  151. package/src/storage/PersistedSyncRulesContent.ts +0 -174
@@ -1,59 +0,0 @@
1
- import { Logger } from '@powersync/lib-services-framework';
2
- import { HydratedSyncRules, HydrationState, SyncConfigWithErrors } from '@powersync/service-sync-rules';
3
- import { SerializedSyncPlan, UpdateSyncRulesOptions } from './BucketStorageFactory.js';
4
- import { ReplicationLock } from './ReplicationLock.js';
5
- import { StorageVersionConfig } from './StorageVersionConfig.js';
6
- export interface ParseSyncRulesOptions {
7
- defaultSchema: string;
8
- }
9
- export interface PersistedSyncRulesContentData {
10
- readonly id: number;
11
- readonly sync_rules_content: string;
12
- readonly compiled_plan: SerializedSyncPlan | null;
13
- readonly slot_name: string;
14
- /**
15
- * True if this is the "active" copy of the sync config.
16
- */
17
- readonly active: boolean;
18
- readonly storageVersion: number;
19
- readonly last_checkpoint_lsn: string | null;
20
- readonly last_fatal_error?: string | null;
21
- readonly last_fatal_error_ts?: Date | null;
22
- readonly last_keepalive_ts?: Date | null;
23
- readonly last_checkpoint_ts?: Date | null;
24
- }
25
- export declare abstract class PersistedSyncRulesContent implements PersistedSyncRulesContentData {
26
- readonly id: number;
27
- readonly sync_rules_content: string;
28
- readonly compiled_plan: SerializedSyncPlan | null;
29
- readonly slot_name: string;
30
- readonly active: boolean;
31
- readonly storageVersion: number;
32
- readonly logger: Logger;
33
- readonly last_checkpoint_lsn: string | null;
34
- readonly last_fatal_error?: string | null;
35
- readonly last_fatal_error_ts?: Date | null;
36
- readonly last_keepalive_ts?: Date | null;
37
- readonly last_checkpoint_ts?: Date | null;
38
- abstract readonly current_lock: ReplicationLock | null;
39
- constructor(data: PersistedSyncRulesContentData);
40
- /**
41
- * Load the storage config.
42
- *
43
- * This may throw if the persisted storage version is not supported.
44
- */
45
- getStorageConfig(): StorageVersionConfig;
46
- parsed(options: ParseSyncRulesOptions): PersistedSyncRules;
47
- asUpdateOptions(options?: Omit<UpdateSyncRulesOptions, 'config'>): UpdateSyncRulesOptions;
48
- abstract lock(): Promise<ReplicationLock>;
49
- }
50
- export interface PersistedSyncRules {
51
- readonly id: number;
52
- readonly sync_rules: SyncConfigWithErrors;
53
- readonly slot_name: string;
54
- /**
55
- * For testing only.
56
- */
57
- readonly hydrationState: HydrationState;
58
- hydratedSyncRules(): HydratedSyncRules;
59
- }
@@ -1,100 +0,0 @@
1
- import { logger as defaultLogger, ErrorCode, ServiceError } from '@powersync/lib-services-framework';
2
- import { CompatibilityContext, CompatibilityOption, DEFAULT_HYDRATION_STATE, deserializeSyncPlan, javaScriptExpressionEngine, PrecompiledSyncConfig, SqlEventDescriptor, SqlSyncRules, versionedHydrationState, YamlError } from '@powersync/service-sync-rules';
3
- import { STORAGE_VERSION_CONFIG } from './StorageVersionConfig.js';
4
- export class PersistedSyncRulesContent {
5
- id;
6
- sync_rules_content;
7
- compiled_plan;
8
- slot_name;
9
- active;
10
- storageVersion;
11
- logger;
12
- last_checkpoint_lsn;
13
- last_fatal_error;
14
- last_fatal_error_ts;
15
- last_keepalive_ts;
16
- last_checkpoint_ts;
17
- constructor(data) {
18
- Object.assign(this, data);
19
- this.logger = defaultLogger.child({ prefix: `[${this.slot_name}] ` });
20
- }
21
- /**
22
- * Load the storage config.
23
- *
24
- * This may throw if the persisted storage version is not supported.
25
- */
26
- getStorageConfig() {
27
- const storageConfig = STORAGE_VERSION_CONFIG[this.storageVersion];
28
- if (storageConfig == null) {
29
- throw new ServiceError(ErrorCode.PSYNC_S1005, `Unsupported storage version ${this.storageVersion} for replication stream ${this.id}`);
30
- }
31
- return storageConfig;
32
- }
33
- parsed(options) {
34
- let hydrationState;
35
- // Do we have a compiled sync plan? If so, restore from there instead of parsing everything again.
36
- let config;
37
- if (this.compiled_plan != null) {
38
- const plan = deserializeSyncPlan(this.compiled_plan.plan);
39
- const compatibility = CompatibilityContext.deserialize(this.compiled_plan.compatibility);
40
- const eventDefinitions = [];
41
- for (const [name, queries] of Object.entries(this.compiled_plan.eventDescriptors)) {
42
- const descriptor = new SqlEventDescriptor(name, compatibility);
43
- for (const query of queries) {
44
- descriptor.addSourceQuery(query, options);
45
- }
46
- eventDefinitions.push(descriptor);
47
- }
48
- const precompiled = new PrecompiledSyncConfig(plan, compatibility, eventDefinitions, {
49
- defaultSchema: options.defaultSchema,
50
- engine: javaScriptExpressionEngine(compatibility),
51
- sourceText: this.sync_rules_content
52
- });
53
- // Note: If the original content did not define a storage version, this will still set the storage version.
54
- // This means asUpdateOptions will not change the storage version, even if the default changes.
55
- precompiled.storageVersion = this.storageVersion;
56
- const errors = [];
57
- if (this.compiled_plan.errors) {
58
- for (const error of this.compiled_plan.errors) {
59
- const location = error.location && {
60
- start: error.location.start_offset,
61
- end: error.location.end_offset
62
- };
63
- const asYamlError = new YamlError(new Error(error.message), location);
64
- asYamlError.type = error.level;
65
- errors.push(asYamlError);
66
- }
67
- }
68
- config = { config: precompiled, errors };
69
- }
70
- else {
71
- config = SqlSyncRules.fromYaml(this.sync_rules_content, options);
72
- }
73
- const storageConfig = this.getStorageConfig();
74
- if (storageConfig.versionedBuckets ||
75
- config.config.compatibility.isEnabled(CompatibilityOption.versionedBucketIds)) {
76
- hydrationState = versionedHydrationState(this.id);
77
- }
78
- else {
79
- hydrationState = DEFAULT_HYDRATION_STATE;
80
- }
81
- return {
82
- id: this.id,
83
- slot_name: this.slot_name,
84
- sync_rules: config,
85
- hydrationState,
86
- hydratedSyncRules: () => {
87
- return config.config.hydrate({ hydrationState });
88
- }
89
- };
90
- }
91
- asUpdateOptions(options) {
92
- // defaultSchema is not relevant for the parsed version here
93
- const parsed = this.parsed({ defaultSchema: 'not_applicable' });
94
- return {
95
- config: { yaml: this.sync_rules_content, plan: this.compiled_plan, parsed: parsed.sync_rules },
96
- ...options
97
- };
98
- }
99
- }
100
- //# sourceMappingURL=PersistedSyncRulesContent.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PersistedSyncRulesContent.js","sourceRoot":"","sources":["../../src/storage/PersistedSyncRulesContent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,SAAS,EAAU,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAC7G,OAAO,EACL,oBAAoB,EACpB,mBAAmB,EACnB,uBAAuB,EACvB,mBAAmB,EAInB,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EAClB,YAAY,EAEZ,uBAAuB,EACvB,SAAS,EACV,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EAAE,sBAAsB,EAAwB,MAAM,2BAA2B,CAAC;AAyBzF,MAAM,OAAgB,yBAAyB;IACpC,EAAE,CAAU;IACZ,kBAAkB,CAAU;IAC5B,aAAa,CAA6B;IAC1C,SAAS,CAAU;IACnB,MAAM,CAAW;IACjB,cAAc,CAAU;IACxB,MAAM,CAAS;IAEf,mBAAmB,CAAiB;IAEpC,gBAAgB,CAAiB;IACjC,mBAAmB,CAAe;IAClC,iBAAiB,CAAe;IAChC,kBAAkB,CAAe;IAI1C,YAAY,IAAmC;QAC7C,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,IAAI,IAAI,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC;IACxE,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,MAAM,aAAa,GAAG,sBAAsB,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC;QAClE,IAAI,aAAa,IAAI,IAAI,EAAE,CAAC;YAC1B,MAAM,IAAI,YAAY,CACpB,SAAS,CAAC,WAAW,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AC,EAAE,cAAc,EAAE,CAAC,CAAC;YACnD,CAAC;SACF,CAAC;IACJ,CAAC;IAED,eAAe,CAAC,OAAgD;QAC9D,4DAA4D;QAC5D,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,gBAAgB,EAAE,CAAC,CAAC;QAChE,OAAO;YACL,MAAM,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,kBAAkB,EAAE,IAAI,EAAE,IAAI,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,CAAC,UAAU,EAAE;YAC9F,GAAG,OAAO;SACX,CAAC;IACJ,CAAC;CAGF"}
@@ -1,174 +0,0 @@
1
- import { logger as defaultLogger, ErrorCode, Logger, ServiceError } from '@powersync/lib-services-framework';
2
- import {
3
- CompatibilityContext,
4
- CompatibilityOption,
5
- DEFAULT_HYDRATION_STATE,
6
- deserializeSyncPlan,
7
- ErrorLocation,
8
- HydratedSyncRules,
9
- HydrationState,
10
- javaScriptExpressionEngine,
11
- PrecompiledSyncConfig,
12
- SqlEventDescriptor,
13
- SqlSyncRules,
14
- SyncConfigWithErrors,
15
- versionedHydrationState,
16
- YamlError
17
- } from '@powersync/service-sync-rules';
18
- import { SerializedSyncPlan, UpdateSyncRulesOptions } from './BucketStorageFactory.js';
19
- import { ReplicationLock } from './ReplicationLock.js';
20
- import { STORAGE_VERSION_CONFIG, StorageVersionConfig } from './StorageVersionConfig.js';
21
-
22
- export interface ParseSyncRulesOptions {
23
- defaultSchema: string;
24
- }
25
-
26
- export interface PersistedSyncRulesContentData {
27
- readonly id: number;
28
- readonly sync_rules_content: string;
29
- readonly compiled_plan: SerializedSyncPlan | null;
30
- readonly slot_name: string;
31
- /**
32
- * True if this is the "active" copy of the sync config.
33
- */
34
- readonly active: boolean;
35
- readonly storageVersion: number;
36
-
37
- readonly last_checkpoint_lsn: string | null;
38
-
39
- readonly last_fatal_error?: string | null;
40
- readonly last_fatal_error_ts?: Date | null;
41
- readonly last_keepalive_ts?: Date | null;
42
- readonly last_checkpoint_ts?: Date | null;
43
- }
44
-
45
- export abstract class PersistedSyncRulesContent implements PersistedSyncRulesContentData {
46
- readonly id!: number;
47
- readonly sync_rules_content!: string;
48
- readonly compiled_plan!: SerializedSyncPlan | null;
49
- readonly slot_name!: string;
50
- readonly active!: boolean;
51
- readonly storageVersion!: number;
52
- readonly logger: Logger;
53
-
54
- readonly last_checkpoint_lsn!: string | null;
55
-
56
- readonly last_fatal_error?: string | null;
57
- readonly last_fatal_error_ts?: Date | null;
58
- readonly last_keepalive_ts?: Date | null;
59
- readonly last_checkpoint_ts?: Date | null;
60
-
61
- abstract readonly current_lock: ReplicationLock | null;
62
-
63
- constructor(data: PersistedSyncRulesContentData) {
64
- Object.assign(this, data);
65
- this.logger = defaultLogger.child({ prefix: `[${this.slot_name}] ` });
66
- }
67
-
68
- /**
69
- * Load the storage config.
70
- *
71
- * This may throw if the persisted storage version is not supported.
72
- */
73
- getStorageConfig(): StorageVersionConfig {
74
- const storageConfig = STORAGE_VERSION_CONFIG[this.storageVersion];
75
- if (storageConfig == null) {
76
- throw new ServiceError(
77
- ErrorCode.PSYNC_S1005,
78
- `Unsupported storage version ${this.storageVersion} for replication stream ${this.id}`
79
- );
80
- }
81
- return storageConfig;
82
- }
83
-
84
- parsed(options: ParseSyncRulesOptions): PersistedSyncRules {
85
- let hydrationState: HydrationState;
86
-
87
- // Do we have a compiled sync plan? If so, restore from there instead of parsing everything again.
88
- let config: SyncConfigWithErrors;
89
- if (this.compiled_plan != null) {
90
- const plan = deserializeSyncPlan(this.compiled_plan.plan);
91
- const compatibility = CompatibilityContext.deserialize(this.compiled_plan.compatibility);
92
- const eventDefinitions: SqlEventDescriptor[] = [];
93
- for (const [name, queries] of Object.entries(this.compiled_plan.eventDescriptors)) {
94
- const descriptor = new SqlEventDescriptor(name, compatibility);
95
- for (const query of queries) {
96
- descriptor.addSourceQuery(query, options);
97
- }
98
-
99
- eventDefinitions.push(descriptor);
100
- }
101
-
102
- const precompiled = new PrecompiledSyncConfig(plan, compatibility, eventDefinitions, {
103
- defaultSchema: options.defaultSchema,
104
- engine: javaScriptExpressionEngine(compatibility),
105
- sourceText: this.sync_rules_content
106
- });
107
-
108
- // Note: If the original content did not define a storage version, this will still set the storage version.
109
- // This means asUpdateOptions will not change the storage version, even if the default changes.
110
- precompiled.storageVersion = this.storageVersion;
111
-
112
- const errors: YamlError[] = [];
113
- if (this.compiled_plan.errors) {
114
- for (const error of this.compiled_plan.errors) {
115
- const location: ErrorLocation | undefined = error.location && {
116
- start: error.location.start_offset,
117
- end: error.location.end_offset
118
- };
119
- const asYamlError = new YamlError(new Error(error.message), location);
120
- asYamlError.type = error.level;
121
-
122
- errors.push(asYamlError);
123
- }
124
- }
125
-
126
- config = { config: precompiled, errors };
127
- } else {
128
- config = SqlSyncRules.fromYaml(this.sync_rules_content, options);
129
- }
130
-
131
- const storageConfig = this.getStorageConfig();
132
- if (
133
- storageConfig.versionedBuckets ||
134
- config.config.compatibility.isEnabled(CompatibilityOption.versionedBucketIds)
135
- ) {
136
- hydrationState = versionedHydrationState(this.id);
137
- } else {
138
- hydrationState = DEFAULT_HYDRATION_STATE;
139
- }
140
-
141
- return {
142
- id: this.id,
143
- slot_name: this.slot_name,
144
- sync_rules: config,
145
- hydrationState,
146
- hydratedSyncRules: () => {
147
- return config.config.hydrate({ hydrationState });
148
- }
149
- };
150
- }
151
-
152
- asUpdateOptions(options?: Omit<UpdateSyncRulesOptions, 'config'>): UpdateSyncRulesOptions {
153
- // defaultSchema is not relevant for the parsed version here
154
- const parsed = this.parsed({ defaultSchema: 'not_applicable' });
155
- return {
156
- config: { yaml: this.sync_rules_content, plan: this.compiled_plan, parsed: parsed.sync_rules },
157
- ...options
158
- };
159
- }
160
-
161
- abstract lock(): Promise<ReplicationLock>;
162
- }
163
-
164
- export interface PersistedSyncRules {
165
- readonly id: number;
166
- readonly sync_rules: SyncConfigWithErrors;
167
- readonly slot_name: string;
168
- /**
169
- * For testing only.
170
- */
171
- readonly hydrationState: HydrationState;
172
-
173
- hydratedSyncRules(): HydratedSyncRules;
174
- }