@powersync/service-core 0.0.0-dev-20240725112650 → 0.0.0-dev-20240918082156

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (331) hide show
  1. package/CHANGELOG.md +80 -2
  2. package/dist/api/RouteAPI.d.ts +68 -0
  3. package/dist/api/RouteAPI.js +2 -0
  4. package/dist/api/RouteAPI.js.map +1 -0
  5. package/dist/api/api-index.d.ts +1 -0
  6. package/dist/api/api-index.js +1 -0
  7. package/dist/api/api-index.js.map +1 -1
  8. package/dist/api/diagnostics.d.ts +4 -4
  9. package/dist/api/diagnostics.js +11 -65
  10. package/dist/api/diagnostics.js.map +1 -1
  11. package/dist/api/schema.d.ts +3 -5
  12. package/dist/api/schema.js +9 -79
  13. package/dist/api/schema.js.map +1 -1
  14. package/dist/auth/KeyStore.d.ts +7 -4
  15. package/dist/auth/KeyStore.js +1 -1
  16. package/dist/auth/KeyStore.js.map +1 -1
  17. package/dist/auth/auth-index.d.ts +0 -1
  18. package/dist/auth/auth-index.js +0 -1
  19. package/dist/auth/auth-index.js.map +1 -1
  20. package/dist/entry/cli-entry.js +3 -2
  21. package/dist/entry/cli-entry.js.map +1 -1
  22. package/dist/entry/commands/compact-action.js +12 -8
  23. package/dist/entry/commands/compact-action.js.map +1 -1
  24. package/dist/entry/commands/migrate-action.js +4 -5
  25. package/dist/entry/commands/migrate-action.js.map +1 -1
  26. package/dist/entry/commands/teardown-action.js +2 -2
  27. package/dist/entry/commands/teardown-action.js.map +1 -1
  28. package/dist/index.d.ts +4 -2
  29. package/dist/index.js +4 -2
  30. package/dist/index.js.map +1 -1
  31. package/dist/metrics/Metrics.d.ts +2 -2
  32. package/dist/metrics/Metrics.js +5 -13
  33. package/dist/metrics/Metrics.js.map +1 -1
  34. package/dist/migrations/db/migrations/1684951997326-init.d.ts +2 -2
  35. package/dist/migrations/db/migrations/1684951997326-init.js +4 -2
  36. package/dist/migrations/db/migrations/1684951997326-init.js.map +1 -1
  37. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +2 -2
  38. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +4 -2
  39. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +1 -1
  40. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +2 -2
  41. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +4 -2
  42. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +1 -1
  43. package/dist/migrations/migrations.d.ts +8 -0
  44. package/dist/migrations/migrations.js +19 -7
  45. package/dist/migrations/migrations.js.map +1 -1
  46. package/dist/modules/AbstractModule.d.ts +26 -0
  47. package/dist/modules/AbstractModule.js +11 -0
  48. package/dist/modules/AbstractModule.js.map +1 -0
  49. package/dist/modules/ModuleManager.d.ts +11 -0
  50. package/dist/modules/ModuleManager.js +32 -0
  51. package/dist/modules/ModuleManager.js.map +1 -0
  52. package/dist/modules/modules-index.d.ts +2 -0
  53. package/dist/modules/modules-index.js +3 -0
  54. package/dist/modules/modules-index.js.map +1 -0
  55. package/dist/replication/AbstractReplicationJob.d.ts +38 -0
  56. package/dist/replication/AbstractReplicationJob.js +51 -0
  57. package/dist/replication/AbstractReplicationJob.js.map +1 -0
  58. package/dist/replication/AbstractReplicator.d.ts +53 -0
  59. package/dist/replication/AbstractReplicator.js +187 -0
  60. package/dist/replication/AbstractReplicator.js.map +1 -0
  61. package/dist/replication/ErrorRateLimiter.d.ts +0 -9
  62. package/dist/replication/ErrorRateLimiter.js +1 -42
  63. package/dist/replication/ErrorRateLimiter.js.map +1 -1
  64. package/dist/replication/ReplicationEngine.d.ts +18 -0
  65. package/dist/replication/ReplicationEngine.js +41 -0
  66. package/dist/replication/ReplicationEngine.js.map +1 -0
  67. package/dist/replication/ReplicationModule.d.ts +39 -0
  68. package/dist/replication/ReplicationModule.js +65 -0
  69. package/dist/replication/ReplicationModule.js.map +1 -0
  70. package/dist/replication/replication-index.d.ts +4 -6
  71. package/dist/replication/replication-index.js +4 -6
  72. package/dist/replication/replication-index.js.map +1 -1
  73. package/dist/routes/RouterEngine.d.ts +42 -0
  74. package/dist/routes/RouterEngine.js +80 -0
  75. package/dist/routes/RouterEngine.js.map +1 -0
  76. package/dist/routes/auth.d.ts +2 -2
  77. package/dist/routes/auth.js +11 -11
  78. package/dist/routes/auth.js.map +1 -1
  79. package/dist/routes/configure-fastify.d.ts +30 -176
  80. package/dist/routes/configure-fastify.js +10 -11
  81. package/dist/routes/configure-fastify.js.map +1 -1
  82. package/dist/routes/configure-rsocket.d.ts +3 -3
  83. package/dist/routes/configure-rsocket.js +6 -5
  84. package/dist/routes/configure-rsocket.js.map +1 -1
  85. package/dist/routes/endpoints/admin.d.ts +0 -34
  86. package/dist/routes/endpoints/admin.js +48 -89
  87. package/dist/routes/endpoints/admin.js.map +1 -1
  88. package/dist/routes/endpoints/checkpointing.d.ts +56 -16
  89. package/dist/routes/endpoints/checkpointing.js +33 -12
  90. package/dist/routes/endpoints/checkpointing.js.map +1 -1
  91. package/dist/routes/endpoints/route-endpoints-index.d.ts +0 -1
  92. package/dist/routes/endpoints/route-endpoints-index.js +0 -1
  93. package/dist/routes/endpoints/route-endpoints-index.js.map +1 -1
  94. package/dist/routes/endpoints/socket-route.js +40 -25
  95. package/dist/routes/endpoints/socket-route.js.map +1 -1
  96. package/dist/routes/endpoints/sync-rules.d.ts +1 -1
  97. package/dist/routes/endpoints/sync-rules.js +32 -23
  98. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  99. package/dist/routes/endpoints/sync-stream.d.ts +10 -0
  100. package/dist/routes/endpoints/sync-stream.js +13 -8
  101. package/dist/routes/endpoints/sync-stream.js.map +1 -1
  102. package/dist/routes/router-socket.d.ts +1 -0
  103. package/dist/routes/router-socket.js +2 -1
  104. package/dist/routes/router-socket.js.map +1 -1
  105. package/dist/routes/router.d.ts +6 -2
  106. package/dist/routes/router.js.map +1 -1
  107. package/dist/routes/routes-index.d.ts +1 -0
  108. package/dist/routes/routes-index.js +1 -0
  109. package/dist/routes/routes-index.js.map +1 -1
  110. package/dist/runner/teardown.js +47 -76
  111. package/dist/runner/teardown.js.map +1 -1
  112. package/dist/storage/BucketStorage.d.ts +30 -19
  113. package/dist/storage/BucketStorage.js +0 -10
  114. package/dist/storage/BucketStorage.js.map +1 -1
  115. package/dist/storage/MongoBucketStorage.d.ts +4 -4
  116. package/dist/storage/MongoBucketStorage.js +19 -24
  117. package/dist/storage/MongoBucketStorage.js.map +1 -1
  118. package/dist/storage/SourceEntity.d.ts +20 -0
  119. package/dist/storage/SourceEntity.js +2 -0
  120. package/dist/storage/SourceEntity.js.map +1 -0
  121. package/dist/storage/SourceTable.d.ts +4 -5
  122. package/dist/storage/SourceTable.js +3 -4
  123. package/dist/storage/SourceTable.js.map +1 -1
  124. package/dist/storage/StorageEngine.d.ts +24 -0
  125. package/dist/storage/StorageEngine.js +43 -0
  126. package/dist/storage/StorageEngine.js.map +1 -0
  127. package/dist/storage/StorageProvider.d.ts +21 -0
  128. package/dist/storage/StorageProvider.js +2 -0
  129. package/dist/storage/StorageProvider.js.map +1 -0
  130. package/dist/storage/mongo/MongoBucketBatch.d.ts +1 -1
  131. package/dist/storage/mongo/MongoBucketBatch.js +6 -7
  132. package/dist/storage/mongo/MongoBucketBatch.js.map +1 -1
  133. package/dist/storage/mongo/MongoCompactor.js +2 -1
  134. package/dist/storage/mongo/MongoCompactor.js.map +1 -1
  135. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +2 -2
  136. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +2 -2
  137. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +1 -1
  138. package/dist/storage/mongo/MongoStorageProvider.d.ts +5 -0
  139. package/dist/storage/mongo/MongoStorageProvider.js +26 -0
  140. package/dist/storage/mongo/MongoStorageProvider.js.map +1 -0
  141. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +7 -6
  142. package/dist/storage/mongo/MongoSyncBucketStorage.js +24 -15
  143. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +1 -1
  144. package/dist/storage/mongo/MongoSyncRulesLock.js +1 -1
  145. package/dist/storage/mongo/MongoSyncRulesLock.js.map +1 -1
  146. package/dist/storage/mongo/OperationBatch.d.ts +7 -3
  147. package/dist/storage/mongo/OperationBatch.js +16 -7
  148. package/dist/storage/mongo/OperationBatch.js.map +1 -1
  149. package/dist/storage/mongo/PersistedBatch.d.ts +3 -3
  150. package/dist/storage/mongo/PersistedBatch.js +2 -2
  151. package/dist/storage/mongo/PersistedBatch.js.map +1 -1
  152. package/dist/storage/mongo/models.d.ts +13 -4
  153. package/dist/storage/mongo/models.js.map +1 -1
  154. package/dist/storage/mongo/util.d.ts +12 -1
  155. package/dist/storage/mongo/util.js +50 -2
  156. package/dist/storage/mongo/util.js.map +1 -1
  157. package/dist/storage/storage-index.d.ts +5 -2
  158. package/dist/storage/storage-index.js +5 -2
  159. package/dist/storage/storage-index.js.map +1 -1
  160. package/dist/sync/sync.d.ts +2 -1
  161. package/dist/sync/sync.js +36 -10
  162. package/dist/sync/sync.js.map +1 -1
  163. package/dist/sync/util.js.map +1 -1
  164. package/dist/system/ServiceContext.d.ts +37 -0
  165. package/dist/system/ServiceContext.js +48 -0
  166. package/dist/system/ServiceContext.js.map +1 -0
  167. package/dist/system/system-index.d.ts +1 -1
  168. package/dist/system/system-index.js +1 -1
  169. package/dist/system/system-index.js.map +1 -1
  170. package/dist/util/config/compound-config-collector.d.ts +9 -2
  171. package/dist/util/config/compound-config-collector.js +14 -23
  172. package/dist/util/config/compound-config-collector.js.map +1 -1
  173. package/dist/util/config/sync-rules/sync-rules-provider.d.ts +9 -0
  174. package/dist/util/config/sync-rules/sync-rules-provider.js +15 -0
  175. package/dist/util/config/sync-rules/sync-rules-provider.js.map +1 -0
  176. package/dist/util/config/types.d.ts +6 -4
  177. package/dist/util/config/types.js.map +1 -1
  178. package/dist/util/config.d.ts +3 -4
  179. package/dist/util/config.js +5 -20
  180. package/dist/util/config.js.map +1 -1
  181. package/dist/util/protocol-types.d.ts +4 -0
  182. package/dist/util/protocol-types.js +5 -1
  183. package/dist/util/protocol-types.js.map +1 -1
  184. package/dist/util/util-index.d.ts +3 -6
  185. package/dist/util/util-index.js +3 -6
  186. package/dist/util/util-index.js.map +1 -1
  187. package/dist/util/utils.d.ts +10 -6
  188. package/dist/util/utils.js +45 -25
  189. package/dist/util/utils.js.map +1 -1
  190. package/package.json +5 -7
  191. package/src/api/RouteAPI.ts +78 -0
  192. package/src/api/api-index.ts +1 -0
  193. package/src/api/diagnostics.ts +16 -71
  194. package/src/api/schema.ts +13 -89
  195. package/src/auth/KeyStore.ts +9 -6
  196. package/src/auth/auth-index.ts +0 -1
  197. package/src/entry/cli-entry.ts +3 -2
  198. package/src/entry/commands/compact-action.ts +12 -9
  199. package/src/entry/commands/migrate-action.ts +5 -8
  200. package/src/entry/commands/teardown-action.ts +2 -2
  201. package/src/index.ts +5 -2
  202. package/src/metrics/Metrics.ts +6 -16
  203. package/src/migrations/db/migrations/1684951997326-init.ts +9 -4
  204. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +7 -4
  205. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +6 -4
  206. package/src/migrations/migrations.ts +24 -8
  207. package/src/modules/AbstractModule.ts +37 -0
  208. package/src/modules/ModuleManager.ts +34 -0
  209. package/src/modules/modules-index.ts +2 -0
  210. package/src/replication/AbstractReplicationJob.ts +79 -0
  211. package/src/replication/AbstractReplicator.ts +227 -0
  212. package/src/replication/ErrorRateLimiter.ts +0 -44
  213. package/src/replication/ReplicationEngine.ts +43 -0
  214. package/src/replication/ReplicationModule.ts +101 -0
  215. package/src/replication/replication-index.ts +4 -6
  216. package/src/routes/RouterEngine.ts +120 -0
  217. package/src/routes/auth.ts +21 -12
  218. package/src/routes/configure-fastify.ts +13 -14
  219. package/src/routes/configure-rsocket.ts +9 -8
  220. package/src/routes/endpoints/admin.ts +74 -100
  221. package/src/routes/endpoints/checkpointing.ts +46 -12
  222. package/src/routes/endpoints/route-endpoints-index.ts +0 -1
  223. package/src/routes/endpoints/socket-route.ts +44 -27
  224. package/src/routes/endpoints/sync-rules.ts +41 -25
  225. package/src/routes/endpoints/sync-stream.ts +13 -8
  226. package/src/routes/router-socket.ts +2 -1
  227. package/src/routes/router.ts +6 -3
  228. package/src/routes/routes-index.ts +1 -0
  229. package/src/runner/teardown.ts +50 -88
  230. package/src/storage/BucketStorage.ts +38 -25
  231. package/src/storage/MongoBucketStorage.ts +23 -26
  232. package/src/storage/SourceEntity.ts +22 -0
  233. package/src/storage/SourceTable.ts +4 -6
  234. package/src/storage/StorageEngine.ts +55 -0
  235. package/src/storage/StorageProvider.ts +27 -0
  236. package/src/storage/mongo/MongoBucketBatch.ts +8 -8
  237. package/src/storage/mongo/MongoCompactor.ts +2 -1
  238. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +3 -3
  239. package/src/storage/mongo/MongoStorageProvider.ts +31 -0
  240. package/src/storage/mongo/MongoSyncBucketStorage.ts +39 -20
  241. package/src/storage/mongo/MongoSyncRulesLock.ts +1 -1
  242. package/src/storage/mongo/OperationBatch.ts +18 -11
  243. package/src/storage/mongo/PersistedBatch.ts +6 -5
  244. package/src/storage/mongo/models.ts +13 -4
  245. package/src/storage/mongo/util.ts +49 -4
  246. package/src/storage/storage-index.ts +5 -2
  247. package/src/sync/sync.ts +46 -11
  248. package/src/sync/util.ts +0 -1
  249. package/src/system/ServiceContext.ts +68 -0
  250. package/src/system/system-index.ts +1 -1
  251. package/src/util/config/compound-config-collector.ts +30 -31
  252. package/src/util/config/sync-rules/sync-rules-provider.ts +18 -0
  253. package/src/util/config/types.ts +6 -5
  254. package/src/util/config.ts +6 -23
  255. package/src/util/protocol-types.ts +6 -1
  256. package/src/util/util-index.ts +3 -6
  257. package/src/util/utils.ts +55 -39
  258. package/test/src/__snapshots__/sync.test.ts.snap +7 -7
  259. package/test/src/auth.test.ts +7 -7
  260. package/test/src/broadcast_iterable.test.ts +1 -1
  261. package/test/src/checksum_cache.test.ts +3 -3
  262. package/test/src/compacting.test.ts +26 -17
  263. package/test/src/data_storage.test.ts +258 -146
  264. package/test/src/env.ts +1 -3
  265. package/test/src/merge_iterable.test.ts +1 -6
  266. package/test/src/setup.ts +1 -1
  267. package/test/src/stream_utils.ts +42 -0
  268. package/test/src/sync.test.ts +52 -31
  269. package/test/src/util.ts +48 -51
  270. package/test/tsconfig.json +1 -1
  271. package/tsconfig.tsbuildinfo +1 -1
  272. package/dist/auth/SupabaseKeyCollector.d.ts +0 -22
  273. package/dist/auth/SupabaseKeyCollector.js +0 -61
  274. package/dist/auth/SupabaseKeyCollector.js.map +0 -1
  275. package/dist/replication/PgRelation.d.ts +0 -16
  276. package/dist/replication/PgRelation.js +0 -26
  277. package/dist/replication/PgRelation.js.map +0 -1
  278. package/dist/replication/WalConnection.d.ts +0 -34
  279. package/dist/replication/WalConnection.js +0 -190
  280. package/dist/replication/WalConnection.js.map +0 -1
  281. package/dist/replication/WalStream.d.ts +0 -57
  282. package/dist/replication/WalStream.js +0 -515
  283. package/dist/replication/WalStream.js.map +0 -1
  284. package/dist/replication/WalStreamManager.d.ts +0 -30
  285. package/dist/replication/WalStreamManager.js +0 -198
  286. package/dist/replication/WalStreamManager.js.map +0 -1
  287. package/dist/replication/WalStreamRunner.d.ts +0 -38
  288. package/dist/replication/WalStreamRunner.js +0 -155
  289. package/dist/replication/WalStreamRunner.js.map +0 -1
  290. package/dist/replication/util.d.ts +0 -9
  291. package/dist/replication/util.js +0 -62
  292. package/dist/replication/util.js.map +0 -1
  293. package/dist/routes/endpoints/dev.d.ts +0 -312
  294. package/dist/routes/endpoints/dev.js +0 -172
  295. package/dist/routes/endpoints/dev.js.map +0 -1
  296. package/dist/system/CorePowerSyncSystem.d.ts +0 -23
  297. package/dist/system/CorePowerSyncSystem.js +0 -52
  298. package/dist/system/CorePowerSyncSystem.js.map +0 -1
  299. package/dist/util/PgManager.d.ts +0 -24
  300. package/dist/util/PgManager.js +0 -55
  301. package/dist/util/PgManager.js.map +0 -1
  302. package/dist/util/migration_lib.d.ts +0 -11
  303. package/dist/util/migration_lib.js +0 -64
  304. package/dist/util/migration_lib.js.map +0 -1
  305. package/dist/util/pgwire_utils.d.ts +0 -24
  306. package/dist/util/pgwire_utils.js +0 -117
  307. package/dist/util/pgwire_utils.js.map +0 -1
  308. package/dist/util/populate_test_data.d.ts +0 -8
  309. package/dist/util/populate_test_data.js +0 -65
  310. package/dist/util/populate_test_data.js.map +0 -1
  311. package/src/auth/SupabaseKeyCollector.ts +0 -67
  312. package/src/replication/PgRelation.ts +0 -42
  313. package/src/replication/WalConnection.ts +0 -227
  314. package/src/replication/WalStream.ts +0 -624
  315. package/src/replication/WalStreamManager.ts +0 -213
  316. package/src/replication/WalStreamRunner.ts +0 -180
  317. package/src/replication/util.ts +0 -76
  318. package/src/routes/endpoints/dev.ts +0 -199
  319. package/src/system/CorePowerSyncSystem.ts +0 -64
  320. package/src/util/PgManager.ts +0 -64
  321. package/src/util/migration_lib.ts +0 -79
  322. package/src/util/pgwire_utils.ts +0 -139
  323. package/src/util/populate_test_data.ts +0 -78
  324. package/test/src/__snapshots__/pg_test.test.ts.snap +0 -256
  325. package/test/src/large_batch.test.ts +0 -194
  326. package/test/src/pg_test.test.ts +0 -450
  327. package/test/src/schema_changes.test.ts +0 -545
  328. package/test/src/slow_tests.test.ts +0 -338
  329. package/test/src/validation.test.ts +0 -63
  330. package/test/src/wal_stream.test.ts +0 -319
  331. package/test/src/wal_stream_utils.ts +0 -156
