-
Notifications
You must be signed in to change notification settings - Fork 55
Add SyncConfig superclass for sync configurations
#492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
96512ee
Add sync definition superinterface
simolus3 947c04c
Remove some unintentional changes
simolus3 59b0f49
Don't expose BaseSyncConfig
simolus3 1d53ea7
BaseSyncConfig -> SyncConfig
simolus3 612ab42
Remove extractSyncPlan
simolus3 ef22766
Remove superfluous method
simolus3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| '@powersync/service-sync-rules': minor | ||
| '@powersync/service-module-postgres-storage': patch | ||
| '@powersync/service-module-mongodb-storage': patch | ||
| '@powersync/service-core-tests': patch | ||
| '@powersync/service-module-postgres': patch | ||
| '@powersync/service-module-mongodb': patch | ||
| '@powersync/service-core': patch | ||
| --- | ||
|
|
||
| Introduce `BaseSyncConfig` to represent SQL-based sync rules and precompiled sync plans. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { BucketDataSource, BucketSource, CreateSourceParams, ParameterIndexLookupCreator } from './BucketSource.js'; | ||
| import { CompatibilityContext, CompatibilityOption } from './compatibility.js'; | ||
| import { YamlError } from './errors.js'; | ||
| import { SqlEventDescriptor } from './events/SqlEventDescriptor.js'; | ||
| import { HydratedSyncRules } from './HydratedSyncRules.js'; | ||
| import { DEFAULT_HYDRATION_STATE } from './HydrationState.js'; | ||
| import { SourceTableInterface } from './SourceTableInterface.js'; | ||
| import { SyncPlan } from './sync_plan/plan.js'; | ||
| import { TablePattern } from './TablePattern.js'; | ||
| import { SqliteInputValue, SqliteRow, SqliteValue, SyncConfig } from './types.js'; | ||
| import { applyRowContext } from './utils.js'; | ||
|
|
||
| /** | ||
| * @internal Sealed class, can only be extended by `SqlSyncRules` and `PrecompiledSyncConfig`. | ||
| */ | ||
| export abstract class BaseSyncConfig { | ||
| bucketDataSources: BucketDataSource[] = []; | ||
| bucketParameterLookupSources: ParameterIndexLookupCreator[] = []; | ||
| bucketSources: BucketSource[] = []; | ||
| compatibility: CompatibilityContext = CompatibilityContext.FULL_BACKWARDS_COMPATIBILITY; | ||
| eventDescriptors: SqlEventDescriptor[] = []; | ||
|
|
||
| /** | ||
| * The (YAML-based) source contents from which these sync rules have been derived. | ||
| */ | ||
| content: string; | ||
|
|
||
| constructor(content: string) { | ||
| this.content = content; | ||
| } | ||
|
|
||
| // Ensure asSyncConfig is not implemented externally | ||
| protected abstract asSyncConfig(): SyncConfig & this; | ||
|
|
||
| /** | ||
| * If this sync config is fully described by a serializable sync plan, returns that plan. | ||
| */ | ||
| abstract extractSyncPlan(): SyncPlan | null; | ||
|
|
||
| /** | ||
| * Hydrate the sync rule definitions with persisted state into runnable sync rules. | ||
| * | ||
| * @param params.hydrationState Transforms bucket ids based on persisted state. May omit for tests. | ||
| */ | ||
| hydrate(params?: CreateSourceParams): HydratedSyncRules { | ||
| let hydrationState = params?.hydrationState; | ||
| if (hydrationState == null || !this.compatibility.isEnabled(CompatibilityOption.versionedBucketIds)) { | ||
| hydrationState = DEFAULT_HYDRATION_STATE; | ||
| } | ||
| const resolvedParams = { hydrationState }; | ||
| return new HydratedSyncRules({ | ||
| definition: this.asSyncConfig(), | ||
| createParams: resolvedParams, | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.