@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,257 @@
|
|
|
1
|
+
import * as bson from 'bson';
|
|
2
|
+
import * as mongo from 'mongodb';
|
|
3
|
+
import { afterEach, describe, expect, test } from 'vitest';
|
|
4
|
+
import { WalStream, WalStreamOptions } from '../../src/replication/WalStream.js';
|
|
5
|
+
import { getClientCheckpoint } from '../../src/util/utils.js';
|
|
6
|
+
import { env } from './env.js';
|
|
7
|
+
import { MONGO_STORAGE_FACTORY, StorageFactory, TEST_CONNECTION_OPTIONS, connectPgPool } from './util.js';
|
|
8
|
+
|
|
9
|
+
import * as pgwire from '@powersync/service-jpgwire';
|
|
10
|
+
import { SqliteRow } from '@powersync/service-sync-rules';
|
|
11
|
+
import { MongoBucketStorage } from '../../src/storage/MongoBucketStorage.js';
|
|
12
|
+
import { PgManager } from '../../src/util/PgManager.js';
|
|
13
|
+
|
|
14
|
+
describe('slow tests - mongodb', function () {
|
|
15
|
+
// These are slow, inconsistent tests.
|
|
16
|
+
// Not run on every test run, but we do run on CI, or when manually debugging issues.
|
|
17
|
+
if (env.CI || env.SLOW_TESTS) {
|
|
18
|
+
defineSlowTests(MONGO_STORAGE_FACTORY);
|
|
19
|
+
} else {
|
|
20
|
+
// Need something in this file.
|
|
21
|
+
test('no-op', () => {});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function defineSlowTests(factory: StorageFactory) {
|
|
26
|
+
let walStream: WalStream | undefined;
|
|
27
|
+
let connections: PgManager | undefined;
|
|
28
|
+
let abortController: AbortController | undefined;
|
|
29
|
+
let streamPromise: Promise<void> | undefined;
|
|
30
|
+
|
|
31
|
+
afterEach(async () => {
|
|
32
|
+
// This cleans up, similar to WalStreamTestContext.dispose().
|
|
33
|
+
// These tests are a little more complex than what is supported by WalStreamTestContext.
|
|
34
|
+
abortController?.abort();
|
|
35
|
+
await streamPromise;
|
|
36
|
+
streamPromise = undefined;
|
|
37
|
+
connections?.destroy();
|
|
38
|
+
|
|
39
|
+
connections = undefined;
|
|
40
|
+
walStream = undefined;
|
|
41
|
+
abortController = undefined;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const TEST_DURATION_MS = 15_000;
|
|
45
|
+
const TIMEOUT_MARGIN_MS = env.CI ? 30_000 : 15_000;
|
|
46
|
+
|
|
47
|
+
// Test repeatedly replicating inserts and deletes, then check that we get
|
|
48
|
+
// consistent data out at the end.
|
|
49
|
+
//
|
|
50
|
+
// Past issues that this could reproduce intermittently:
|
|
51
|
+
// * Skipping LSNs after a keepalive message
|
|
52
|
+
// * Skipping LSNs when source transactions overlap
|
|
53
|
+
test(
|
|
54
|
+
'repeated replication',
|
|
55
|
+
async () => {
|
|
56
|
+
const connections = new PgManager(TEST_CONNECTION_OPTIONS, {});
|
|
57
|
+
const replicationConnection = await connections.replicationConnection();
|
|
58
|
+
const pool = connections.pool;
|
|
59
|
+
const f = (await factory()) as MongoBucketStorage;
|
|
60
|
+
|
|
61
|
+
const syncRuleContent = `
|
|
62
|
+
bucket_definitions:
|
|
63
|
+
global:
|
|
64
|
+
data:
|
|
65
|
+
- SELECT id, description FROM "test_data"
|
|
66
|
+
`;
|
|
67
|
+
const syncRules = await f.updateSyncRules({ content: syncRuleContent });
|
|
68
|
+
const storage = f.getInstance(syncRules.parsed());
|
|
69
|
+
abortController = new AbortController();
|
|
70
|
+
const options: WalStreamOptions = {
|
|
71
|
+
abort_signal: abortController.signal,
|
|
72
|
+
connections,
|
|
73
|
+
storage: storage,
|
|
74
|
+
factory: f
|
|
75
|
+
};
|
|
76
|
+
walStream = new WalStream(options);
|
|
77
|
+
|
|
78
|
+
await pool.query(`DROP TABLE IF EXISTS test_data`);
|
|
79
|
+
await pool.query(`CREATE TABLE test_data(id uuid primary key default uuid_generate_v4(), description text)`);
|
|
80
|
+
|
|
81
|
+
await walStream.initReplication(replicationConnection);
|
|
82
|
+
await storage.autoActivate();
|
|
83
|
+
let abort = false;
|
|
84
|
+
streamPromise = walStream.streamChanges(replicationConnection).finally(() => {
|
|
85
|
+
abort = true;
|
|
86
|
+
});
|
|
87
|
+
const start = Date.now();
|
|
88
|
+
|
|
89
|
+
while (!abort && Date.now() - start < TEST_DURATION_MS) {
|
|
90
|
+
const bg = async () => {
|
|
91
|
+
for (let j = 0; j < 5 && !abort; j++) {
|
|
92
|
+
const n = Math.floor(Math.random() * 50);
|
|
93
|
+
let statements: pgwire.Statement[] = [];
|
|
94
|
+
for (let i = 0; i < n; i++) {
|
|
95
|
+
const description = `test${i}`;
|
|
96
|
+
statements.push({
|
|
97
|
+
statement: `INSERT INTO test_data(description) VALUES($1) returning id as test_id`,
|
|
98
|
+
params: [{ type: 'varchar', value: description }]
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
const results = await pool.query(...statements);
|
|
102
|
+
const ids = results.results.map((sub) => {
|
|
103
|
+
return sub.rows[0][0] as string;
|
|
104
|
+
});
|
|
105
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 30));
|
|
106
|
+
|
|
107
|
+
const deleteStatements: pgwire.Statement[] = ids.map((id) => {
|
|
108
|
+
return {
|
|
109
|
+
statement: `DELETE FROM test_data WHERE id = $1`,
|
|
110
|
+
params: [{ type: 'uuid', value: id }]
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
await pool.query(...deleteStatements);
|
|
114
|
+
|
|
115
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 10));
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
// Call the above loop multiple times concurrently
|
|
120
|
+
let promises = [1, 2, 3].map((i) => bg());
|
|
121
|
+
await Promise.all(promises);
|
|
122
|
+
|
|
123
|
+
// Wait for replication to finish
|
|
124
|
+
let checkpoint = await getClientCheckpoint(pool, storage.factory, { timeout: TIMEOUT_MARGIN_MS });
|
|
125
|
+
|
|
126
|
+
// Check that all inserts have been deleted again
|
|
127
|
+
const docs = await f.db.current_data.find().toArray();
|
|
128
|
+
const transformed = docs.map((doc) => {
|
|
129
|
+
return bson.deserialize((doc.data as mongo.Binary).buffer) as SqliteRow;
|
|
130
|
+
});
|
|
131
|
+
expect(transformed).toEqual([]);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
abortController.abort();
|
|
135
|
+
await streamPromise;
|
|
136
|
+
},
|
|
137
|
+
{ timeout: TEST_DURATION_MS + TIMEOUT_MARGIN_MS }
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
// Test repeatedly performing initial replication.
|
|
141
|
+
//
|
|
142
|
+
// If the first LSN does not correctly match with the first replication transaction,
|
|
143
|
+
// we may miss some updates.
|
|
144
|
+
test(
|
|
145
|
+
'repeated initial replication',
|
|
146
|
+
async () => {
|
|
147
|
+
const pool = await connectPgPool();
|
|
148
|
+
const f = await factory();
|
|
149
|
+
|
|
150
|
+
const syncRuleContent = `
|
|
151
|
+
bucket_definitions:
|
|
152
|
+
global:
|
|
153
|
+
data:
|
|
154
|
+
- SELECT id, description FROM "test_data"
|
|
155
|
+
`;
|
|
156
|
+
const syncRules = await f.updateSyncRules({ content: syncRuleContent });
|
|
157
|
+
const storage = f.getInstance(syncRules.parsed());
|
|
158
|
+
|
|
159
|
+
// 1. Setup some base data that will be replicated in initial replication
|
|
160
|
+
await pool.query(`DROP TABLE IF EXISTS test_data`);
|
|
161
|
+
await pool.query(`CREATE TABLE test_data(id uuid primary key default uuid_generate_v4(), description text)`);
|
|
162
|
+
|
|
163
|
+
let statements: pgwire.Statement[] = [];
|
|
164
|
+
|
|
165
|
+
const n = Math.floor(Math.random() * 200);
|
|
166
|
+
for (let i = 0; i < n; i++) {
|
|
167
|
+
statements.push({
|
|
168
|
+
statement: `INSERT INTO test_data(description) VALUES('test_init')`
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
await pool.query(...statements);
|
|
172
|
+
|
|
173
|
+
const start = Date.now();
|
|
174
|
+
let i = 0;
|
|
175
|
+
|
|
176
|
+
while (Date.now() - start < TEST_DURATION_MS) {
|
|
177
|
+
// 2. Each iteration starts with a clean slate
|
|
178
|
+
await pool.query(`SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE active = FALSE`);
|
|
179
|
+
i += 1;
|
|
180
|
+
|
|
181
|
+
const connections = new PgManager(TEST_CONNECTION_OPTIONS, {});
|
|
182
|
+
const replicationConnection = await connections.replicationConnection();
|
|
183
|
+
|
|
184
|
+
abortController = new AbortController();
|
|
185
|
+
const options: WalStreamOptions = {
|
|
186
|
+
abort_signal: abortController.signal,
|
|
187
|
+
connections,
|
|
188
|
+
storage: storage,
|
|
189
|
+
factory: f
|
|
190
|
+
};
|
|
191
|
+
walStream = new WalStream(options);
|
|
192
|
+
|
|
193
|
+
await storage.clear();
|
|
194
|
+
|
|
195
|
+
// 3. Start initial replication, then streaming, but don't wait for any of this
|
|
196
|
+
let initialReplicationDone = false;
|
|
197
|
+
streamPromise = (async () => {
|
|
198
|
+
await walStream.initReplication(replicationConnection);
|
|
199
|
+
await storage.autoActivate();
|
|
200
|
+
initialReplicationDone = true;
|
|
201
|
+
await walStream.streamChanges(replicationConnection);
|
|
202
|
+
})()
|
|
203
|
+
.catch((e) => {
|
|
204
|
+
initialReplicationDone = true;
|
|
205
|
+
throw e;
|
|
206
|
+
})
|
|
207
|
+
.then((v) => {
|
|
208
|
+
return v;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// 4. While initial replication is still running, write more changes
|
|
212
|
+
while (!initialReplicationDone) {
|
|
213
|
+
let statements: pgwire.Statement[] = [];
|
|
214
|
+
const n = Math.floor(Math.random() * 10) + 1;
|
|
215
|
+
for (let i = 0; i < n; i++) {
|
|
216
|
+
const description = `test${i}`;
|
|
217
|
+
statements.push({
|
|
218
|
+
statement: `INSERT INTO test_data(description) VALUES('test1') returning id as test_id`,
|
|
219
|
+
params: [{ type: 'varchar', value: description }]
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
const results = await pool.query(...statements);
|
|
223
|
+
const ids = results.results.map((sub) => {
|
|
224
|
+
return sub.rows[0][0] as string;
|
|
225
|
+
});
|
|
226
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 30));
|
|
227
|
+
const deleteStatements: pgwire.Statement[] = ids.map((id) => {
|
|
228
|
+
return {
|
|
229
|
+
statement: `DELETE FROM test_data WHERE id = $1`,
|
|
230
|
+
params: [{ type: 'uuid', value: id }]
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
await pool.query(...deleteStatements);
|
|
234
|
+
await new Promise((resolve) => setTimeout(resolve, Math.random() * 10));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// 5. Once initial replication is done, wait for the streaming changes to complete syncing.
|
|
238
|
+
// getClientCheckpoint() effectively waits for the above replication to complete
|
|
239
|
+
// Race with streamingPromise to catch replication errors here.
|
|
240
|
+
let checkpoint = await Promise.race([
|
|
241
|
+
getClientCheckpoint(pool, storage.factory, { timeout: TIMEOUT_MARGIN_MS }),
|
|
242
|
+
streamPromise
|
|
243
|
+
]);
|
|
244
|
+
if (typeof checkpoint == undefined) {
|
|
245
|
+
// This indicates an issue with the test setup - streamingPromise completed instead
|
|
246
|
+
// of getClientCheckpoint()
|
|
247
|
+
throw new Error('Test failure - streamingPromise completed');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
abortController.abort();
|
|
251
|
+
await streamPromise;
|
|
252
|
+
await connections.end();
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
{ timeout: TEST_DURATION_MS + TIMEOUT_MARGIN_MS }
|
|
256
|
+
);
|
|
257
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import { SQL_FUNCTIONS_CALL, cast, jsonExtract } from '@powersync/service-sync-rules';
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
|
|
4
|
+
const fn = SQL_FUNCTIONS_CALL;
|
|
5
|
+
describe('SQL functions', () => {
|
|
6
|
+
test('json extract', () => {
|
|
7
|
+
expect(fn.json_extract(JSON.stringify({ foo: 'bar' }), '$.foo')).toEqual('bar');
|
|
8
|
+
expect(fn.json_extract(JSON.stringify({ foo: 42 }), '$.foo')).toEqual(42n);
|
|
9
|
+
expect(fn.json_extract(JSON.stringify({ foo: true }), '$.foo')).toEqual(1n);
|
|
10
|
+
expect(fn.json_extract(`{"foo": 42.0}`, '$.foo')).toEqual(42.0);
|
|
11
|
+
expect(fn.json_extract(JSON.stringify({ foo: 42 }), '$.bar.baz')).toEqual(null);
|
|
12
|
+
expect(fn.json_extract(JSON.stringify({ foo: 42 }), '$')).toEqual('{"foo":42}');
|
|
13
|
+
expect(fn.json_extract(`{"foo": 42.0}`, '$')).toEqual('{"foo":42.0}');
|
|
14
|
+
expect(fn.json_extract(`{"foo": true}`, '$')).toEqual('{"foo":true}');
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('->>', () => {
|
|
18
|
+
expect(jsonExtract(JSON.stringify({ foo: 'bar' }), '$.foo', '->>')).toEqual('bar');
|
|
19
|
+
expect(jsonExtract(JSON.stringify({ foo: 42 }), '$.foo', '->>')).toEqual(42n);
|
|
20
|
+
expect(jsonExtract(`{"foo": 42.0}`, '$.foo', '->>')).toEqual(42.0);
|
|
21
|
+
expect(jsonExtract(JSON.stringify({ foo: 'bar' }), 'foo', '->>')).toEqual('bar');
|
|
22
|
+
expect(jsonExtract(JSON.stringify({ foo: 42 }), 'foo', '->>')).toEqual(42n);
|
|
23
|
+
expect(jsonExtract(`{"foo": 42.0}`, 'foo', '->>')).toEqual(42.0);
|
|
24
|
+
expect(jsonExtract(`{"foo": 42.0}`, '$', '->>')).toEqual(`{"foo":42.0}`);
|
|
25
|
+
expect(jsonExtract(`{"foo": true}`, '$.foo', '->>')).toEqual(1n);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('->', () => {
|
|
29
|
+
expect(jsonExtract(JSON.stringify({ foo: 'bar' }), '$.foo', '->')).toEqual('"bar"');
|
|
30
|
+
expect(jsonExtract(JSON.stringify({ foo: 42 }), '$.foo', '->')).toEqual('42');
|
|
31
|
+
expect(jsonExtract(`{"foo": 42.0}`, '$.foo', '->')).toEqual('42.0');
|
|
32
|
+
expect(jsonExtract(JSON.stringify({ foo: 'bar' }), 'foo', '->')).toEqual('"bar"');
|
|
33
|
+
expect(jsonExtract(JSON.stringify({ foo: 42 }), 'foo', '->')).toEqual('42');
|
|
34
|
+
expect(jsonExtract(`{"foo": 42.0}`, 'foo', '->')).toEqual('42.0');
|
|
35
|
+
expect(jsonExtract(`{"foo": 42.0}`, '$', '->')).toEqual(`{"foo":42.0}`);
|
|
36
|
+
expect(jsonExtract(`{"foo": true}`, '$.foo', '->')).toEqual('true');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('json_array_length', () => {
|
|
40
|
+
expect(fn.json_array_length(`[1,2,3,4]`)).toEqual(4n);
|
|
41
|
+
expect(fn.json_array_length(`[1,2,3,4]`, '$')).toEqual(4n);
|
|
42
|
+
expect(fn.json_array_length(`[1,2,3,4]`, '$.a')).toEqual(null);
|
|
43
|
+
expect(fn.json_array_length(`{"a":[1,2,3,4]}`, '$.a')).toEqual(4n);
|
|
44
|
+
expect(fn.json_array_length(`{"a":[1,2,3,4]}`)).toEqual(0n);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('typeof', () => {
|
|
48
|
+
expect(fn.typeof(null)).toEqual('null');
|
|
49
|
+
expect(fn.typeof('test')).toEqual('text');
|
|
50
|
+
expect(fn.typeof('')).toEqual('text');
|
|
51
|
+
expect(fn.typeof(1n)).toEqual('integer');
|
|
52
|
+
expect(fn.typeof(1)).toEqual('real');
|
|
53
|
+
expect(fn.typeof(1.5)).toEqual('real');
|
|
54
|
+
expect(fn.typeof(Uint8Array.of(1, 2, 3))).toEqual('blob');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('length', () => {
|
|
58
|
+
expect(fn.length(null)).toEqual(null);
|
|
59
|
+
expect(fn.length('abc')).toEqual(3n);
|
|
60
|
+
expect(fn.length(123n)).toEqual(3n);
|
|
61
|
+
expect(fn.length(123.4)).toEqual(5n);
|
|
62
|
+
expect(fn.length(Uint8Array.of(1, 2, 3))).toEqual(3n);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('hex', () => {
|
|
66
|
+
expect(fn.hex(null)).toEqual('');
|
|
67
|
+
expect(fn.hex('abC')).toEqual('616243');
|
|
68
|
+
expect(fn.hex(Uint8Array.of(0x61, 0x62, 0x43))).toEqual('616243');
|
|
69
|
+
expect(fn.hex(123n)).toEqual('313233');
|
|
70
|
+
expect(fn.hex(123.4)).toEqual('3132332E34');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('base64', () => {
|
|
74
|
+
expect(fn.base64(null)).toEqual('');
|
|
75
|
+
expect(fn.base64('abc')).toEqual('YWJj');
|
|
76
|
+
expect(fn.base64(Uint8Array.of(0x61, 0x62, 0x63))).toEqual('YWJj');
|
|
77
|
+
expect(fn.base64(123n)).toEqual('MTIz');
|
|
78
|
+
expect(fn.base64(123.4)).toEqual('MTIzLjQ=');
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('ifnull', () => {
|
|
82
|
+
expect(fn.ifnull(null, null)).toEqual(null);
|
|
83
|
+
expect(fn.ifnull('test', null)).toEqual('test');
|
|
84
|
+
expect(fn.ifnull(null, 'test')).toEqual('test');
|
|
85
|
+
expect(fn.ifnull('test1', 'test2')).toEqual('test1');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('upper', () => {
|
|
89
|
+
expect(fn.upper(null)).toEqual(null);
|
|
90
|
+
expect(fn.upper('abc')).toEqual('ABC');
|
|
91
|
+
expect(fn.upper(123n)).toEqual('123');
|
|
92
|
+
expect(fn.upper(3e60)).toEqual('3E+60');
|
|
93
|
+
expect(fn.upper(Uint8Array.of(0x61, 0x62, 0x43))).toEqual('ABC');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('lower', () => {
|
|
97
|
+
expect(fn.lower(null)).toEqual(null);
|
|
98
|
+
expect(fn.lower('ABC')).toEqual('abc');
|
|
99
|
+
expect(fn.lower(123n)).toEqual('123');
|
|
100
|
+
expect(fn.lower(3e60)).toEqual('3e+60');
|
|
101
|
+
expect(fn.lower(Uint8Array.of(0x61, 0x62, 0x43))).toEqual('abc');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('cast', () => {
|
|
105
|
+
expect(cast(null, 'text')).toEqual(null);
|
|
106
|
+
expect(cast(null, 'integer')).toEqual(null);
|
|
107
|
+
expect(cast(null, 'numeric')).toEqual(null);
|
|
108
|
+
expect(cast(null, 'real')).toEqual(null);
|
|
109
|
+
expect(cast(null, 'blob')).toEqual(null);
|
|
110
|
+
|
|
111
|
+
expect(cast(1n, 'text')).toEqual('1');
|
|
112
|
+
expect(cast(1n, 'integer')).toEqual(1n);
|
|
113
|
+
expect(cast(1n, 'numeric')).toEqual(1n);
|
|
114
|
+
expect(cast(1n, 'real')).toEqual(1.0);
|
|
115
|
+
// We differ from SQLite here
|
|
116
|
+
expect(cast(1n, 'blob')).toEqual(Uint8Array.of(49));
|
|
117
|
+
|
|
118
|
+
expect(cast(1.2, 'text')).toEqual('1.2');
|
|
119
|
+
expect(cast(1.2, 'integer')).toEqual(1n);
|
|
120
|
+
expect(cast(1.2, 'numeric')).toEqual(1.2);
|
|
121
|
+
expect(cast(1.2, 'real')).toEqual(1.2);
|
|
122
|
+
// We differ from SQLite here
|
|
123
|
+
expect(cast(1.2, 'blob')).toEqual(Uint8Array.of(49, 46, 50));
|
|
124
|
+
|
|
125
|
+
expect(cast('abc', 'text')).toEqual('abc');
|
|
126
|
+
expect(cast('abc', 'integer')).toEqual(0n);
|
|
127
|
+
expect(cast('abc', 'numeric')).toEqual(0n);
|
|
128
|
+
expect(cast('abc', 'real')).toEqual(0.0);
|
|
129
|
+
|
|
130
|
+
expect(cast('1.2abc', 'integer')).toEqual(1n);
|
|
131
|
+
expect(cast('1.2abc', 'numeric')).toEqual(1.2);
|
|
132
|
+
expect(cast('1.2abc', 'real')).toEqual(1.2);
|
|
133
|
+
expect(cast('1.2e60abc', 'numeric')).toEqual(1.2e60);
|
|
134
|
+
expect(cast(' 1e60abc', 'numeric')).toEqual(1e60);
|
|
135
|
+
|
|
136
|
+
// We differ from SQLite here
|
|
137
|
+
expect(cast('abc', 'blob')).toEqual(Uint8Array.of(0x61, 0x62, 0x63));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('unixepoch', () => {
|
|
141
|
+
// Differences from SQLite
|
|
142
|
+
// These versions are not supported
|
|
143
|
+
expect(fn.unixepoch()).toEqual(null);
|
|
144
|
+
expect(fn.unixepoch('now')).toEqual(null);
|
|
145
|
+
expect(fn.unixepoch('2023-06-28 14:12', 'auto')).toEqual(null);
|
|
146
|
+
|
|
147
|
+
// Same as SQLite
|
|
148
|
+
expect(fn.unixepoch('2023-06-28 14:12')).toEqual(1687961520n);
|
|
149
|
+
expect(fn.unixepoch('2023-06-28 14:12:00')).toEqual(1687961520n);
|
|
150
|
+
expect(fn.unixepoch('2023-06-28 14:12:00+00:00')).toEqual(1687961520n);
|
|
151
|
+
expect(fn.unixepoch('2023-06-28 14:12:00Z')).toEqual(1687961520n);
|
|
152
|
+
expect(fn.unixepoch('2023-06-28 14:12:00.999')).toEqual(1687961520n);
|
|
153
|
+
expect(fn.unixepoch('2023-06-28 14:12:00.999Z')).toEqual(1687961520n);
|
|
154
|
+
expect(fn.unixepoch('2023-06-28T14:12:00.999Z')).toEqual(1687961520n);
|
|
155
|
+
expect(fn.unixepoch('2023-06-28T14:12:00+00:00')).toEqual(1687961520n);
|
|
156
|
+
expect(fn.unixepoch('2023-06-28T14:12+00:00')).toEqual(1687961520n);
|
|
157
|
+
expect(fn.unixepoch('2023-06-28T16:12+02:00')).toEqual(1687961520n);
|
|
158
|
+
expect(fn.unixepoch('2023-06-28T16:12+02')).toEqual(null);
|
|
159
|
+
expect(fn.unixepoch('2023-06-28')).toEqual(1687910400n);
|
|
160
|
+
expect(fn.unixepoch('0000-01-01T00:00')).toEqual(-62167219200n);
|
|
161
|
+
expect(fn.unixepoch('3000-01-01T00:00')).toEqual(32503680000n);
|
|
162
|
+
expect(fn.unixepoch('2023')).toEqual(-210691972800n);
|
|
163
|
+
expect(fn.unixepoch('2023.')).toEqual(-210691972800n);
|
|
164
|
+
expect(fn.unixepoch('2023.123')).toEqual(-210691962173n);
|
|
165
|
+
expect(fn.unixepoch(2023)).toEqual(-210691972800n);
|
|
166
|
+
expect(fn.unixepoch(2023n)).toEqual(-210691972800n);
|
|
167
|
+
expect(fn.unixepoch(2023.123)).toEqual(-210691962173n);
|
|
168
|
+
expect(fn.unixepoch(1687961520n, 'unixepoch')).toEqual(1687961520n);
|
|
169
|
+
expect(fn.unixepoch(1687961520, 'unixepoch')).toEqual(1687961520n);
|
|
170
|
+
expect(fn.unixepoch(1687961520.567, 'unixepoch')).toEqual(1687961520n);
|
|
171
|
+
expect(fn.unixepoch(1687961520.567, 'unixepoch', 'subsec')).toEqual(1687961520.567);
|
|
172
|
+
expect(fn.unixepoch(1687961520.567, 'unixepoch', 'subsecond')).toEqual(1687961520.567);
|
|
173
|
+
|
|
174
|
+
expect(fn.unixepoch('2023-06-28 14:12:00.999', 'subsec')).toEqual(1687961520.999);
|
|
175
|
+
expect(fn.unixepoch('2023-06-28 14:12:00.999', 'subsecond')).toEqual(1687961520.999);
|
|
176
|
+
expect(fn.unixepoch('2023-06-28 14:12:00.0', 'subsecond')).toEqual(1687961520.0);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('datetime', () => {
|
|
180
|
+
// Differences from SQLite
|
|
181
|
+
// These versions are not supported
|
|
182
|
+
expect(fn.datetime()).toEqual(null);
|
|
183
|
+
expect(fn.datetime('now')).toEqual(null);
|
|
184
|
+
expect(fn.datetime('2023-06-28 14:12', 'auto')).toEqual(null);
|
|
185
|
+
|
|
186
|
+
// Same as SQLite
|
|
187
|
+
expect(fn.datetime('2023-06-28 14:12')).toEqual('2023-06-28 14:12:00');
|
|
188
|
+
expect(fn.datetime('2023-06-28 14:12:00')).toEqual('2023-06-28 14:12:00');
|
|
189
|
+
expect(fn.datetime('2023-06-28 14:12:00+00:00')).toEqual('2023-06-28 14:12:00');
|
|
190
|
+
expect(fn.datetime('2023-06-28 14:12:00Z')).toEqual('2023-06-28 14:12:00');
|
|
191
|
+
expect(fn.datetime('2023-06-28 14:12:00.999')).toEqual('2023-06-28 14:12:00');
|
|
192
|
+
expect(fn.datetime('2023-06-28 14:12:00.999Z')).toEqual('2023-06-28 14:12:00');
|
|
193
|
+
expect(fn.datetime('2023-06-28T14:12:00.999Z')).toEqual('2023-06-28 14:12:00');
|
|
194
|
+
expect(fn.datetime('2023-06-28T14:12:00+00:00')).toEqual('2023-06-28 14:12:00');
|
|
195
|
+
expect(fn.datetime('2023-06-28T14:12+00:00')).toEqual('2023-06-28 14:12:00');
|
|
196
|
+
expect(fn.datetime('2023-06-28T16:12+02:00')).toEqual('2023-06-28 14:12:00');
|
|
197
|
+
expect(fn.datetime('2023-06-28T16:12+02')).toEqual(null);
|
|
198
|
+
expect(fn.datetime('2023-06-28')).toEqual('2023-06-28 00:00:00');
|
|
199
|
+
expect(fn.datetime('0000-01-01T00:00')).toEqual('0000-01-01 00:00:00');
|
|
200
|
+
expect(fn.datetime('3000-01-01T00:00')).toEqual('3000-01-01 00:00:00');
|
|
201
|
+
expect(fn.datetime('2023')).toEqual('-4707-06-08 12:00:00');
|
|
202
|
+
expect(fn.datetime('2023.')).toEqual('-4707-06-08 12:00:00');
|
|
203
|
+
expect(fn.datetime('2023.123')).toEqual('-4707-06-08 14:57:07');
|
|
204
|
+
expect(fn.datetime(2023)).toEqual('-4707-06-08 12:00:00');
|
|
205
|
+
expect(fn.datetime(2023n)).toEqual('-4707-06-08 12:00:00');
|
|
206
|
+
expect(fn.datetime(2023.123)).toEqual('-4707-06-08 14:57:07');
|
|
207
|
+
expect(fn.datetime(1687961520n, 'unixepoch')).toEqual('2023-06-28 14:12:00');
|
|
208
|
+
expect(fn.datetime(1687961520, 'unixepoch')).toEqual('2023-06-28 14:12:00');
|
|
209
|
+
expect(fn.datetime(1687961520.567, 'unixepoch')).toEqual('2023-06-28 14:12:00');
|
|
210
|
+
expect(fn.datetime(1687961520.567, 'unixepoch', 'subsec')).toEqual('2023-06-28 14:12:00.567');
|
|
211
|
+
expect(fn.datetime(1687961520.567, 'unixepoch', 'subsecond')).toEqual('2023-06-28 14:12:00.567');
|
|
212
|
+
|
|
213
|
+
expect(fn.datetime('2023-06-28 14:12:00.999', 'subsec')).toEqual('2023-06-28 14:12:00.999');
|
|
214
|
+
expect(fn.datetime('2023-06-28 14:12:00.999', 'subsecond')).toEqual('2023-06-28 14:12:00.999');
|
|
215
|
+
expect(fn.datetime('2023-06-28 14:12:00.0', 'subsecond')).toEqual('2023-06-28 14:12:00.000');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('ST_AsGeoJSON', () => {
|
|
219
|
+
expect(fn.st_asgeojson(null)).toEqual(null);
|
|
220
|
+
expect(fn.st_asgeojson('0101000000029a081b9ede4340d7a3703d0a3f5ac0')).toEqual(
|
|
221
|
+
'{"type":"Point","coordinates":[39.7392,-104.985]}'
|
|
222
|
+
);
|
|
223
|
+
expect(
|
|
224
|
+
fn.st_asgeojson(
|
|
225
|
+
'01030000000100000005000000000000000000000000000000000000000000000000000000000000000000f03f000000000000f03f000000000000f03f000000000000f03f000000000000000000000000000000000000000000000000'
|
|
226
|
+
)
|
|
227
|
+
).toEqual('{"type":"Polygon","coordinates":[[[0.0,0.0],[0.0,1.0],[1.0,1.0],[1.0,0.0],[0.0,0.0]]]}');
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('ST_X', () => {
|
|
231
|
+
expect(fn.st_x(null)).toEqual(null);
|
|
232
|
+
expect(fn.st_x('0101000000029a081b9ede4340d7a3703d0a3f5ac0')).toEqual(39.7392);
|
|
233
|
+
expect(
|
|
234
|
+
fn.st_x(
|
|
235
|
+
'01030000000100000005000000000000000000000000000000000000000000000000000000000000000000f03f000000000000f03f000000000000f03f000000000000f03f000000000000000000000000000000000000000000000000'
|
|
236
|
+
)
|
|
237
|
+
).toEqual(null);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
test('ST_Y', () => {
|
|
241
|
+
expect(fn.st_y(null)).toEqual(null);
|
|
242
|
+
expect(fn.st_y('0101000000029a081b9ede4340d7a3703d0a3f5ac0')).toEqual(-104.985);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('ST_AsText', () => {
|
|
246
|
+
expect(fn.st_astext(null)).toEqual(null);
|
|
247
|
+
expect(fn.st_astext('0101000000029a081b9ede4340d7a3703d0a3f5ac0')).toEqual('POINT(39.7392 -104.985)');
|
|
248
|
+
expect(
|
|
249
|
+
fn.st_astext(
|
|
250
|
+
`01030000000100000005000000000000000000000000000000000000000000000000000000000000000000F03F000000000000F03F000000000000F03F000000000000F03F000000000000000000000000000000000000000000000000`
|
|
251
|
+
)
|
|
252
|
+
).toEqual('POLYGON((0 0,0 1,1 1,1 0,0 0))');
|
|
253
|
+
});
|
|
254
|
+
});
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { evaluateOperator } from '@powersync/service-sync-rules';
|
|
2
|
+
import { describe, expect, test } from 'vitest';
|
|
3
|
+
|
|
4
|
+
describe('SQL operators', () => {
|
|
5
|
+
test('=', () => {
|
|
6
|
+
expect(evaluateOperator('=', 1, 1n)).toStrictEqual(1n);
|
|
7
|
+
expect(evaluateOperator('=', 1.1, 1n)).toStrictEqual(0n);
|
|
8
|
+
expect(evaluateOperator('=', '1', 1n)).toStrictEqual(0n);
|
|
9
|
+
expect(evaluateOperator('=', '', 0n)).toStrictEqual(0n);
|
|
10
|
+
expect(evaluateOperator('=', 1, null)).toStrictEqual(null);
|
|
11
|
+
expect(evaluateOperator('=', 0, null)).toStrictEqual(null);
|
|
12
|
+
expect(evaluateOperator('=', null, '')).toStrictEqual(null);
|
|
13
|
+
expect(evaluateOperator('=', null, null)).toStrictEqual(null);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('!=', () => {
|
|
17
|
+
expect(evaluateOperator('!=', 1, 1n)).toStrictEqual(0n);
|
|
18
|
+
expect(evaluateOperator('!=', 1.1, 1n)).toStrictEqual(1n);
|
|
19
|
+
expect(evaluateOperator('!=', '1', 1n)).toStrictEqual(1n);
|
|
20
|
+
expect(evaluateOperator('!=', '', 0n)).toStrictEqual(1n);
|
|
21
|
+
expect(evaluateOperator('!=', 1, null)).toStrictEqual(null);
|
|
22
|
+
expect(evaluateOperator('!=', 0, null)).toStrictEqual(null);
|
|
23
|
+
expect(evaluateOperator('!=', null, '')).toStrictEqual(null);
|
|
24
|
+
expect(evaluateOperator('!=', null, null)).toStrictEqual(null);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('IS', () => {
|
|
28
|
+
expect(evaluateOperator('IS', 1, 1n)).toStrictEqual(1n);
|
|
29
|
+
expect(evaluateOperator('IS', 1.1, 1n)).toStrictEqual(0n);
|
|
30
|
+
expect(evaluateOperator('IS', '1', 1n)).toStrictEqual(0n);
|
|
31
|
+
expect(evaluateOperator('IS', '', 0n)).toStrictEqual(0n);
|
|
32
|
+
expect(evaluateOperator('IS', 1, null)).toStrictEqual(0n);
|
|
33
|
+
expect(evaluateOperator('IS', 0, null)).toStrictEqual(0n);
|
|
34
|
+
expect(evaluateOperator('IS', null, '')).toStrictEqual(0n);
|
|
35
|
+
expect(evaluateOperator('IS', null, null)).toStrictEqual(1n);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('IS NOT', () => {
|
|
39
|
+
expect(evaluateOperator('IS NOT', 1, 1n)).toStrictEqual(0n);
|
|
40
|
+
expect(evaluateOperator('IS NOT', 1.1, 1n)).toStrictEqual(1n);
|
|
41
|
+
expect(evaluateOperator('IS NOT', '1', 1n)).toStrictEqual(1n);
|
|
42
|
+
expect(evaluateOperator('IS NOT', '', 0n)).toStrictEqual(1n);
|
|
43
|
+
expect(evaluateOperator('IS NOT', 1, null)).toStrictEqual(1n);
|
|
44
|
+
expect(evaluateOperator('IS NOT', 0, null)).toStrictEqual(1n);
|
|
45
|
+
expect(evaluateOperator('IS NOT', null, '')).toStrictEqual(1n);
|
|
46
|
+
expect(evaluateOperator('IS NOT', null, null)).toStrictEqual(0n);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('>', () => {
|
|
50
|
+
expect(evaluateOperator('>', 1, 1n)).toStrictEqual(0n);
|
|
51
|
+
expect(evaluateOperator('>', 2, 1n)).toStrictEqual(1n);
|
|
52
|
+
expect(evaluateOperator('>', 2n, 1)).toStrictEqual(1n);
|
|
53
|
+
expect(evaluateOperator('>', 'text', 1)).toStrictEqual(1n);
|
|
54
|
+
expect(evaluateOperator('>', 1, 'text')).toStrictEqual(0n);
|
|
55
|
+
expect(evaluateOperator('>', 1, null)).toStrictEqual(null);
|
|
56
|
+
expect(evaluateOperator('>', null, null)).toStrictEqual(null);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('>=', () => {
|
|
60
|
+
expect(evaluateOperator('>=', 1, 1n)).toStrictEqual(1n);
|
|
61
|
+
expect(evaluateOperator('>=', 2, 1n)).toStrictEqual(1n);
|
|
62
|
+
expect(evaluateOperator('>=', 2n, 1)).toStrictEqual(1n);
|
|
63
|
+
expect(evaluateOperator('>=', 'text', 1)).toStrictEqual(1n);
|
|
64
|
+
expect(evaluateOperator('>=', 1, 'text')).toStrictEqual(0n);
|
|
65
|
+
expect(evaluateOperator('>=', 1, null)).toStrictEqual(null);
|
|
66
|
+
expect(evaluateOperator('>=', null, null)).toStrictEqual(null);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('<', () => {
|
|
70
|
+
expect(evaluateOperator('<', 1, 1n)).toStrictEqual(0n);
|
|
71
|
+
expect(evaluateOperator('<', 1n, 2)).toStrictEqual(1n);
|
|
72
|
+
expect(evaluateOperator('<', 1, 2n)).toStrictEqual(1n);
|
|
73
|
+
expect(evaluateOperator('<', 'text', 1)).toStrictEqual(0n);
|
|
74
|
+
expect(evaluateOperator('<', 1, 'text')).toStrictEqual(1n);
|
|
75
|
+
expect(evaluateOperator('<', 1, null)).toStrictEqual(null);
|
|
76
|
+
expect(evaluateOperator('<', null, null)).toStrictEqual(null);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('<=', () => {
|
|
80
|
+
expect(evaluateOperator('<=', 1, 1n)).toStrictEqual(1n);
|
|
81
|
+
expect(evaluateOperator('<=', 1n, 2)).toStrictEqual(1n);
|
|
82
|
+
expect(evaluateOperator('<=', 1, 2n)).toStrictEqual(1n);
|
|
83
|
+
expect(evaluateOperator('<=', 'text', 1)).toStrictEqual(0n);
|
|
84
|
+
expect(evaluateOperator('<=', 1, 'text')).toStrictEqual(1n);
|
|
85
|
+
expect(evaluateOperator('<=', 1, null)).toStrictEqual(null);
|
|
86
|
+
expect(evaluateOperator('<=', null, null)).toStrictEqual(null);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('||', () => {
|
|
90
|
+
expect(evaluateOperator('||', 'test', null)).toStrictEqual(null);
|
|
91
|
+
expect(evaluateOperator('||', 'a', 31n)).toStrictEqual('a31');
|
|
92
|
+
expect(evaluateOperator('||', 3.1, 'a')).toStrictEqual('3.1a');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('+', () => {
|
|
96
|
+
expect(evaluateOperator('+', 1n, null)).toStrictEqual(null);
|
|
97
|
+
expect(evaluateOperator('+', 1n, 2)).toStrictEqual(3);
|
|
98
|
+
expect(evaluateOperator('+', 1n, 2n)).toStrictEqual(3n);
|
|
99
|
+
expect(evaluateOperator('+', 1, 2)).toStrictEqual(3);
|
|
100
|
+
expect(evaluateOperator('+', 1n, '2')).toStrictEqual(3n);
|
|
101
|
+
expect(evaluateOperator('+', 1n, '2.0')).toStrictEqual(3.0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test('/', () => {
|
|
105
|
+
expect(evaluateOperator('/', 1n, null)).toStrictEqual(null);
|
|
106
|
+
expect(evaluateOperator('/', 5n, 2)).toStrictEqual(2.5);
|
|
107
|
+
expect(evaluateOperator('/', 5n, 2n)).toStrictEqual(2n);
|
|
108
|
+
expect(evaluateOperator('/', 5, 2)).toStrictEqual(2.5);
|
|
109
|
+
expect(evaluateOperator('/', 5n, '2')).toStrictEqual(2n);
|
|
110
|
+
expect(evaluateOperator('/', 5n, '2.0')).toStrictEqual(2.5);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('*', () => {
|
|
114
|
+
expect(evaluateOperator('*', 3n, null)).toStrictEqual(null);
|
|
115
|
+
expect(evaluateOperator('*', 3n, 2)).toStrictEqual(6);
|
|
116
|
+
expect(evaluateOperator('*', 3n, 2n)).toStrictEqual(6n);
|
|
117
|
+
expect(evaluateOperator('*', 3, 2)).toStrictEqual(6);
|
|
118
|
+
expect(evaluateOperator('*', 3n, '2')).toStrictEqual(6n);
|
|
119
|
+
expect(evaluateOperator('*', 3n, '2.0')).toStrictEqual(6.0);
|
|
120
|
+
expect(evaluateOperator('*', '3', '2.0')).toStrictEqual(6.0);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('-', () => {
|
|
124
|
+
expect(evaluateOperator('-', 3n, null)).toStrictEqual(null);
|
|
125
|
+
expect(evaluateOperator('-', 3n, 2)).toStrictEqual(1);
|
|
126
|
+
expect(evaluateOperator('-', 3n, 2n)).toStrictEqual(1n);
|
|
127
|
+
expect(evaluateOperator('-', 3, 2)).toStrictEqual(1);
|
|
128
|
+
expect(evaluateOperator('-', 3n, '2')).toStrictEqual(1n);
|
|
129
|
+
expect(evaluateOperator('-', 3n, '2.0')).toStrictEqual(1.0);
|
|
130
|
+
expect(evaluateOperator('-', '3', '2.0')).toStrictEqual(1.0);
|
|
131
|
+
});
|
|
132
|
+
});
|