package/test/src/env.ts CHANGED
@@ -2,7 +2,5 @@ import { utils } from '@powersync/lib-services-framework';
2
2
 
3
3
  export const env = utils.collectEnvironmentVariables({
4
4
  MONGO_TEST_URL: utils.type.string.default('mongodb://localhost:27017/powersync_test'),
5
- PG_TEST_URL: utils.type.string.default('postgres://postgres:postgres@localhost:5432/powersync_test'),
6
- CI: utils.type.boolean.default('false'),
7
- SLOW_TESTS: utils.type.boolean.default('false')
5
+ CI: utils.type.boolean.default('false')
8
6
  });
@@ -1,11 +1,6 @@
1
+ import { mergeAsyncIterablesNew, mergeAsyncIterablesOld } from '@/sync/merge.js';
1
2
  import * as timers from 'timers/promises';
2
3
  import { describe, expect, test } from 'vitest';
3
- import {
4
- FixedMergeAsyncIterable,
5
- mergeAsyncIterables,
6
- mergeAsyncIterablesNew,
7
- mergeAsyncIterablesOld
8
- } from '../../src/sync/merge.js';
9
4
 
10
5
  type MergeIteratorFunction = <T>(source: AsyncIterable<T>[]) => AsyncIterable<T>;
11
6
 
