@powersync/service-core 0.0.2
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.
- package/.probes/.gitkeep +0 -0
- package/CHANGELOG.md +13 -0
- package/LICENSE +67 -0
- package/README.md +3 -0
- package/dist/api/api-index.d.ts +2 -0
- package/dist/api/api-index.js +3 -0
- package/dist/api/api-index.js.map +1 -0
- package/dist/api/diagnostics.d.ts +21 -0
- package/dist/api/diagnostics.js +183 -0
- package/dist/api/diagnostics.js.map +1 -0
- package/dist/api/schema.d.ts +5 -0
- package/dist/api/schema.js +88 -0
- package/dist/api/schema.js.map +1 -0
- package/dist/auth/CachedKeyCollector.d.ts +46 -0
- package/dist/auth/CachedKeyCollector.js +116 -0
- package/dist/auth/CachedKeyCollector.js.map +1 -0
- package/dist/auth/CompoundKeyCollector.d.ts +8 -0
- package/dist/auth/CompoundKeyCollector.js +23 -0
- package/dist/auth/CompoundKeyCollector.js.map +1 -0
- package/dist/auth/JwtPayload.d.ts +10 -0
- package/dist/auth/JwtPayload.js +2 -0
- package/dist/auth/JwtPayload.js.map +1 -0
- package/dist/auth/KeyCollector.d.ts +24 -0
- package/dist/auth/KeyCollector.js +2 -0
- package/dist/auth/KeyCollector.js.map +1 -0
- package/dist/auth/KeySpec.d.ts +26 -0
- package/dist/auth/KeySpec.js +49 -0
- package/dist/auth/KeySpec.js.map +1 -0
- package/dist/auth/KeyStore.d.ts +39 -0
- package/dist/auth/KeyStore.js +131 -0
- package/dist/auth/KeyStore.js.map +1 -0
- package/dist/auth/LeakyBucket.d.ts +39 -0
- package/dist/auth/LeakyBucket.js +57 -0
- package/dist/auth/LeakyBucket.js.map +1 -0
- package/dist/auth/RemoteJWKSCollector.d.ts +24 -0
- package/dist/auth/RemoteJWKSCollector.js +106 -0
- package/dist/auth/RemoteJWKSCollector.js.map +1 -0
- package/dist/auth/StaticKeyCollector.d.ts +14 -0
- package/dist/auth/StaticKeyCollector.js +19 -0
- package/dist/auth/StaticKeyCollector.js.map +1 -0
- package/dist/auth/SupabaseKeyCollector.d.ts +22 -0
- package/dist/auth/SupabaseKeyCollector.js +61 -0
- package/dist/auth/SupabaseKeyCollector.js.map +1 -0
- package/dist/auth/auth-index.d.ts +10 -0
- package/dist/auth/auth-index.js +11 -0
- package/dist/auth/auth-index.js.map +1 -0
- package/dist/db/db-index.d.ts +1 -0
- package/dist/db/db-index.js +2 -0
- package/dist/db/db-index.js.map +1 -0
- package/dist/db/mongo.d.ts +29 -0
- package/dist/db/mongo.js +65 -0
- package/dist/db/mongo.js.map +1 -0
- package/dist/entry/cli-entry.d.ts +15 -0
- package/dist/entry/cli-entry.js +36 -0
- package/dist/entry/cli-entry.js.map +1 -0
- package/dist/entry/commands/config-command.d.ts +10 -0
- package/dist/entry/commands/config-command.js +21 -0
- package/dist/entry/commands/config-command.js.map +1 -0
- package/dist/entry/commands/migrate-action.d.ts +2 -0
- package/dist/entry/commands/migrate-action.js +18 -0
- package/dist/entry/commands/migrate-action.js.map +1 -0
- package/dist/entry/commands/start-action.d.ts +3 -0
- package/dist/entry/commands/start-action.js +15 -0
- package/dist/entry/commands/start-action.js.map +1 -0
- package/dist/entry/commands/teardown-action.d.ts +2 -0
- package/dist/entry/commands/teardown-action.js +17 -0
- package/dist/entry/commands/teardown-action.js.map +1 -0
- package/dist/entry/entry-index.d.ts +5 -0
- package/dist/entry/entry-index.js +6 -0
- package/dist/entry/entry-index.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/metrics/metrics.d.ts +16 -0
- package/dist/metrics/metrics.js +139 -0
- package/dist/metrics/metrics.js.map +1 -0
- package/dist/migrations/db/migrations/1684951997326-init.d.ts +3 -0
- package/dist/migrations/db/migrations/1684951997326-init.js +31 -0
- package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +2 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +5 -0
- package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +1 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +3 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +54 -0
- package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +3 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +27 -0
- package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -0
- package/dist/migrations/db/store.d.ts +3 -0
- package/dist/migrations/db/store.js +10 -0
- package/dist/migrations/db/store.js.map +1 -0
- package/dist/migrations/migrations.d.ts +10 -0
- package/dist/migrations/migrations.js +94 -0
- package/dist/migrations/migrations.js.map +1 -0
- package/dist/replication/ErrorRateLimiter.d.ts +17 -0
- package/dist/replication/ErrorRateLimiter.js +42 -0
- package/dist/replication/ErrorRateLimiter.js.map +1 -0
- package/dist/replication/PgRelation.d.ts +16 -0
- package/dist/replication/PgRelation.js +26 -0
- package/dist/replication/PgRelation.js.map +1 -0
- package/dist/replication/WalConnection.d.ts +34 -0
- package/dist/replication/WalConnection.js +190 -0
- package/dist/replication/WalConnection.js.map +1 -0
- package/dist/replication/WalStream.d.ts +58 -0
- package/dist/replication/WalStream.js +517 -0
- package/dist/replication/WalStream.js.map +1 -0
- package/dist/replication/WalStreamManager.d.ts +30 -0
- package/dist/replication/WalStreamManager.js +199 -0
- package/dist/replication/WalStreamManager.js.map +1 -0
- package/dist/replication/WalStreamRunner.d.ts +38 -0
- package/dist/replication/WalStreamRunner.js +155 -0
- package/dist/replication/WalStreamRunner.js.map +1 -0
- package/dist/replication/replication-index.d.ts +7 -0
- package/dist/replication/replication-index.js +8 -0
- package/dist/replication/replication-index.js.map +1 -0
- package/dist/replication/util.d.ts +9 -0
- package/dist/replication/util.js +62 -0
- package/dist/replication/util.js.map +1 -0
- package/dist/routes/admin.d.ts +7 -0
- package/dist/routes/admin.js +192 -0
- package/dist/routes/admin.js.map +1 -0
- package/dist/routes/auth.d.ts +58 -0
- package/dist/routes/auth.js +182 -0
- package/dist/routes/auth.js.map +1 -0
- package/dist/routes/checkpointing.d.ts +3 -0
- package/dist/routes/checkpointing.js +30 -0
- package/dist/routes/checkpointing.js.map +1 -0
- package/dist/routes/dev.d.ts +6 -0
- package/dist/routes/dev.js +163 -0
- package/dist/routes/dev.js.map +1 -0
- package/dist/routes/route-generators.d.ts +15 -0
- package/dist/routes/route-generators.js +32 -0
- package/dist/routes/route-generators.js.map +1 -0
- package/dist/routes/router-socket.d.ts +10 -0
- package/dist/routes/router-socket.js +5 -0
- package/dist/routes/router-socket.js.map +1 -0
- package/dist/routes/router.d.ts +13 -0
- package/dist/routes/router.js +2 -0
- package/dist/routes/router.js.map +1 -0
- package/dist/routes/routes-index.d.ts +4 -0
- package/dist/routes/routes-index.js +5 -0
- package/dist/routes/routes-index.js.map +1 -0
- package/dist/routes/socket-route.d.ts +2 -0
- package/dist/routes/socket-route.js +119 -0
- package/dist/routes/socket-route.js.map +1 -0
- package/dist/routes/sync-rules.d.ts +6 -0
- package/dist/routes/sync-rules.js +182 -0
- package/dist/routes/sync-rules.js.map +1 -0
- package/dist/routes/sync-stream.d.ts +5 -0
- package/dist/routes/sync-stream.js +74 -0
- package/dist/routes/sync-stream.js.map +1 -0
- package/dist/runner/teardown.d.ts +2 -0
- package/dist/runner/teardown.js +79 -0
- package/dist/runner/teardown.js.map +1 -0
- package/dist/storage/BucketStorage.d.ts +298 -0
- package/dist/storage/BucketStorage.js +25 -0
- package/dist/storage/BucketStorage.js.map +1 -0
- package/dist/storage/MongoBucketStorage.d.ts +51 -0
- package/dist/storage/MongoBucketStorage.js +388 -0
- package/dist/storage/MongoBucketStorage.js.map +1 -0
- package/dist/storage/SourceTable.d.ts +39 -0
- package/dist/storage/SourceTable.js +50 -0
- package/dist/storage/SourceTable.js.map +1 -0
- package/dist/storage/mongo/MongoBucketBatch.d.ts +48 -0
- package/dist/storage/mongo/MongoBucketBatch.js +584 -0
- package/dist/storage/mongo/MongoBucketBatch.js.map +1 -0
- package/dist/storage/mongo/MongoIdSequence.d.ts +12 -0
- package/dist/storage/mongo/MongoIdSequence.js +21 -0
- package/dist/storage/mongo/MongoIdSequence.js.map +1 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +9 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.js +9 -0
- package/dist/storage/mongo/MongoPersistedSyncRules.js.map +1 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +20 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +26 -0
- package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +1 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +27 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.js +379 -0
- package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -0
- package/dist/storage/mongo/MongoSyncRulesLock.d.ts +16 -0
- package/dist/storage/mongo/MongoSyncRulesLock.js +65 -0
- package/dist/storage/mongo/MongoSyncRulesLock.js.map +1 -0
- package/dist/storage/mongo/OperationBatch.d.ts +26 -0
- package/dist/storage/mongo/OperationBatch.js +101 -0
- package/dist/storage/mongo/OperationBatch.js.map +1 -0
- package/dist/storage/mongo/PersistedBatch.d.ts +42 -0
- package/dist/storage/mongo/PersistedBatch.js +200 -0
- package/dist/storage/mongo/PersistedBatch.js.map +1 -0
- package/dist/storage/mongo/db.d.ts +23 -0
- package/dist/storage/mongo/db.js +34 -0
- package/dist/storage/mongo/db.js.map +1 -0
- package/dist/storage/mongo/models.d.ts +137 -0
- package/dist/storage/mongo/models.js +27 -0
- package/dist/storage/mongo/models.js.map +1 -0
- package/dist/storage/mongo/util.d.ts +26 -0
- package/dist/storage/mongo/util.js +81 -0
- package/dist/storage/mongo/util.js.map +1 -0
- package/dist/storage/storage-index.d.ts +14 -0
- package/dist/storage/storage-index.js +15 -0
- package/dist/storage/storage-index.js.map +1 -0
- package/dist/sync/BroadcastIterable.d.ts +38 -0
- package/dist/sync/BroadcastIterable.js +153 -0
- package/dist/sync/BroadcastIterable.js.map +1 -0
- package/dist/sync/LastValueSink.d.ts +25 -0
- package/dist/sync/LastValueSink.js +84 -0
- package/dist/sync/LastValueSink.js.map +1 -0
- package/dist/sync/merge.d.ts +39 -0
- package/dist/sync/merge.js +175 -0
- package/dist/sync/merge.js.map +1 -0
- package/dist/sync/safeRace.d.ts +1 -0
- package/dist/sync/safeRace.js +91 -0
- package/dist/sync/safeRace.js.map +1 -0
- package/dist/sync/sync-index.d.ts +6 -0
- package/dist/sync/sync-index.js +7 -0
- package/dist/sync/sync-index.js.map +1 -0
- package/dist/sync/sync.d.ts +18 -0
- package/dist/sync/sync.js +248 -0
- package/dist/sync/sync.js.map +1 -0
- package/dist/sync/util.d.ts +26 -0
- package/dist/sync/util.js +73 -0
- package/dist/sync/util.js.map +1 -0
- package/dist/system/CorePowerSyncSystem.d.ts +18 -0
- package/dist/system/CorePowerSyncSystem.js +28 -0
- package/dist/system/CorePowerSyncSystem.js.map +1 -0
- package/dist/util/Mutex.d.ts +47 -0
- package/dist/util/Mutex.js +132 -0
- package/dist/util/Mutex.js.map +1 -0
- package/dist/util/PgManager.d.ts +24 -0
- package/dist/util/PgManager.js +55 -0
- package/dist/util/PgManager.js.map +1 -0
- package/dist/util/alerting.d.ts +4 -0
- package/dist/util/alerting.js +14 -0
- package/dist/util/alerting.js.map +1 -0
- package/dist/util/config/collectors/config-collector.d.ts +29 -0
- package/dist/util/config/collectors/config-collector.js +116 -0
- package/dist/util/config/collectors/config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/base64-config-collector.d.ts +6 -0
- package/dist/util/config/collectors/impl/base64-config-collector.js +15 -0
- package/dist/util/config/collectors/impl/base64-config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.d.ts +11 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.js +19 -0
- package/dist/util/config/collectors/impl/fallback-config-collector.js.map +1 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.d.ts +6 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.js +35 -0
- package/dist/util/config/collectors/impl/filesystem-config-collector.js.map +1 -0
- package/dist/util/config/compound-config-collector.d.ts +32 -0
- package/dist/util/config/compound-config-collector.js +126 -0
- package/dist/util/config/compound-config-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js +17 -0
- package/dist/util/config/sync-rules/impl/base64-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js +21 -0
- package/dist/util/config/sync-rules/impl/filesystem-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.d.ts +7 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js +17 -0
- package/dist/util/config/sync-rules/impl/inline-sync-rules-collector.js.map +1 -0
- package/dist/util/config/sync-rules/sync-collector.d.ts +6 -0
- package/dist/util/config/sync-rules/sync-collector.js +3 -0
- package/dist/util/config/sync-rules/sync-collector.js.map +1 -0
- package/dist/util/config/types.d.ts +53 -0
- package/dist/util/config/types.js +7 -0
- package/dist/util/config/types.js.map +1 -0
- package/dist/util/config.d.ts +7 -0
- package/dist/util/config.js +35 -0
- package/dist/util/config.js.map +1 -0
- package/dist/util/env.d.ts +10 -0
- package/dist/util/env.js +25 -0
- package/dist/util/env.js.map +1 -0
- package/dist/util/memory-tracking.d.ts +7 -0
- package/dist/util/memory-tracking.js +58 -0
- package/dist/util/memory-tracking.js.map +1 -0
- package/dist/util/migration_lib.d.ts +11 -0
- package/dist/util/migration_lib.js +64 -0
- package/dist/util/migration_lib.js.map +1 -0
- package/dist/util/pgwire_utils.d.ts +24 -0
- package/dist/util/pgwire_utils.js +117 -0
- package/dist/util/pgwire_utils.js.map +1 -0
- package/dist/util/populate_test_data.d.ts +8 -0
- package/dist/util/populate_test_data.js +65 -0
- package/dist/util/populate_test_data.js.map +1 -0
- package/dist/util/protocol-types.d.ts +178 -0
- package/dist/util/protocol-types.js +38 -0
- package/dist/util/protocol-types.js.map +1 -0
- package/dist/util/secs.d.ts +2 -0
- package/dist/util/secs.js +49 -0
- package/dist/util/secs.js.map +1 -0
- package/dist/util/util-index.d.ts +22 -0
- package/dist/util/util-index.js +23 -0
- package/dist/util/util-index.js.map +1 -0
- package/dist/util/utils.d.ts +14 -0
- package/dist/util/utils.js +75 -0
- package/dist/util/utils.js.map +1 -0
- package/package.json +55 -0
- package/src/api/api-index.ts +2 -0
- package/src/api/diagnostics.ts +221 -0
- package/src/api/schema.ts +99 -0
- package/src/auth/CachedKeyCollector.ts +132 -0
- package/src/auth/CompoundKeyCollector.ts +33 -0
- package/src/auth/JwtPayload.ts +11 -0
- package/src/auth/KeyCollector.ts +27 -0
- package/src/auth/KeySpec.ts +67 -0
- package/src/auth/KeyStore.ts +156 -0
- package/src/auth/LeakyBucket.ts +66 -0
- package/src/auth/RemoteJWKSCollector.ts +130 -0
- package/src/auth/StaticKeyCollector.ts +21 -0
- package/src/auth/SupabaseKeyCollector.ts +67 -0
- package/src/auth/auth-index.ts +10 -0
- package/src/db/db-index.ts +1 -0
- package/src/db/mongo.ts +72 -0
- package/src/entry/cli-entry.ts +41 -0
- package/src/entry/commands/config-command.ts +36 -0
- package/src/entry/commands/migrate-action.ts +25 -0
- package/src/entry/commands/start-action.ts +24 -0
- package/src/entry/commands/teardown-action.ts +23 -0
- package/src/entry/entry-index.ts +5 -0
- package/src/index.ts +37 -0
- package/src/metrics/metrics.ts +169 -0
- package/src/migrations/db/migrations/1684951997326-init.ts +33 -0
- package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +5 -0
- package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +99 -0
- package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +32 -0
- package/src/migrations/db/store.ts +11 -0
- package/src/migrations/migrations.ts +122 -0
- package/src/replication/ErrorRateLimiter.ts +49 -0
- package/src/replication/PgRelation.ts +42 -0
- package/src/replication/WalConnection.ts +227 -0
- package/src/replication/WalStream.ts +626 -0
- package/src/replication/WalStreamManager.ts +214 -0
- package/src/replication/WalStreamRunner.ts +180 -0
- package/src/replication/replication-index.ts +7 -0
- package/src/replication/util.ts +76 -0
- package/src/routes/admin.ts +229 -0
- package/src/routes/auth.ts +209 -0
- package/src/routes/checkpointing.ts +38 -0
- package/src/routes/dev.ts +194 -0
- package/src/routes/route-generators.ts +39 -0
- package/src/routes/router-socket.ts +13 -0
- package/src/routes/router.ts +17 -0
- package/src/routes/routes-index.ts +5 -0
- package/src/routes/socket-route.ts +131 -0
- package/src/routes/sync-rules.ts +210 -0
- package/src/routes/sync-stream.ts +92 -0
- package/src/runner/teardown.ts +91 -0
- package/src/storage/BucketStorage.ts +386 -0
- package/src/storage/MongoBucketStorage.ts +493 -0
- package/src/storage/SourceTable.ts +60 -0
- package/src/storage/mongo/MongoBucketBatch.ts +756 -0
- package/src/storage/mongo/MongoIdSequence.ts +24 -0
- package/src/storage/mongo/MongoPersistedSyncRules.ts +16 -0
- package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +47 -0
- package/src/storage/mongo/MongoSyncBucketStorage.ts +517 -0
- package/src/storage/mongo/MongoSyncRulesLock.ts +81 -0
- package/src/storage/mongo/OperationBatch.ts +115 -0
- package/src/storage/mongo/PersistedBatch.ts +245 -0
- package/src/storage/mongo/db.ts +69 -0
- package/src/storage/mongo/models.ts +157 -0
- package/src/storage/mongo/util.ts +88 -0
- package/src/storage/storage-index.ts +15 -0
- package/src/sync/BroadcastIterable.ts +161 -0
- package/src/sync/LastValueSink.ts +100 -0
- package/src/sync/merge.ts +200 -0
- package/src/sync/safeRace.ts +99 -0
- package/src/sync/sync-index.ts +6 -0
- package/src/sync/sync.ts +312 -0
- package/src/sync/util.ts +98 -0
- package/src/system/CorePowerSyncSystem.ts +43 -0
- package/src/util/Mutex.ts +159 -0
- package/src/util/PgManager.ts +64 -0
- package/src/util/alerting.ts +17 -0
- package/src/util/config/collectors/config-collector.ts +141 -0
- package/src/util/config/collectors/impl/base64-config-collector.ts +18 -0
- package/src/util/config/collectors/impl/fallback-config-collector.ts +22 -0
- package/src/util/config/collectors/impl/filesystem-config-collector.ts +41 -0
- package/src/util/config/compound-config-collector.ts +171 -0
- package/src/util/config/sync-rules/impl/base64-sync-rules-collector.ts +21 -0
- package/src/util/config/sync-rules/impl/filesystem-sync-rules-collector.ts +26 -0
- package/src/util/config/sync-rules/impl/inline-sync-rules-collector.ts +21 -0
- package/src/util/config/sync-rules/sync-collector.ts +8 -0
- package/src/util/config/types.ts +60 -0
- package/src/util/config.ts +39 -0
- package/src/util/env.ts +28 -0
- package/src/util/memory-tracking.ts +67 -0
- package/src/util/migration_lib.ts +79 -0
- package/src/util/pgwire_utils.ts +139 -0
- package/src/util/populate_test_data.ts +78 -0
- package/src/util/protocol-types.ts +223 -0
- package/src/util/secs.ts +54 -0
- package/src/util/util-index.ts +25 -0
- package/src/util/utils.ts +102 -0
- package/test/src/__snapshots__/pg_test.test.ts.snap +256 -0
- package/test/src/__snapshots__/sync.test.ts.snap +235 -0
- package/test/src/auth.test.ts +340 -0
- package/test/src/broadcast_iterable.test.ts +156 -0
- package/test/src/data_storage.test.ts +1176 -0
- package/test/src/env.ts +8 -0
- package/test/src/large_batch.test.ts +194 -0
- package/test/src/merge_iterable.test.ts +355 -0
- package/test/src/pg_test.test.ts +432 -0
- package/test/src/schema_changes.test.ts +545 -0
- package/test/src/slow_tests.test.ts +257 -0
- package/test/src/sql_functions.test.ts +254 -0
- package/test/src/sql_operators.test.ts +132 -0
- package/test/src/sync.test.ts +293 -0
- package/test/src/sync_rules.test.ts +1051 -0
- package/test/src/util.ts +67 -0
- package/test/src/validation.test.ts +63 -0
- package/test/src/wal_stream.test.ts +310 -0
- package/test/src/wal_stream_utils.ts +147 -0
- package/test/tsconfig.json +20 -0
- package/tsconfig.json +20 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import * as bson from 'bson';
|
|
2
|
+
import { ToastableSqliteRow } from '@powersync/service-sync-rules';
|
|
3
|
+
|
|
4
|
+
import * as util from '@/util/util-index.js';
|
|
5
|
+
import { SaveOptions } from '../BucketStorage.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Maximum number of operations in a batch.
|
|
9
|
+
*/
|
|
10
|
+
const MAX_BATCH_COUNT = 2000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Maximum size of operations in the batch (estimated).
|
|
14
|
+
*/
|
|
15
|
+
const MAX_RECORD_BATCH_SIZE = 14_000_000;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Maximum size of size of current_data documents we lookup at a time.
|
|
19
|
+
*/
|
|
20
|
+
const MAX_CURRENT_DATA_BATCH_SIZE = 16_000_000;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Batch of input operations.
|
|
24
|
+
*
|
|
25
|
+
* We accumulate operations up to MAX_RECORD_BATCH_SIZE,
|
|
26
|
+
* then further split into sub-batches if MAX_CURRENT_DATA_BATCH_SIZE is exceeded.
|
|
27
|
+
*/
|
|
28
|
+
export class OperationBatch {
|
|
29
|
+
batch: RecordOperation[] = [];
|
|
30
|
+
currentSize: number = 0;
|
|
31
|
+
|
|
32
|
+
get length() {
|
|
33
|
+
return this.batch.length;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
push(op: RecordOperation) {
|
|
37
|
+
this.batch.push(op);
|
|
38
|
+
this.currentSize += op.estimatedSize;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
shouldFlush() {
|
|
42
|
+
return this.batch.length >= MAX_BATCH_COUNT || this.currentSize > MAX_RECORD_BATCH_SIZE;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
*batched(sizes: Map<string, number>): Generator<RecordOperation[]> {
|
|
46
|
+
let currentBatch: RecordOperation[] = [];
|
|
47
|
+
let currentBatchSize = 0;
|
|
48
|
+
for (let op of this.batch) {
|
|
49
|
+
const key = op.internalBeforeKey;
|
|
50
|
+
const size = sizes.get(key) ?? 0;
|
|
51
|
+
if (currentBatchSize + size > MAX_CURRENT_DATA_BATCH_SIZE && currentBatch.length > 0) {
|
|
52
|
+
yield currentBatch;
|
|
53
|
+
currentBatch = [];
|
|
54
|
+
currentBatchSize = 0;
|
|
55
|
+
}
|
|
56
|
+
currentBatchSize += size;
|
|
57
|
+
currentBatch.push(op);
|
|
58
|
+
}
|
|
59
|
+
if (currentBatch.length > 0) {
|
|
60
|
+
yield currentBatch;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class RecordOperation {
|
|
66
|
+
public readonly afterId: bson.UUID | null;
|
|
67
|
+
public readonly beforeId: bson.UUID;
|
|
68
|
+
public readonly internalBeforeKey: string;
|
|
69
|
+
public readonly internalAfterKey: string | null;
|
|
70
|
+
public readonly estimatedSize: number;
|
|
71
|
+
|
|
72
|
+
constructor(public readonly record: SaveOptions) {
|
|
73
|
+
const after = record.after;
|
|
74
|
+
const afterId = after ? util.getUuidReplicaIdentityBson(after, record.sourceTable.replicaIdColumns!) : null;
|
|
75
|
+
const beforeId = record.before
|
|
76
|
+
? util.getUuidReplicaIdentityBson(record.before, record.sourceTable.replicaIdColumns!)
|
|
77
|
+
: afterId!;
|
|
78
|
+
this.afterId = afterId;
|
|
79
|
+
this.beforeId = beforeId;
|
|
80
|
+
this.internalBeforeKey = cacheKey(record.sourceTable.id, beforeId);
|
|
81
|
+
this.internalAfterKey = afterId ? cacheKey(record.sourceTable.id, afterId) : null;
|
|
82
|
+
|
|
83
|
+
this.estimatedSize = estimateRowSize(record.before) + estimateRowSize(record.after);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function cacheKey(table: bson.ObjectId, id: bson.UUID) {
|
|
88
|
+
return `${table.toHexString()}.${id.toHexString()}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Estimate in-memory size of row.
|
|
93
|
+
*/
|
|
94
|
+
function estimateRowSize(record: ToastableSqliteRow | undefined) {
|
|
95
|
+
if (record == null) {
|
|
96
|
+
return 12;
|
|
97
|
+
}
|
|
98
|
+
let size = 0;
|
|
99
|
+
for (let [key, value] of Object.entries(record)) {
|
|
100
|
+
size += 12 + key.length;
|
|
101
|
+
// number | string | null | bigint | Uint8Array
|
|
102
|
+
if (value == null) {
|
|
103
|
+
size += 4;
|
|
104
|
+
} else if (typeof value == 'number') {
|
|
105
|
+
size += 8;
|
|
106
|
+
} else if (typeof value == 'bigint') {
|
|
107
|
+
size += 8;
|
|
108
|
+
} else if (typeof value == 'string') {
|
|
109
|
+
size += value.length;
|
|
110
|
+
} else if (value instanceof Uint8Array) {
|
|
111
|
+
size += value.byteLength;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return size;
|
|
115
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { JSONBig } from '@powersync/service-jsonbig';
|
|
2
|
+
import { EvaluatedParameters, EvaluatedRow } from '@powersync/service-sync-rules';
|
|
3
|
+
import * as bson from 'bson';
|
|
4
|
+
import * as mongo from 'mongodb';
|
|
5
|
+
|
|
6
|
+
import * as util from '@/util/util-index.js';
|
|
7
|
+
import { SourceTable } from '../SourceTable.js';
|
|
8
|
+
import { currentBucketKey } from './MongoBucketBatch.js';
|
|
9
|
+
import { MongoIdSequence } from './MongoIdSequence.js';
|
|
10
|
+
import { PowerSyncMongo } from './db.js';
|
|
11
|
+
import {
|
|
12
|
+
BucketDataDocument,
|
|
13
|
+
BucketParameterDocument,
|
|
14
|
+
CurrentBucket,
|
|
15
|
+
CurrentDataDocument,
|
|
16
|
+
SourceKey
|
|
17
|
+
} from './models.js';
|
|
18
|
+
import { serializeLookup } from './util.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Maximum size of operations we write in a single transaction.
|
|
22
|
+
*
|
|
23
|
+
* It's tricky to find the exact limit, but from experience, over 100MB
|
|
24
|
+
* can cause an error:
|
|
25
|
+
* > transaction is too large and will not fit in the storage engine cache
|
|
26
|
+
*
|
|
27
|
+
* Additionally, unbounded size here can balloon our memory usage in some edge
|
|
28
|
+
* cases.
|
|
29
|
+
*
|
|
30
|
+
* When we reach this threshold, we commit the transaction and start a new one.
|
|
31
|
+
*/
|
|
32
|
+
const MAX_TRANSACTION_BATCH_SIZE = 30_000_000;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Keeps track of bulkwrite operations within a transaction.
|
|
36
|
+
*
|
|
37
|
+
* There may be multiple of these batches per transaction, but it may not span
|
|
38
|
+
* multiple transactions.
|
|
39
|
+
*/
|
|
40
|
+
export class PersistedBatch {
|
|
41
|
+
bucketData: mongo.AnyBulkWriteOperation<BucketDataDocument>[] = [];
|
|
42
|
+
bucketParameters: mongo.AnyBulkWriteOperation<BucketParameterDocument>[] = [];
|
|
43
|
+
currentData: mongo.AnyBulkWriteOperation<CurrentDataDocument>[] = [];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Very rough estimate of transaction size.
|
|
47
|
+
*/
|
|
48
|
+
currentSize = 0;
|
|
49
|
+
|
|
50
|
+
constructor(private group_id: number, writtenSize: number) {
|
|
51
|
+
this.currentSize = writtenSize;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
saveBucketData(options: {
|
|
55
|
+
op_seq: MongoIdSequence;
|
|
56
|
+
sourceKey: bson.UUID;
|
|
57
|
+
table: SourceTable;
|
|
58
|
+
evaluated: EvaluatedRow[];
|
|
59
|
+
before_buckets: CurrentBucket[];
|
|
60
|
+
}) {
|
|
61
|
+
const remaining_buckets = new Map<string, CurrentBucket>();
|
|
62
|
+
for (let b of options.before_buckets) {
|
|
63
|
+
const key = currentBucketKey(b);
|
|
64
|
+
remaining_buckets.set(key, b);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const dchecksum = util.hashDelete(`${options.table.id}/${options.sourceKey}`);
|
|
68
|
+
|
|
69
|
+
for (let k of options.evaluated) {
|
|
70
|
+
const key = currentBucketKey(k);
|
|
71
|
+
remaining_buckets.delete(key);
|
|
72
|
+
|
|
73
|
+
// INSERT
|
|
74
|
+
const recordData = JSONBig.stringify(k.data);
|
|
75
|
+
const checksum = util.hashData(k.table, k.id, recordData);
|
|
76
|
+
this.currentSize += recordData.length + 200;
|
|
77
|
+
|
|
78
|
+
this.bucketData.push({
|
|
79
|
+
insertOne: {
|
|
80
|
+
document: {
|
|
81
|
+
_id: {
|
|
82
|
+
g: this.group_id,
|
|
83
|
+
b: k.bucket,
|
|
84
|
+
o: options.op_seq.next()
|
|
85
|
+
},
|
|
86
|
+
op: 'PUT',
|
|
87
|
+
source_table: options.table.id,
|
|
88
|
+
source_key: options.sourceKey,
|
|
89
|
+
table: k.table,
|
|
90
|
+
row_id: k.id,
|
|
91
|
+
checksum: checksum,
|
|
92
|
+
data: recordData
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (let bd of remaining_buckets.values()) {
|
|
99
|
+
// REMOVE
|
|
100
|
+
this.bucketData.push({
|
|
101
|
+
insertOne: {
|
|
102
|
+
document: {
|
|
103
|
+
_id: {
|
|
104
|
+
g: this.group_id,
|
|
105
|
+
b: bd.bucket,
|
|
106
|
+
o: options.op_seq.next()
|
|
107
|
+
},
|
|
108
|
+
op: 'REMOVE',
|
|
109
|
+
source_table: options.table.id,
|
|
110
|
+
source_key: options.sourceKey,
|
|
111
|
+
table: bd.table,
|
|
112
|
+
row_id: bd.id,
|
|
113
|
+
checksum: dchecksum,
|
|
114
|
+
data: null
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
this.currentSize += 200;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
saveParameterData(data: {
|
|
123
|
+
op_seq: MongoIdSequence;
|
|
124
|
+
sourceKey: bson.UUID;
|
|
125
|
+
sourceTable: SourceTable;
|
|
126
|
+
evaluated: EvaluatedParameters[];
|
|
127
|
+
existing_lookups: bson.Binary[];
|
|
128
|
+
}) {
|
|
129
|
+
// This is similar to saving bucket data.
|
|
130
|
+
// A key difference is that we don't need to keep the history intact.
|
|
131
|
+
// We do need to keep track of recent history though - enough that we can get consistent data for any specific checkpoint.
|
|
132
|
+
// Instead of storing per bucket id, we store per "lookup".
|
|
133
|
+
// A key difference is that we don't need to store or keep track of anything per-bucket - the entire record is
|
|
134
|
+
// either persisted or removed.
|
|
135
|
+
// We also don't need to keep history intact.
|
|
136
|
+
const { sourceTable, sourceKey, evaluated } = data;
|
|
137
|
+
|
|
138
|
+
const remaining_lookups = new Map<string, bson.Binary>();
|
|
139
|
+
for (let l of data.existing_lookups) {
|
|
140
|
+
remaining_lookups.set(l.toString('base64'), l);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 1. Insert new entries
|
|
144
|
+
for (let result of evaluated) {
|
|
145
|
+
const binLookup = serializeLookup(result.lookup);
|
|
146
|
+
const hex = binLookup.toString('base64');
|
|
147
|
+
remaining_lookups.delete(hex);
|
|
148
|
+
const op_id = data.op_seq.next();
|
|
149
|
+
this.bucketParameters.push({
|
|
150
|
+
insertOne: {
|
|
151
|
+
document: {
|
|
152
|
+
_id: op_id,
|
|
153
|
+
key: {
|
|
154
|
+
g: this.group_id,
|
|
155
|
+
t: sourceTable.id,
|
|
156
|
+
k: sourceKey
|
|
157
|
+
},
|
|
158
|
+
lookup: binLookup,
|
|
159
|
+
bucket_parameters: result.bucket_parameters
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
this.currentSize += 200;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// 2. "REMOVE" entries for any lookup not touched.
|
|
168
|
+
for (let lookup of remaining_lookups.values()) {
|
|
169
|
+
const op_id = data.op_seq.next();
|
|
170
|
+
this.bucketParameters.push({
|
|
171
|
+
insertOne: {
|
|
172
|
+
document: {
|
|
173
|
+
_id: op_id,
|
|
174
|
+
key: {
|
|
175
|
+
g: this.group_id,
|
|
176
|
+
t: sourceTable.id,
|
|
177
|
+
k: sourceKey
|
|
178
|
+
},
|
|
179
|
+
lookup: lookup,
|
|
180
|
+
bucket_parameters: []
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
this.currentSize += 200;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
deleteCurrentData(id: SourceKey) {
|
|
190
|
+
const op: mongo.AnyBulkWriteOperation<CurrentDataDocument> = {
|
|
191
|
+
deleteOne: {
|
|
192
|
+
filter: { _id: id }
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
this.currentData.push(op);
|
|
196
|
+
this.currentSize += 50;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
upsertCurrentData(id: SourceKey, values: Partial<CurrentDataDocument>) {
|
|
200
|
+
const op: mongo.AnyBulkWriteOperation<CurrentDataDocument> = {
|
|
201
|
+
updateOne: {
|
|
202
|
+
filter: { _id: id },
|
|
203
|
+
update: {
|
|
204
|
+
$set: values
|
|
205
|
+
},
|
|
206
|
+
upsert: true
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
this.currentData.push(op);
|
|
210
|
+
this.currentSize += (values.data?.length() ?? 0) + 100;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
shouldFlushTransaction() {
|
|
214
|
+
return this.currentSize >= MAX_TRANSACTION_BATCH_SIZE;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async flush(db: PowerSyncMongo, session: mongo.ClientSession) {
|
|
218
|
+
if (this.bucketData.length > 0) {
|
|
219
|
+
await db.bucket_data.bulkWrite(this.bucketData, {
|
|
220
|
+
session,
|
|
221
|
+
// inserts only - order doesn't matter
|
|
222
|
+
ordered: false
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
if (this.bucketParameters.length > 0) {
|
|
226
|
+
await db.bucket_parameters.bulkWrite(this.bucketParameters, {
|
|
227
|
+
session,
|
|
228
|
+
// inserts only - order doesn't matter
|
|
229
|
+
ordered: false
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
if (this.currentData.length > 0) {
|
|
233
|
+
await db.current_data.bulkWrite(this.currentData, {
|
|
234
|
+
session,
|
|
235
|
+
// may update and delete data within the same batch - order matters
|
|
236
|
+
ordered: true
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
this.bucketData = [];
|
|
241
|
+
this.bucketParameters = [];
|
|
242
|
+
this.currentData = [];
|
|
243
|
+
this.currentSize = 0;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as mongo from 'mongodb';
|
|
2
|
+
|
|
3
|
+
import * as db from '@/db/db-index.js';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
BucketDataDocument,
|
|
7
|
+
BucketParameterDocument,
|
|
8
|
+
CurrentDataDocument,
|
|
9
|
+
IdSequenceDocument,
|
|
10
|
+
SourceTableDocument,
|
|
11
|
+
SyncRuleDocument,
|
|
12
|
+
WriteCheckpointDocument
|
|
13
|
+
} from './models.js';
|
|
14
|
+
import { BSON_DESERIALIZE_OPTIONS } from './util.js';
|
|
15
|
+
import { configFile } from '@powersync/service-types';
|
|
16
|
+
|
|
17
|
+
export interface PowerSyncMongoOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Optional - uses the database from the MongoClient connection URI if not specified.
|
|
20
|
+
*/
|
|
21
|
+
database?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createPowerSyncMongo(config: configFile.PowerSyncConfig['storage']) {
|
|
25
|
+
return new PowerSyncMongo(db.mongo.createMongoClient(config), { database: config.database });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class PowerSyncMongo {
|
|
29
|
+
readonly current_data: mongo.Collection<CurrentDataDocument>;
|
|
30
|
+
readonly bucket_data: mongo.Collection<BucketDataDocument>;
|
|
31
|
+
readonly bucket_parameters: mongo.Collection<BucketParameterDocument>;
|
|
32
|
+
readonly op_id_sequence: mongo.Collection<IdSequenceDocument>;
|
|
33
|
+
readonly sync_rules: mongo.Collection<SyncRuleDocument>;
|
|
34
|
+
readonly source_tables: mongo.Collection<SourceTableDocument>;
|
|
35
|
+
readonly write_checkpoints: mongo.Collection<WriteCheckpointDocument>;
|
|
36
|
+
|
|
37
|
+
readonly client: mongo.MongoClient;
|
|
38
|
+
readonly db: mongo.Db;
|
|
39
|
+
|
|
40
|
+
constructor(client: mongo.MongoClient, options?: PowerSyncMongoOptions) {
|
|
41
|
+
this.client = client;
|
|
42
|
+
|
|
43
|
+
const db = client.db(options?.database, {
|
|
44
|
+
// Note this issue with useBigInt64: https://jira.mongodb.org/browse/NODE-6165
|
|
45
|
+
// Unfortunately there is no workaround if we want to continue using bigint.
|
|
46
|
+
// We selectively disable this in individual queries where we don't need that.
|
|
47
|
+
...BSON_DESERIALIZE_OPTIONS
|
|
48
|
+
});
|
|
49
|
+
this.db = db;
|
|
50
|
+
|
|
51
|
+
this.current_data = db.collection<CurrentDataDocument>('current_data');
|
|
52
|
+
this.bucket_data = db.collection('bucket_data');
|
|
53
|
+
this.bucket_parameters = db.collection('bucket_parameters');
|
|
54
|
+
this.op_id_sequence = db.collection('op_id_sequence');
|
|
55
|
+
this.sync_rules = db.collection('sync_rules');
|
|
56
|
+
this.source_tables = db.collection('source_tables');
|
|
57
|
+
this.write_checkpoints = db.collection('write_checkpoints');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async clear() {
|
|
61
|
+
await this.current_data.deleteMany({});
|
|
62
|
+
await this.bucket_data.deleteMany({});
|
|
63
|
+
await this.bucket_parameters.deleteMany({});
|
|
64
|
+
await this.op_id_sequence.deleteMany({});
|
|
65
|
+
await this.sync_rules.deleteMany({});
|
|
66
|
+
await this.source_tables.deleteMany({});
|
|
67
|
+
await this.write_checkpoints.deleteMany({});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import * as bson from 'bson';
|
|
2
|
+
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
3
|
+
|
|
4
|
+
export interface SourceKey {
|
|
5
|
+
/** group_id */
|
|
6
|
+
g: number;
|
|
7
|
+
/** source table id */
|
|
8
|
+
t: bson.ObjectId;
|
|
9
|
+
/** source key */
|
|
10
|
+
k: bson.UUID;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface BucketDataKey {
|
|
14
|
+
/** group_id */
|
|
15
|
+
g: number;
|
|
16
|
+
/** bucket name */
|
|
17
|
+
b: string;
|
|
18
|
+
/** op_id */
|
|
19
|
+
o: bigint;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface CurrentDataDocument {
|
|
23
|
+
_id: SourceKey;
|
|
24
|
+
data: bson.Binary;
|
|
25
|
+
buckets: CurrentBucket[];
|
|
26
|
+
lookups: bson.Binary[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface CurrentBucket {
|
|
30
|
+
bucket: string;
|
|
31
|
+
table: string;
|
|
32
|
+
id: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface BucketParameterDocument {
|
|
36
|
+
_id: bigint;
|
|
37
|
+
key: SourceKey;
|
|
38
|
+
lookup: bson.Binary;
|
|
39
|
+
bucket_parameters: Record<string, SqliteJsonValue>[];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface BucketDataDocument {
|
|
43
|
+
_id: BucketDataKey;
|
|
44
|
+
op: OpType;
|
|
45
|
+
source_table: bson.ObjectId;
|
|
46
|
+
source_key: bson.UUID;
|
|
47
|
+
table: string;
|
|
48
|
+
row_id: string;
|
|
49
|
+
checksum: number;
|
|
50
|
+
data: string | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type OpType = 'PUT' | 'REMOVE' | 'MOVE' | 'CLEAR';
|
|
54
|
+
|
|
55
|
+
export interface SourceTableDocument {
|
|
56
|
+
_id: bson.ObjectId;
|
|
57
|
+
group_id: number;
|
|
58
|
+
connection_id: number;
|
|
59
|
+
relation_id: number | undefined;
|
|
60
|
+
schema_name: string;
|
|
61
|
+
table_name: string;
|
|
62
|
+
replica_id_columns: string[] | null;
|
|
63
|
+
replica_id_columns2: { name: string; type_oid: number }[] | undefined;
|
|
64
|
+
snapshot_done: boolean | undefined;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface IdSequenceDocument {
|
|
68
|
+
_id: string;
|
|
69
|
+
op_id: bigint;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export enum SyncRuleState {
|
|
73
|
+
/**
|
|
74
|
+
* New sync rules - needs to be processed (initial replication).
|
|
75
|
+
*
|
|
76
|
+
* While multiple sets of sync rules _can_ be in PROCESSING,
|
|
77
|
+
* it's generally pointless, so we only keep one in that state.
|
|
78
|
+
*/
|
|
79
|
+
PROCESSING = 'PROCESSING',
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Sync rule processing is done, and can be used for sync.
|
|
83
|
+
*
|
|
84
|
+
* Only one set of sync rules should be in ACTIVE state.
|
|
85
|
+
*/
|
|
86
|
+
ACTIVE = 'ACTIVE',
|
|
87
|
+
/**
|
|
88
|
+
* This state is used when the sync rules has been replaced,
|
|
89
|
+
* and replication is or should be stopped.
|
|
90
|
+
*/
|
|
91
|
+
STOP = 'STOP',
|
|
92
|
+
/**
|
|
93
|
+
* After sync rules have been stopped, the data needs to be
|
|
94
|
+
* deleted. Once deleted, the state is TERMINATED.
|
|
95
|
+
*/
|
|
96
|
+
TERMINATED = 'TERMINATED'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface SyncRuleDocument {
|
|
100
|
+
_id: number;
|
|
101
|
+
|
|
102
|
+
state: SyncRuleState;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* True if initial snapshot has been replicated.
|
|
106
|
+
*
|
|
107
|
+
* Can only be false if state == PROCESSING.
|
|
108
|
+
*/
|
|
109
|
+
snapshot_done: boolean;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The last consistent checkpoint.
|
|
113
|
+
*
|
|
114
|
+
* There may be higher OpIds used in the database if we're in the middle of replicating a large transaction.
|
|
115
|
+
*/
|
|
116
|
+
last_checkpoint: bigint | null;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The LSN associated with the last consistent checkpoint.
|
|
120
|
+
*/
|
|
121
|
+
last_checkpoint_lsn: string | null;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* If set, no new checkpoints may be created < this value.
|
|
125
|
+
*/
|
|
126
|
+
no_checkpoint_before: string | null;
|
|
127
|
+
|
|
128
|
+
slot_name: string | null;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Last time we persisted a checkpoint.
|
|
132
|
+
*
|
|
133
|
+
* This may be old if no data is incoming.
|
|
134
|
+
*/
|
|
135
|
+
last_checkpoint_ts: Date | null;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Last time we persisted a checkpoint or keepalive.
|
|
139
|
+
*
|
|
140
|
+
* This should stay fairly current while replicating.
|
|
141
|
+
*/
|
|
142
|
+
last_keepalive_ts: Date | null;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* If an error is stopping replication, it will be stored here.
|
|
146
|
+
*/
|
|
147
|
+
last_fatal_error: string | null;
|
|
148
|
+
|
|
149
|
+
content: string;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface WriteCheckpointDocument {
|
|
153
|
+
_id: bson.ObjectId;
|
|
154
|
+
user_id: string;
|
|
155
|
+
lsns: Record<string, string>;
|
|
156
|
+
client_id: bigint;
|
|
157
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { SqliteJsonValue } from '@powersync/service-sync-rules';
|
|
2
|
+
import * as bson from 'bson';
|
|
3
|
+
import * as mongo from 'mongodb';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Lookup serialization must be number-agnostic. I.e. normalize numbers, instead of preserving numbers.
|
|
8
|
+
* @param lookup
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export function serializeLookup(lookup: SqliteJsonValue[]) {
|
|
12
|
+
const normalized = lookup.map((value) => {
|
|
13
|
+
if (typeof value == 'number' && Number.isInteger(value)) {
|
|
14
|
+
return BigInt(value);
|
|
15
|
+
} else {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
return new bson.Binary(bson.serialize({ l: normalized }));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T> {
|
|
23
|
+
let filter = {
|
|
24
|
+
$gte: {
|
|
25
|
+
...prefix
|
|
26
|
+
} as any,
|
|
27
|
+
$lt: {
|
|
28
|
+
...prefix
|
|
29
|
+
} as any
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
for (let key of rest) {
|
|
33
|
+
filter.$gte[key] = new bson.MinKey();
|
|
34
|
+
filter.$lt[key] = new bson.MaxKey();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return filter;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function generateSlotName(prefix: string, sync_rules_id: number) {
|
|
41
|
+
const slot_suffix = crypto.randomBytes(2).toString('hex');
|
|
42
|
+
return `${prefix}${sync_rules_id}_${slot_suffix}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Read a single batch of data from a cursor, then close it.
|
|
47
|
+
*
|
|
48
|
+
* We do our best to avoid MongoDB fetching any more data than this single batch.
|
|
49
|
+
*
|
|
50
|
+
* This is similar to using `singleBatch: true` in find options.
|
|
51
|
+
* However, that makes `has_more` detection very difficult, since the cursor is always closed
|
|
52
|
+
* after the first batch. Instead, we do a workaround to only fetch a single batch below.
|
|
53
|
+
*
|
|
54
|
+
* For this to be effective, set batchSize = limit in the find command.
|
|
55
|
+
*/
|
|
56
|
+
export async function readSingleBatch<T>(cursor: mongo.FindCursor<T>): Promise<{ data: T[]; hasMore: boolean }> {
|
|
57
|
+
try {
|
|
58
|
+
let data: T[];
|
|
59
|
+
let hasMore = true;
|
|
60
|
+
// Let MongoDB load the first batch of data
|
|
61
|
+
const hasAny = await cursor.hasNext();
|
|
62
|
+
// Now it's in memory, and we can read it
|
|
63
|
+
data = cursor.readBufferedDocuments();
|
|
64
|
+
if (!hasAny || cursor.id?.isZero()) {
|
|
65
|
+
// A zero id means the cursor is exhaused.
|
|
66
|
+
// No results (hasAny == false) means even this batch doesn't have data.
|
|
67
|
+
// This should similar results as `await cursor.hasNext()`, but without
|
|
68
|
+
// actually fetching the next batch.
|
|
69
|
+
//
|
|
70
|
+
// Note that it is safe (but slightly inefficient) to return `hasMore: true`
|
|
71
|
+
// without there being more data, as long as the next batch
|
|
72
|
+
// will return `hasMore: false`.
|
|
73
|
+
hasMore = false;
|
|
74
|
+
}
|
|
75
|
+
return { data, hasMore };
|
|
76
|
+
} finally {
|
|
77
|
+
// Match the from the cursor iterator logic here:
|
|
78
|
+
// https://github.com/mongodb/node-mongodb-native/blob/e02534e7d1c627bf50b85ca39f5995dbf165ad44/src/cursor/abstract_cursor.ts#L327-L331
|
|
79
|
+
if (!cursor.closed) {
|
|
80
|
+
await cursor.close();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export const BSON_DESERIALIZE_OPTIONS: bson.DeserializeOptions = {
|
|
86
|
+
// use bigint instead of long
|
|
87
|
+
useBigInt64: true
|
|
88
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export * from './SourceTable.js';
|
|
2
|
+
export * from './MongoBucketStorage.js';
|
|
3
|
+
export * from './BucketStorage.js';
|
|
4
|
+
|
|
5
|
+
export * from './mongo/db.js';
|
|
6
|
+
export * from './mongo/models.js';
|
|
7
|
+
export * from './mongo/MongoBucketBatch.js';
|
|
8
|
+
export * from './mongo/MongoIdSequence.js';
|
|
9
|
+
export * from './mongo/MongoPersistedSyncRules.js';
|
|
10
|
+
export * from './mongo/MongoPersistedSyncRulesContent.js';
|
|
11
|
+
export * from './mongo/MongoSyncBucketStorage.js';
|
|
12
|
+
export * from './mongo/MongoSyncRulesLock.js';
|
|
13
|
+
export * from './mongo/OperationBatch.js';
|
|
14
|
+
export * from './mongo/PersistedBatch.js';
|
|
15
|
+
export * from './mongo/util.js';
|