package/test/src/setup.ts CHANGED
@@ -2,6 +2,6 @@ import { container } from '@powersync/lib-services-framework';
2
2
  import { beforeAll } from 'vitest';
3
3
 
4
4
  beforeAll(() => {
5
- // Your setup code here
5
+ // Executes for every test file
6
6
  container.registerDefaults();
7
7
  });
@@ -0,0 +1,42 @@
1
+ import { OplogEntry } from '@/util/protocol-types.js';
2
+ import { JSONBig } from '@powersync/service-jsonbig';
3
+
4
+ export function putOp(table: string, data: Record<string, any>): Partial<OplogEntry> {
5
+ return {
6
+ op: 'PUT',
7
+ object_type: table,
8
+ object_id: data.id,
9
+ data: JSONBig.stringify(data)
10
+ };
11
+ }
12
+
13
+ export function removeOp(table: string, id: string): Partial<OplogEntry> {
14
+ return {
15
+ op: 'REMOVE',
16
+ object_type: table,
17
+ object_id: id
18
+ };
19
+ }
20
+
21
+ export function compareIds(a: OplogEntry, b: OplogEntry) {
22
+ return a.object_id!.localeCompare(b.object_id!);
23
+ }
24
+
25
+ export async function oneFromAsync<T>(source: Iterable<T> | AsyncIterable<T>): Promise<T> {
26
+ const items: T[] = [];
27
+ for await (const item of source) {
28
+ items.push(item);
29
+ }
30
+ if (items.length != 1) {
31
+ throw new Error(`One item expected, got: ${items.length}`);
32
+ }
33
+ return items[0];
34
+ }
35
+
36
+ export async function fromAsync<T>(source: Iterable<T> | AsyncIterable<T>): Promise<T[]> {
37
+ const items: T[] = [];
38
+ for await (const item of source) {
39
+ items.push(item);
40
+ }
41
+ return items;
42
+ }
@@ -1,13 +1,19 @@
1
1
  import { RequestTracker } from '@/sync/RequestTracker.js';
2
+ import { streamResponse } from '@/sync/sync.js';
2
3
  import { StreamingSyncLine } from '@/util/protocol-types.js';
3
- import { lsnMakeComparable } from '@powersync/service-jpgwire';
4
4
  import { JSONBig } from '@powersync/service-jsonbig';
5
5
  import { RequestParameters } from '@powersync/service-sync-rules';
6
6
  import * as timers from 'timers/promises';
7
7
  import { describe, expect, test } from 'vitest';
8
- import { ZERO_LSN } from '../../src/replication/WalStream.js';
9
- import { streamResponse } from '../../src/sync/sync.js';
10
- import { makeTestTable, MONGO_STORAGE_FACTORY, StorageFactory } from './util.js';
8
+ import {
9
+ BATCH_OPTIONS,
10
+ makeTestTable,
11
+ MONGO_STORAGE_FACTORY,
12
+ PARSE_OPTIONS,
13
+ StorageFactory,
14
+ ZERO_LSN
15
+ } from './util.js';
16
+ import { ParseSyncRulesOptions, StartBatchOptions } from '@/storage/BucketStorage.js';
11
17
 
12
18
  describe('sync - mongodb', function () {
13
19
  defineTests(MONGO_STORAGE_FACTORY);
@@ -32,18 +38,19 @@ function defineTests(factory: StorageFactory) {
32
38
  content: BASIC_SYNC_RULES
33
39
  });
34
40
 
35
- const storage = await f.getInstance(syncRules.parsed());
41
+ const storage = await f.getInstance(syncRules);
36
42
  await storage.setSnapshotDone(ZERO_LSN);
37
43
  await storage.autoActivate();
38
44
 
39
- const result = await storage.startBatch({}, async (batch) => {
45
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
40
46
  await batch.save({
41
47
  sourceTable: TEST_TABLE,
42
48
  tag: 'insert',
43
49
  after: {
44
50
  id: 't1',
45
51
  description: 'Test 1'
46
- }
52
+ },
53
+ afterReplicaId: 't1'
47
54
  });
48
55
 
49
56
  await batch.save({
@@ -52,10 +59,11 @@ function defineTests(factory: StorageFactory) {
52
59
  after: {
53
60
  id: 't2',
54
61
  description: 'Test 2'
55
- }
62
+ },
63
+ afterReplicaId: 't2'
56
64
  });
57
65
 
58
- await batch.commit(lsnMakeComparable('0/1'));
66
+ await batch.commit('0/1');
59
67
  });
60
68
 
61
69
  const stream = streamResponse({
@@ -65,6 +73,7 @@ function defineTests(factory: StorageFactory) {
65
73
  include_checksum: true,
66
74
  raw_data: true
67
75
  },
76
+ parseOptions: PARSE_OPTIONS,
68
77
  tracker,
69
78
  syncParams: new RequestParameters({ sub: '' }, {}),
70
79
  token: { exp: Date.now() / 1000 + 10 } as any
@@ -81,11 +90,11 @@ function defineTests(factory: StorageFactory) {
81
90
  content: BASIC_SYNC_RULES
82
91
  });
83
92
 
84
- const storage = await f.getInstance(syncRules.parsed());
93
+ const storage = await f.getInstance(syncRules);
85
94
  await storage.setSnapshotDone(ZERO_LSN);
86
95
  await storage.autoActivate();
87
96
 
88
- const result = await storage.startBatch({}, async (batch) => {
97
+ const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
89
98
  await batch.save({
90
99
  sourceTable: TEST_TABLE,
91
100
  tag: 'insert',
@@ -93,10 +102,11 @@ function defineTests(factory: StorageFactory) {
93
102
  id: 't1',
94
103
  description: 'Test\n"string"',
95
104
  large_num: 12345678901234567890n
96
- }
105
+ },
106
+ afterReplicaId: 't1'
97
107
  });
98
108
 
99
- await batch.commit(lsnMakeComparable('0/1'));
109
+ await batch.commit('0/1');
100
110
  });
101
111
 
102
112
  const stream = streamResponse({
@@ -106,6 +116,7 @@ function defineTests(factory: StorageFactory) {
106
116
  include_checksum: true,
107
117
  raw_data: false
108
118
  },
119
+ parseOptions: PARSE_OPTIONS,
109
120
  tracker,
110
121
  syncParams: new RequestParameters({ sub: '' }, {}),
111
122
  token: { exp: Date.now() / 1000 + 10 } as any
@@ -124,7 +135,7 @@ function defineTests(factory: StorageFactory) {
124
135
  content: BASIC_SYNC_RULES
125
136
  });
126
137
 
127
- const storage = await f.getInstance(syncRules.parsed());
138
+ const storage = await f.getInstance(syncRules);
128
139
  await storage.setSnapshotDone(ZERO_LSN);
129
140
  await storage.autoActivate();
130
141
 
@@ -135,6 +146,7 @@ function defineTests(factory: StorageFactory) {
135
146
  include_checksum: true,
136
147
  raw_data: true
137
148
  },
149
+ parseOptions: PARSE_OPTIONS,
138
150
  tracker,
139
151
  syncParams: new RequestParameters({ sub: '' }, {}),
140
152
  token: { exp: 0 } as any
@@ -151,7 +163,7 @@ function defineTests(factory: StorageFactory) {
151
163
  content: BASIC_SYNC_RULES
152
164
  });
153
165
 
154
- const storage = await f.getInstance(syncRules.parsed());
166
+ const storage = await f.getInstance(syncRules);
155
167
  await storage.setSnapshotDone(ZERO_LSN);
156
168
  await storage.autoActivate();
157
169
 
@@ -162,6 +174,7 @@ function defineTests(factory: StorageFactory) {
162
174
  include_checksum: true,
163
175
  raw_data: true
164
176
  },
177
+ parseOptions: PARSE_OPTIONS,
165
178
  tracker,
166
179
  syncParams: new RequestParameters({ sub: '' }, {}),
167
180
  token: { exp: Date.now() / 1000 + 10 } as any
@@ -170,32 +183,34 @@ function defineTests(factory: StorageFactory) {
170
183
 
171
184
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
172
185
 
173
- await storage.startBatch({}, async (batch) => {
186
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
174
187
  await batch.save({
175
188
  sourceTable: TEST_TABLE,
176
189
  tag: 'insert',
177
190
  after: {
178
191
  id: 't1',
179
192
  description: 'Test 1'
180
- }
193
+ },
194
+ afterReplicaId: 't1'
181
195
  });
182
196
 
183
- await batch.commit(lsnMakeComparable('0/1'));
197
+ await batch.commit('0/1');
184
198
  });
185
199
 
186
200
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
187
201
 
188
- await storage.startBatch({}, async (batch) => {
202
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
189
203
  await batch.save({
190
204
  sourceTable: TEST_TABLE,
191
205
  tag: 'insert',
192
206
  after: {
193
207
  id: 't2',
194
208
  description: 'Test 2'
195
- }
209
+ },
210
+ afterReplicaId: 't2'
196
211
  });
197
212
 
198
- await batch.commit(lsnMakeComparable('0/2'));
213
+ await batch.commit('0/2');
199
214
  });
200
215
 
201
216
  expect(await getCheckpointLines(iter)).toMatchSnapshot();
@@ -210,7 +225,7 @@ function defineTests(factory: StorageFactory) {
210
225
  content: BASIC_SYNC_RULES
211
226
  });
212
227
 
213
- const storage = await f.getInstance(syncRules.parsed());
228
+ const storage = await f.getInstance(syncRules);
214
229
  await storage.setSnapshotDone(ZERO_LSN);
215
230
  await storage.autoActivate();
216
231
 
@@ -223,6 +238,7 @@ function defineTests(factory: StorageFactory) {
223
238
  include_checksum: true,
224
239
  raw_data: true
225
240
  },
241
+ parseOptions: PARSE_OPTIONS,
226
242
  tracker,
227
243
  syncParams: new RequestParameters({ sub: '' }, {}),
228
244
  token: { exp: exp } as any
@@ -248,18 +264,19 @@ function defineTests(factory: StorageFactory) {
248
264
  content: BASIC_SYNC_RULES
249
265
  });
250
266
 
251
- const storage = await f.getInstance(syncRules.parsed());
267
+ const storage = await f.getInstance(syncRules);
252
268
  await storage.setSnapshotDone(ZERO_LSN);
253
269
  await storage.autoActivate();
254
270
 
255
- await storage.startBatch({}, async (batch) => {
271
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
256
272
  await batch.save({
257
273
  sourceTable: TEST_TABLE,
258
274
  tag: 'insert',
259
275
  after: {
260
276
  id: 't1',
261
277
  description: 'Test 1'
262
- }
278
+ },
279
+ afterReplicaId: 't1'
263
280
  });
264
281
 
265
282
  await batch.save({
@@ -268,10 +285,11 @@ function defineTests(factory: StorageFactory) {
268
285
  after: {
269
286
  id: 't2',
270
287
  description: 'Test 2'
271
- }
288
+ },
289
+ afterReplicaId: 't2'
272
290
  });
273
291
 
274
- await batch.commit(lsnMakeComparable('0/1'));
292
+ await batch.commit('0/1');
275
293
  });
276
294
 
277
295
  const stream = streamResponse({
@@ -281,6 +299,7 @@ function defineTests(factory: StorageFactory) {
281
299
  include_checksum: true,
282
300
  raw_data: true
283
301
  },
302
+ parseOptions: PARSE_OPTIONS,
284
303
  tracker,
285
304
  syncParams: new RequestParameters({ sub: '' }, {}),
286
305
  token: { exp: Date.now() / 1000 + 10 } as any
@@ -300,14 +319,15 @@ function defineTests(factory: StorageFactory) {
300
319
  // Now we save additional data AND compact before continuing.
301
320
  // This invalidates the checkpoint we've received above.
302
321
 
303
- await storage.startBatch({}, async (batch) => {
322
+ await storage.startBatch(BATCH_OPTIONS, async (batch) => {
304
323
  await batch.save({
305
324
  sourceTable: TEST_TABLE,
306
325
  tag: 'update',
307
326
  after: {
308
327
  id: 't1',
309
328
  description: 'Test 1b'
310
- }
329
+ },
330
+ afterReplicaId: 't1'
311
331
  });
312
332
 
313
333
  await batch.save({
@@ -316,10 +336,11 @@ function defineTests(factory: StorageFactory) {
316
336
  after: {
317
337
  id: 't2',
318
338
  description: 'Test 2b'
319
- }
339
+ },
340
+ afterReplicaId: 't2'
320
341
  });
321
342
 
322
- await batch.commit(lsnMakeComparable('0/2'));
343
+ await batch.commit('0/2');
323
344
  });
324
345
 
325
346
  await storage.compact();
package/test/src/util.ts CHANGED
@@ -1,16 +1,21 @@
1
- import * as pgwire from '@powersync/service-jpgwire';
2
- import { normalizeConnection } from '@powersync/service-types';
3
- import * as mongo from 'mongodb';
4
- import { BucketStorageFactory, SyncBucketDataBatch } from '../../src/storage/BucketStorage.js';
5
- import { MongoBucketStorage } from '../../src/storage/MongoBucketStorage.js';
6
- import { PowerSyncMongo } from '../../src/storage/mongo/db.js';
7
- import { escapeIdentifier } from '../../src/util/pgwire_utils.js';
8
- import { env } from './env.js';
9
1
  import { Metrics } from '@/metrics/Metrics.js';
10
- import { hashData } from '@/util/utils.js';
2
+ import {
3
+ BucketStorageFactory,
4
+ ParseSyncRulesOptions,
5
+ PersistedSyncRulesContent,
6
+ StartBatchOptions,
7
+ SyncBucketDataBatch
8
+ } from '@/storage/BucketStorage.js';
9
+ import { MongoBucketStorage } from '@/storage/MongoBucketStorage.js';
11
10
  import { SourceTable } from '@/storage/SourceTable.js';
12
- import * as bson from 'bson';
11
+ import { PowerSyncMongo } from '@/storage/mongo/db.js';
13
12
  import { SyncBucketData } from '@/util/protocol-types.js';
13
+ import { getUuidReplicaIdentityBson, hashData } from '@/util/utils.js';
14
+ import * as bson from 'bson';
15
+ import * as mongo from 'mongodb';
16
+ import { env } from './env.js';
17
+ import { SqlSyncRules } from '@powersync/service-sync-rules';
18
+ import { ReplicaId } from '@/storage/storage-index.js';
14
19
 
15
20
  // The metrics need to be initialised before they can be used
16
21
  await Metrics.initialise({
@@ -20,8 +25,6 @@ await Metrics.initialise({
20
25
  });
21
26
  Metrics.getInstance().resetCounters();
22
27
 
23
- export const TEST_URI = env.PG_TEST_URL;
24
-
25
28
  export type StorageFactory = () => Promise<BucketStorageFactory>;
26
29
 
27
30
  export const MONGO_STORAGE_FACTORY: StorageFactory = async () => {
@@ -30,45 +33,33 @@ export const MONGO_STORAGE_FACTORY: StorageFactory = async () => {
30
33
  return new MongoBucketStorage(db, { slot_name_prefix: 'test_' });
31
34
  };
32
35
 
33
- export async function clearTestDb(db: pgwire.PgClient) {
34
- await db.query(
35
- "select pg_drop_replication_slot(slot_name) from pg_replication_slots where active = false and slot_name like 'test_%'"
36
- );
36
+ export const ZERO_LSN = '0/0';
37
37
 
38
- await db.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
39
- try {
40
- await db.query(`DROP PUBLICATION powersync`);
41
- } catch (e) {
42
- // Ignore
43
- }
38
+ export const PARSE_OPTIONS: ParseSyncRulesOptions = {
39
+ defaultSchema: 'public'
40
+ };
44
41
 
45
- await db.query(`CREATE PUBLICATION powersync FOR ALL TABLES`);
42
+ export const BATCH_OPTIONS: StartBatchOptions = {
43
+ ...PARSE_OPTIONS,
44
+ zeroLSN: ZERO_LSN
45
+ };
46
46
 
47
- const tableRows = pgwire.pgwireRows(
48
- await db.query(`SELECT table_name FROM information_schema.tables where table_schema = 'public'`)
49
- );
50
- for (let row of tableRows) {
51
- const name = row.table_name;
52
- if (name.startsWith('test_')) {
53
- await db.query(`DROP TABLE public.${escapeIdentifier(name)}`);
47
+ export function testRules(content: string): PersistedSyncRulesContent {
48
+ return {
49
+ id: 1,
50
+ sync_rules_content: content,
51
+ slot_name: 'test',
52
+ parsed(options) {
53
+ return {
54
+ id: 1,
55
+ sync_rules: SqlSyncRules.fromYaml(content, PARSE_OPTIONS),
56
+ slot_name: 'test'
57
+ };
58
+ },
59
+ lock() {
60
+ throw new Error('Not implemented');
54
61
  }
55
- }
56
- }
57
-
58
- export const TEST_CONNECTION_OPTIONS = normalizeConnection({
59
- type: 'postgresql',
60
- uri: TEST_URI,
61
- sslmode: 'disable'
62
- });
63
-
64
- export async function connectPgWire(type?: 'replication' | 'standard') {
65
- const db = await pgwire.connectPgWire(TEST_CONNECTION_OPTIONS, { type });
66
- return db;
67
- }
68
-
69
- export function connectPgPool() {
70
- const db = pgwire.connectPgWirePool(TEST_CONNECTION_OPTIONS);
71
- return db;
62
+ };
72
63
  }
73
64
 
74
65
  export async function connectMongo() {
@@ -79,8 +70,7 @@ export async function connectMongo() {
79
70
  socketTimeoutMS: env.CI ? 15_000 : 5_000,
80
71
  serverSelectionTimeoutMS: env.CI ? 15_000 : 2_500
81
72
  });
82
- const db = new PowerSyncMongo(client);
83
- return db;
73
+ return new PowerSyncMongo(client);
84
74
  }
85
75
 
86
76
  export function makeTestTable(name: string, columns?: string[] | undefined) {
@@ -90,9 +80,9 @@ export function makeTestTable(name: string, columns?: string[] | undefined) {
90
80
  id,
91
81
  SourceTable.DEFAULT_TAG,
92
82
  relId,
93
- SourceTable.DEFAULT_SCHEMA,
83
+ 'public',
94
84
  name,
95
- (columns ?? ['id']).map((column) => ({ name: column, typeOid: 25 })),
85
+ (columns ?? ['id']).map((column) => ({ name: column, type: 'VARCHAR', typeId: 25 })),
96
86
  true
97
87
  );
98
88
  }
@@ -138,3 +128,10 @@ function getFirst(batch: SyncBucketData[] | SyncBucketDataBatch[] | SyncBucketDa
138
128
  return first as SyncBucketData;
139
129
  }
140
130
  }
131
+
132
+ /**
133
+ * Replica id in the old Postgres format, for backwards-compatible tests.
134
+ */
135
+ export function rid(id: string): bson.UUID {
136
+ return getUuidReplicaIdentityBson({ id: id }, [{ name: 'id', type: 'VARCHAR', typeId: 25 }]);
137
+ }
@@ -2,8 +2,8 @@
2
2
  "extends": "../../../tsconfig.base.json",
3
3
  "compilerOptions": {
4
4
  "rootDir": "src",
5
- "noEmit": true,
6
5
  "baseUrl": "./",
6
+ "outDir": "dist",
7
7
  "esModuleInterop": true,
8
8
  "skipLibCheck": true,
9
9
  "sourceMap": true,