@powersync/service-core 0.0.0-dev-20241119082750 → 0.0.0-dev-20241219091224

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 (201) hide show
  1. package/CHANGELOG.md +88 -18
  2. package/dist/auth/KeySpec.d.ts +1 -0
  3. package/dist/auth/KeySpec.js +10 -8
  4. package/dist/auth/KeySpec.js.map +1 -1
  5. package/dist/auth/RemoteJWKSCollector.js +2 -2
  6. package/dist/auth/RemoteJWKSCollector.js.map +1 -1
  7. package/dist/auth/StaticSupabaseKeyCollector.d.ts +19 -0
  8. package/dist/auth/StaticSupabaseKeyCollector.js +28 -0
  9. package/dist/auth/StaticSupabaseKeyCollector.js.map +1 -0
  10. package/dist/auth/auth-index.d.ts +1 -0
  11. package/dist/auth/auth-index.js +1 -0
  12. package/dist/auth/auth-index.js.map +1 -1
  13. package/dist/entry/commands/compact-action.js +15 -15
  14. package/dist/entry/commands/compact-action.js.map +1 -1
  15. package/dist/entry/commands/migrate-action.js +15 -4
  16. package/dist/entry/commands/migrate-action.js.map +1 -1
  17. package/dist/index.d.ts +1 -3
  18. package/dist/index.js +1 -3
  19. package/dist/index.js.map +1 -1
  20. package/dist/migrations/PowerSyncMigrationManager.d.ts +17 -0
  21. package/dist/migrations/PowerSyncMigrationManager.js +22 -0
  22. package/dist/migrations/PowerSyncMigrationManager.js.map +1 -0
  23. package/dist/migrations/ensure-automatic-migrations.d.ts +4 -0
  24. package/dist/migrations/ensure-automatic-migrations.js +14 -0
  25. package/dist/migrations/ensure-automatic-migrations.js.map +1 -0
  26. package/dist/migrations/migrations-index.d.ts +2 -3
  27. package/dist/migrations/migrations-index.js +2 -3
  28. package/dist/migrations/migrations-index.js.map +1 -1
  29. package/dist/routes/RouterEngine.js +2 -1
  30. package/dist/routes/RouterEngine.js.map +1 -1
  31. package/dist/routes/configure-fastify.d.ts +28 -28
  32. package/dist/routes/endpoints/admin.d.ts +24 -24
  33. package/dist/routes/endpoints/sync-rules.js.map +1 -1
  34. package/dist/storage/BucketStorage.d.ts +41 -1
  35. package/dist/storage/BucketStorage.js +26 -0
  36. package/dist/storage/BucketStorage.js.map +1 -1
  37. package/dist/storage/storage-index.d.ts +2 -14
  38. package/dist/storage/storage-index.js +2 -14
  39. package/dist/storage/storage-index.js.map +1 -1
  40. package/dist/sync/sync.js +12 -3
  41. package/dist/sync/sync.js.map +1 -1
  42. package/dist/system/ServiceContext.d.ts +3 -0
  43. package/dist/system/ServiceContext.js +11 -3
  44. package/dist/system/ServiceContext.js.map +1 -1
  45. package/dist/util/config/compound-config-collector.js +16 -0
  46. package/dist/util/config/compound-config-collector.js.map +1 -1
  47. package/dist/util/config/types.d.ts +2 -2
  48. package/dist/util/utils.d.ts +14 -1
  49. package/dist/util/utils.js +56 -0
  50. package/dist/util/utils.js.map +1 -1
  51. package/package.json +7 -11
  52. package/src/auth/KeySpec.ts +12 -9
  53. package/src/auth/RemoteJWKSCollector.ts +2 -2
  54. package/src/auth/StaticSupabaseKeyCollector.ts +31 -0
  55. package/src/auth/auth-index.ts +1 -0
  56. package/src/entry/commands/compact-action.ts +20 -15
  57. package/src/entry/commands/migrate-action.ts +17 -4
  58. package/src/index.ts +1 -4
  59. package/src/migrations/PowerSyncMigrationManager.ts +43 -0
  60. package/src/migrations/ensure-automatic-migrations.ts +15 -0
  61. package/src/migrations/migrations-index.ts +2 -3
  62. package/src/routes/RouterEngine.ts +2 -1
  63. package/src/routes/endpoints/sync-rules.ts +1 -2
  64. package/src/storage/BucketStorage.ts +44 -1
  65. package/src/storage/storage-index.ts +3 -15
  66. package/src/sync/sync.ts +12 -3
  67. package/src/system/ServiceContext.ts +17 -4
  68. package/src/util/config/compound-config-collector.ts +19 -1
  69. package/src/util/config/types.ts +2 -2
  70. package/src/util/utils.ts +59 -1
  71. package/test/src/auth.test.ts +54 -21
  72. package/test/src/env.ts +0 -1
  73. package/tsconfig.tsbuildinfo +1 -1
  74. package/dist/db/db-index.d.ts +0 -1
  75. package/dist/db/db-index.js +0 -2
  76. package/dist/db/db-index.js.map +0 -1
  77. package/dist/db/mongo.d.ts +0 -35
  78. package/dist/db/mongo.js +0 -73
  79. package/dist/db/mongo.js.map +0 -1
  80. package/dist/locks/LockManager.d.ts +0 -10
  81. package/dist/locks/LockManager.js +0 -7
  82. package/dist/locks/LockManager.js.map +0 -1
  83. package/dist/locks/MongoLocks.d.ts +0 -36
  84. package/dist/locks/MongoLocks.js +0 -81
  85. package/dist/locks/MongoLocks.js.map +0 -1
  86. package/dist/locks/locks-index.d.ts +0 -2
  87. package/dist/locks/locks-index.js +0 -3
  88. package/dist/locks/locks-index.js.map +0 -1
  89. package/dist/migrations/db/migrations/1684951997326-init.d.ts +0 -3
  90. package/dist/migrations/db/migrations/1684951997326-init.js +0 -33
  91. package/dist/migrations/db/migrations/1684951997326-init.js.map +0 -1
  92. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.d.ts +0 -2
  93. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js +0 -5
  94. package/dist/migrations/db/migrations/1688556755264-initial-sync-rules.js.map +0 -1
  95. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.d.ts +0 -3
  96. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js +0 -56
  97. package/dist/migrations/db/migrations/1702295701188-sync-rule-state.js.map +0 -1
  98. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.d.ts +0 -3
  99. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js +0 -29
  100. package/dist/migrations/db/migrations/1711543888062-write-checkpoint-index.js.map +0 -1
  101. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.d.ts +0 -3
  102. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js +0 -31
  103. package/dist/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.js.map +0 -1
  104. package/dist/migrations/definitions.d.ts +0 -18
  105. package/dist/migrations/definitions.js +0 -6
  106. package/dist/migrations/definitions.js.map +0 -1
  107. package/dist/migrations/executor.d.ts +0 -16
  108. package/dist/migrations/executor.js +0 -64
  109. package/dist/migrations/executor.js.map +0 -1
  110. package/dist/migrations/migrations.d.ts +0 -18
  111. package/dist/migrations/migrations.js +0 -110
  112. package/dist/migrations/migrations.js.map +0 -1
  113. package/dist/migrations/store/migration-store.d.ts +0 -11
  114. package/dist/migrations/store/migration-store.js +0 -46
  115. package/dist/migrations/store/migration-store.js.map +0 -1
  116. package/dist/storage/MongoBucketStorage.d.ts +0 -48
  117. package/dist/storage/MongoBucketStorage.js +0 -426
  118. package/dist/storage/MongoBucketStorage.js.map +0 -1
  119. package/dist/storage/mongo/MongoBucketBatch.d.ts +0 -67
  120. package/dist/storage/mongo/MongoBucketBatch.js +0 -643
  121. package/dist/storage/mongo/MongoBucketBatch.js.map +0 -1
  122. package/dist/storage/mongo/MongoCompactor.d.ts +0 -40
  123. package/dist/storage/mongo/MongoCompactor.js +0 -309
  124. package/dist/storage/mongo/MongoCompactor.js.map +0 -1
  125. package/dist/storage/mongo/MongoIdSequence.d.ts +0 -12
  126. package/dist/storage/mongo/MongoIdSequence.js +0 -21
  127. package/dist/storage/mongo/MongoIdSequence.js.map +0 -1
  128. package/dist/storage/mongo/MongoPersistedSyncRules.d.ts +0 -9
  129. package/dist/storage/mongo/MongoPersistedSyncRules.js +0 -9
  130. package/dist/storage/mongo/MongoPersistedSyncRules.js.map +0 -1
  131. package/dist/storage/mongo/MongoPersistedSyncRulesContent.d.ts +0 -20
  132. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js +0 -26
  133. package/dist/storage/mongo/MongoPersistedSyncRulesContent.js.map +0 -1
  134. package/dist/storage/mongo/MongoStorageProvider.d.ts +0 -5
  135. package/dist/storage/mongo/MongoStorageProvider.js +0 -26
  136. package/dist/storage/mongo/MongoStorageProvider.js.map +0 -1
  137. package/dist/storage/mongo/MongoSyncBucketStorage.d.ts +0 -38
  138. package/dist/storage/mongo/MongoSyncBucketStorage.js +0 -531
  139. package/dist/storage/mongo/MongoSyncBucketStorage.js.map +0 -1
  140. package/dist/storage/mongo/MongoSyncRulesLock.d.ts +0 -16
  141. package/dist/storage/mongo/MongoSyncRulesLock.js +0 -65
  142. package/dist/storage/mongo/MongoSyncRulesLock.js.map +0 -1
  143. package/dist/storage/mongo/MongoWriteCheckpointAPI.d.ts +0 -20
  144. package/dist/storage/mongo/MongoWriteCheckpointAPI.js +0 -103
  145. package/dist/storage/mongo/MongoWriteCheckpointAPI.js.map +0 -1
  146. package/dist/storage/mongo/OperationBatch.d.ts +0 -35
  147. package/dist/storage/mongo/OperationBatch.js +0 -119
  148. package/dist/storage/mongo/OperationBatch.js.map +0 -1
  149. package/dist/storage/mongo/PersistedBatch.d.ts +0 -46
  150. package/dist/storage/mongo/PersistedBatch.js +0 -213
  151. package/dist/storage/mongo/PersistedBatch.js.map +0 -1
  152. package/dist/storage/mongo/config.d.ts +0 -19
  153. package/dist/storage/mongo/config.js +0 -26
  154. package/dist/storage/mongo/config.js.map +0 -1
  155. package/dist/storage/mongo/db.d.ts +0 -36
  156. package/dist/storage/mongo/db.js +0 -47
  157. package/dist/storage/mongo/db.js.map +0 -1
  158. package/dist/storage/mongo/models.d.ts +0 -156
  159. package/dist/storage/mongo/models.js +0 -27
  160. package/dist/storage/mongo/models.js.map +0 -1
  161. package/dist/storage/mongo/util.d.ts +0 -40
  162. package/dist/storage/mongo/util.js +0 -151
  163. package/dist/storage/mongo/util.js.map +0 -1
  164. package/src/db/db-index.ts +0 -1
  165. package/src/db/mongo.ts +0 -81
  166. package/src/locks/LockManager.ts +0 -16
  167. package/src/locks/MongoLocks.ts +0 -142
  168. package/src/locks/locks-index.ts +0 -2
  169. package/src/migrations/db/migrations/1684951997326-init.ts +0 -38
  170. package/src/migrations/db/migrations/1688556755264-initial-sync-rules.ts +0 -5
  171. package/src/migrations/db/migrations/1702295701188-sync-rule-state.ts +0 -102
  172. package/src/migrations/db/migrations/1711543888062-write-checkpoint-index.ts +0 -34
  173. package/src/migrations/db/migrations/1727099539247-custom-write-checkpoint-index.ts +0 -37
  174. package/src/migrations/definitions.ts +0 -21
  175. package/src/migrations/executor.ts +0 -87
  176. package/src/migrations/migrations.ts +0 -142
  177. package/src/migrations/store/migration-store.ts +0 -63
  178. package/src/storage/MongoBucketStorage.ts +0 -540
  179. package/src/storage/mongo/MongoBucketBatch.ts +0 -841
  180. package/src/storage/mongo/MongoCompactor.ts +0 -392
  181. package/src/storage/mongo/MongoIdSequence.ts +0 -24
  182. package/src/storage/mongo/MongoPersistedSyncRules.ts +0 -16
  183. package/src/storage/mongo/MongoPersistedSyncRulesContent.ts +0 -50
  184. package/src/storage/mongo/MongoStorageProvider.ts +0 -31
  185. package/src/storage/mongo/MongoSyncBucketStorage.ts +0 -636
  186. package/src/storage/mongo/MongoSyncRulesLock.ts +0 -85
  187. package/src/storage/mongo/MongoWriteCheckpointAPI.ts +0 -151
  188. package/src/storage/mongo/OperationBatch.ts +0 -131
  189. package/src/storage/mongo/PersistedBatch.ts +0 -272
  190. package/src/storage/mongo/config.ts +0 -40
  191. package/src/storage/mongo/db.ts +0 -88
  192. package/src/storage/mongo/models.ts +0 -179
  193. package/src/storage/mongo/util.ts +0 -158
  194. package/test/src/__snapshots__/sync.test.ts.snap +0 -332
  195. package/test/src/bucket_validation.test.ts +0 -142
  196. package/test/src/bucket_validation.ts +0 -116
  197. package/test/src/compacting.test.ts +0 -295
  198. package/test/src/data_storage.test.ts +0 -1499
  199. package/test/src/stream_utils.ts +0 -42
  200. package/test/src/sync.test.ts +0 -511
  201. package/test/src/util.ts +0 -148
@@ -1,1499 +0,0 @@
1
- import { BucketDataBatchOptions, SaveOperationTag } from '@/storage/BucketStorage.js';
2
- import { getUuidReplicaIdentityBson } from '@/util/util-index.js';
3
- import { RequestParameters } from '@powersync/service-sync-rules';
4
- import { describe, expect, test } from 'vitest';
5
- import { fromAsync, oneFromAsync } from './stream_utils.js';
6
- import {
7
- BATCH_OPTIONS,
8
- getBatchData,
9
- getBatchMeta,
10
- makeTestTable,
11
- MONGO_STORAGE_FACTORY,
12
- PARSE_OPTIONS,
13
- rid,
14
- StorageFactory,
15
- testRules
16
- } from './util.js';
17
-
18
- const TEST_TABLE = makeTestTable('test', ['id']);
19
-
20
- describe('store - mongodb', function () {
21
- defineDataStorageTests(MONGO_STORAGE_FACTORY);
22
- });
23
-
24
- function defineDataStorageTests(factory: StorageFactory) {
25
- test('save and load parameters', async () => {
26
- const sync_rules = testRules(`
27
- bucket_definitions:
28
- mybucket:
29
- parameters:
30
- - SELECT group_id FROM test WHERE id1 = token_parameters.user_id OR id2 = token_parameters.user_id
31
- data: []
32
- `);
33
-
34
- const storage = (await factory()).getInstance(sync_rules);
35
-
36
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
37
- await batch.save({
38
- sourceTable: TEST_TABLE,
39
- tag: SaveOperationTag.INSERT,
40
- after: {
41
- id: 't2',
42
- id1: 'user3',
43
- id2: 'user4',
44
- group_id: 'group2a'
45
- },
46
- afterReplicaId: rid('t2')
47
- });
48
-
49
- await batch.save({
50
- sourceTable: TEST_TABLE,
51
- tag: SaveOperationTag.INSERT,
52
- after: {
53
- id: 't1',
54
- id1: 'user1',
55
- id2: 'user2',
56
- group_id: 'group1a'
57
- },
58
- afterReplicaId: rid('t1')
59
- });
60
- });
61
-
62
- const parameters = await storage.getParameterSets(result!.flushed_op, [['mybucket', '1', 'user1']]);
63
- expect(parameters).toEqual([
64
- {
65
- group_id: 'group1a'
66
- }
67
- ]);
68
- });
69
-
70
- test('it should use the latest version', async () => {
71
- const sync_rules = testRules(
72
- `
73
- bucket_definitions:
74
- mybucket:
75
- parameters:
76
- - SELECT group_id FROM test WHERE id = token_parameters.user_id
77
- data: []
78
- `
79
- );
80
-
81
- const storage = (await factory()).getInstance(sync_rules);
82
-
83
- const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
84
- await batch.save({
85
- sourceTable: TEST_TABLE,
86
- tag: SaveOperationTag.INSERT,
87
- after: {
88
- id: 'user1',
89
- group_id: 'group1'
90
- },
91
- afterReplicaId: rid('user1')
92
- });
93
- });
94
- const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
95
- await batch.save({
96
- sourceTable: TEST_TABLE,
97
- tag: SaveOperationTag.INSERT,
98
- after: {
99
- id: 'user1',
100
- group_id: 'group2'
101
- },
102
- afterReplicaId: rid('user1')
103
- });
104
- });
105
-
106
- const parameters = await storage.getParameterSets(result2!.flushed_op, [['mybucket', '1', 'user1']]);
107
- expect(parameters).toEqual([
108
- {
109
- group_id: 'group2'
110
- }
111
- ]);
112
-
113
- // Use the checkpoint to get older data if relevant
114
- const parameters2 = await storage.getParameterSets(result1!.flushed_op, [['mybucket', '1', 'user1']]);
115
- expect(parameters2).toEqual([
116
- {
117
- group_id: 'group1'
118
- }
119
- ]);
120
- });
121
-
122
- test('save and load parameters with different number types', async () => {
123
- const sync_rules = testRules(
124
- `
125
- bucket_definitions:
126
- mybucket:
127
- parameters:
128
- - SELECT group_id FROM test WHERE n1 = token_parameters.n1 and f2 = token_parameters.f2 and f3 = token_parameters.f3
129
- data: []
130
- `
131
- );
132
-
133
- const storage = (await factory()).getInstance(sync_rules);
134
-
135
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
136
- await batch.save({
137
- sourceTable: TEST_TABLE,
138
- tag: SaveOperationTag.INSERT,
139
- after: {
140
- id: 't1',
141
- group_id: 'group1',
142
- n1: 314n,
143
- f2: 314,
144
- f3: 3.14
145
- },
146
- afterReplicaId: rid('t1')
147
- });
148
- });
149
-
150
- const TEST_PARAMS = { group_id: 'group1' };
151
-
152
- const checkpoint = result!.flushed_op;
153
-
154
- const parameters1 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314n, 314, 3.14]]);
155
- expect(parameters1).toEqual([TEST_PARAMS]);
156
- const parameters2 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314, 314n, 3.14]]);
157
- expect(parameters2).toEqual([TEST_PARAMS]);
158
- const parameters3 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 314n, 314, 3]]);
159
- expect(parameters3).toEqual([]);
160
- });
161
-
162
- test('save and load parameters with large numbers', async () => {
163
- // This ensures serialization / deserialization of "current_data" is done correctly.
164
- // This specific case tested here cannot happen with postgres in practice, but we still
165
- // test this to ensure correct deserialization.
166
-
167
- const sync_rules = testRules(
168
- `
169
- bucket_definitions:
170
- mybucket:
171
- parameters:
172
- - SELECT group_id FROM test WHERE n1 = token_parameters.n1
173
- data: []
174
- `
175
- );
176
-
177
- const storage = (await factory()).getInstance(sync_rules);
178
-
179
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
180
- await batch.save({
181
- sourceTable: TEST_TABLE,
182
- tag: SaveOperationTag.INSERT,
183
- after: {
184
- id: 't1',
185
- group_id: 'group1',
186
- n1: 1152921504606846976n // 2^60
187
- },
188
- afterReplicaId: rid('t1')
189
- });
190
-
191
- await batch.save({
192
- sourceTable: TEST_TABLE,
193
- tag: SaveOperationTag.UPDATE,
194
- after: {
195
- id: 't1',
196
- group_id: 'group1',
197
- // Simulate a TOAST value, even though it can't happen for values like this
198
- // in practice.
199
- n1: undefined
200
- },
201
- afterReplicaId: rid('t1')
202
- });
203
- });
204
-
205
- const TEST_PARAMS = { group_id: 'group1' };
206
-
207
- const checkpoint = result!.flushed_op;
208
-
209
- const parameters1 = await storage.getParameterSets(checkpoint, [['mybucket', '1', 1152921504606846976n]]);
210
- expect(parameters1).toEqual([TEST_PARAMS]);
211
- });
212
-
213
- test('removing row', async () => {
214
- const sync_rules = testRules(
215
- `
216
- bucket_definitions:
217
- global:
218
- data:
219
- - SELECT id, description FROM "%"
220
- `
221
- );
222
- const storage = (await factory()).getInstance(sync_rules);
223
-
224
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
225
- const sourceTable = TEST_TABLE;
226
-
227
- await batch.save({
228
- sourceTable,
229
- tag: SaveOperationTag.INSERT,
230
- after: {
231
- id: 'test1',
232
- description: 'test1'
233
- },
234
- afterReplicaId: rid('test1')
235
- });
236
- await batch.save({
237
- sourceTable,
238
- tag: SaveOperationTag.DELETE,
239
- beforeReplicaId: rid('test1')
240
- });
241
- });
242
-
243
- const checkpoint = result!.flushed_op;
244
-
245
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
246
- const data = batch[0].batch.data.map((d) => {
247
- return {
248
- op: d.op,
249
- object_id: d.object_id,
250
- checksum: d.checksum
251
- };
252
- });
253
-
254
- const c1 = 2871785649;
255
- const c2 = 2872534815;
256
-
257
- expect(data).toEqual([
258
- { op: 'PUT', object_id: 'test1', checksum: c1 },
259
- { op: 'REMOVE', object_id: 'test1', checksum: c2 }
260
- ]);
261
-
262
- const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
263
- expect(checksums).toEqual([
264
- {
265
- bucket: 'global[]',
266
- checksum: (c1 + c2) & 0xffffffff,
267
- count: 2
268
- }
269
- ]);
270
- });
271
-
272
- test('save and load parameters with workspaceId', async () => {
273
- const WORKSPACE_TABLE = makeTestTable('workspace', ['id']);
274
-
275
- const sync_rules_content = testRules(
276
- `
277
- bucket_definitions:
278
- by_workspace:
279
- parameters:
280
- - SELECT id as workspace_id FROM workspace WHERE
281
- workspace."userId" = token_parameters.user_id
282
- data: []
283
- `
284
- );
285
- const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
286
-
287
- const storage = (await factory()).getInstance(sync_rules_content);
288
-
289
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
290
- await batch.save({
291
- sourceTable: WORKSPACE_TABLE,
292
- tag: SaveOperationTag.INSERT,
293
- after: {
294
- id: 'workspace1',
295
- userId: 'u1'
296
- },
297
- afterReplicaId: rid('workspace1')
298
- });
299
- });
300
-
301
- const checkpoint = result!.flushed_op;
302
-
303
- const parameters = new RequestParameters({ sub: 'u1' }, {});
304
-
305
- const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
306
-
307
- const lookups = q1.getLookups(parameters);
308
- expect(lookups).toEqual([['by_workspace', '1', 'u1']]);
309
-
310
- const parameter_sets = await storage.getParameterSets(checkpoint, lookups);
311
- expect(parameter_sets).toEqual([{ workspace_id: 'workspace1' }]);
312
-
313
- const buckets = await sync_rules.queryBucketIds({
314
- getParameterSets(lookups) {
315
- return storage.getParameterSets(checkpoint, lookups);
316
- },
317
- parameters
318
- });
319
- expect(buckets).toEqual(['by_workspace["workspace1"]']);
320
- });
321
-
322
- test('save and load parameters with dynamic global buckets', async () => {
323
- const WORKSPACE_TABLE = makeTestTable('workspace');
324
-
325
- const sync_rules_content = testRules(
326
- `
327
- bucket_definitions:
328
- by_public_workspace:
329
- parameters:
330
- - SELECT id as workspace_id FROM workspace WHERE
331
- workspace.visibility = 'public'
332
- data: []
333
- `
334
- );
335
- const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
336
-
337
- const storage = (await factory()).getInstance(sync_rules_content);
338
-
339
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
340
- await batch.save({
341
- sourceTable: WORKSPACE_TABLE,
342
- tag: SaveOperationTag.INSERT,
343
- after: {
344
- id: 'workspace1',
345
- visibility: 'public'
346
- },
347
- afterReplicaId: rid('workspace1')
348
- });
349
-
350
- await batch.save({
351
- sourceTable: WORKSPACE_TABLE,
352
- tag: SaveOperationTag.INSERT,
353
- after: {
354
- id: 'workspace2',
355
- visibility: 'private'
356
- },
357
- afterReplicaId: rid('workspace2')
358
- });
359
-
360
- await batch.save({
361
- sourceTable: WORKSPACE_TABLE,
362
- tag: SaveOperationTag.INSERT,
363
- after: {
364
- id: 'workspace3',
365
- visibility: 'public'
366
- },
367
- afterReplicaId: rid('workspace3')
368
- });
369
- });
370
-
371
- const checkpoint = result!.flushed_op;
372
-
373
- const parameters = new RequestParameters({ sub: 'unknown' }, {});
374
-
375
- const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
376
-
377
- const lookups = q1.getLookups(parameters);
378
- expect(lookups).toEqual([['by_public_workspace', '1']]);
379
-
380
- const parameter_sets = await storage.getParameterSets(checkpoint, lookups);
381
- parameter_sets.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
382
- expect(parameter_sets).toEqual([{ workspace_id: 'workspace1' }, { workspace_id: 'workspace3' }]);
383
-
384
- const buckets = await sync_rules.queryBucketIds({
385
- getParameterSets(lookups) {
386
- return storage.getParameterSets(checkpoint, lookups);
387
- },
388
- parameters
389
- });
390
- buckets.sort();
391
- expect(buckets).toEqual(['by_public_workspace["workspace1"]', 'by_public_workspace["workspace3"]']);
392
- });
393
-
394
- test('multiple parameter queries', async () => {
395
- const WORKSPACE_TABLE = makeTestTable('workspace');
396
-
397
- const sync_rules_content = testRules(
398
- `
399
- bucket_definitions:
400
- by_workspace:
401
- parameters:
402
- - SELECT id as workspace_id FROM workspace WHERE
403
- workspace.visibility = 'public'
404
- - SELECT id as workspace_id FROM workspace WHERE
405
- workspace.user_id = token_parameters.user_id
406
- data: []
407
- `
408
- );
409
- const sync_rules = sync_rules_content.parsed(PARSE_OPTIONS).sync_rules;
410
-
411
- const storage = (await factory()).getInstance(sync_rules_content);
412
-
413
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
414
- await batch.save({
415
- sourceTable: WORKSPACE_TABLE,
416
- tag: SaveOperationTag.INSERT,
417
- after: {
418
- id: 'workspace1',
419
- visibility: 'public'
420
- },
421
- afterReplicaId: rid('workspace1')
422
- });
423
-
424
- await batch.save({
425
- sourceTable: WORKSPACE_TABLE,
426
- tag: SaveOperationTag.INSERT,
427
- after: {
428
- id: 'workspace2',
429
- visibility: 'private'
430
- },
431
- afterReplicaId: rid('workspace2')
432
- });
433
-
434
- await batch.save({
435
- sourceTable: WORKSPACE_TABLE,
436
- tag: SaveOperationTag.INSERT,
437
- after: {
438
- id: 'workspace3',
439
- user_id: 'u1',
440
- visibility: 'private'
441
- },
442
- afterReplicaId: rid('workspace3')
443
- });
444
-
445
- await batch.save({
446
- sourceTable: WORKSPACE_TABLE,
447
- tag: SaveOperationTag.INSERT,
448
- after: {
449
- id: 'workspace4',
450
- user_id: 'u2',
451
- visibility: 'private'
452
- },
453
- afterReplicaId: rid('workspace4')
454
- });
455
- });
456
-
457
- const checkpoint = result!.flushed_op;
458
-
459
- const parameters = new RequestParameters({ sub: 'u1' }, {});
460
-
461
- // Test intermediate values - could be moved to sync_rules.test.ts
462
- const q1 = sync_rules.bucket_descriptors[0].parameter_queries[0];
463
- const lookups1 = q1.getLookups(parameters);
464
- expect(lookups1).toEqual([['by_workspace', '1']]);
465
-
466
- const parameter_sets1 = await storage.getParameterSets(checkpoint, lookups1);
467
- parameter_sets1.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
468
- expect(parameter_sets1).toEqual([{ workspace_id: 'workspace1' }]);
469
-
470
- const q2 = sync_rules.bucket_descriptors[0].parameter_queries[1];
471
- const lookups2 = q2.getLookups(parameters);
472
- expect(lookups2).toEqual([['by_workspace', '2', 'u1']]);
473
-
474
- const parameter_sets2 = await storage.getParameterSets(checkpoint, lookups2);
475
- parameter_sets2.sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
476
- expect(parameter_sets2).toEqual([{ workspace_id: 'workspace3' }]);
477
-
478
- // Test final values - the important part
479
- const buckets = await sync_rules.queryBucketIds({
480
- getParameterSets(lookups) {
481
- return storage.getParameterSets(checkpoint, lookups);
482
- },
483
- parameters
484
- });
485
- buckets.sort();
486
- expect(buckets).toEqual(['by_workspace["workspace1"]', 'by_workspace["workspace3"]']);
487
- });
488
-
489
- test('changing client ids', async () => {
490
- const sync_rules = testRules(
491
- `
492
- bucket_definitions:
493
- global:
494
- data:
495
- - SELECT client_id as id, description FROM "%"
496
- `
497
- );
498
- const storage = (await factory()).getInstance(sync_rules);
499
-
500
- const sourceTable = TEST_TABLE;
501
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
502
- await batch.save({
503
- sourceTable,
504
- tag: SaveOperationTag.INSERT,
505
- after: {
506
- id: 'test1',
507
- client_id: 'client1a',
508
- description: 'test1a'
509
- },
510
- afterReplicaId: rid('test1')
511
- });
512
- await batch.save({
513
- sourceTable,
514
- tag: SaveOperationTag.UPDATE,
515
- after: {
516
- id: 'test1',
517
- client_id: 'client1b',
518
- description: 'test1b'
519
- },
520
- afterReplicaId: rid('test1')
521
- });
522
-
523
- await batch.save({
524
- sourceTable,
525
- tag: SaveOperationTag.INSERT,
526
- after: {
527
- id: 'test2',
528
- client_id: 'client2',
529
- description: 'test2'
530
- },
531
- afterReplicaId: rid('test2')
532
- });
533
- });
534
- const checkpoint = result!.flushed_op;
535
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
536
- const data = batch[0].batch.data.map((d) => {
537
- return {
538
- op: d.op,
539
- object_id: d.object_id
540
- };
541
- });
542
-
543
- expect(data).toEqual([
544
- { op: 'PUT', object_id: 'client1a' },
545
- { op: 'PUT', object_id: 'client1b' },
546
- { op: 'REMOVE', object_id: 'client1a' },
547
- { op: 'PUT', object_id: 'client2' }
548
- ]);
549
- });
550
-
551
- test('re-apply delete', async () => {
552
- const sync_rules = testRules(
553
- `
554
- bucket_definitions:
555
- global:
556
- data:
557
- - SELECT id, description FROM "%"
558
- `
559
- );
560
- const storage = (await factory()).getInstance(sync_rules);
561
-
562
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
563
- const sourceTable = TEST_TABLE;
564
-
565
- await batch.save({
566
- sourceTable,
567
- tag: SaveOperationTag.INSERT,
568
- after: {
569
- id: 'test1',
570
- description: 'test1'
571
- },
572
- afterReplicaId: rid('test1')
573
- });
574
- });
575
-
576
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
577
- const sourceTable = TEST_TABLE;
578
-
579
- await batch.save({
580
- sourceTable,
581
- tag: SaveOperationTag.DELETE,
582
- beforeReplicaId: rid('test1')
583
- });
584
- });
585
-
586
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
587
- const sourceTable = TEST_TABLE;
588
-
589
- await batch.save({
590
- sourceTable,
591
- tag: SaveOperationTag.DELETE,
592
- beforeReplicaId: rid('test1')
593
- });
594
- });
595
-
596
- const checkpoint = result!.flushed_op;
597
-
598
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
599
- const data = batch[0].batch.data.map((d) => {
600
- return {
601
- op: d.op,
602
- object_id: d.object_id,
603
- checksum: d.checksum
604
- };
605
- });
606
-
607
- const c1 = 2871785649;
608
- const c2 = 2872534815;
609
-
610
- expect(data).toEqual([
611
- { op: 'PUT', object_id: 'test1', checksum: c1 },
612
- { op: 'REMOVE', object_id: 'test1', checksum: c2 }
613
- ]);
614
-
615
- const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
616
- expect(checksums).toEqual([
617
- {
618
- bucket: 'global[]',
619
- checksum: (c1 + c2) & 0xffffffff,
620
- count: 2
621
- }
622
- ]);
623
- });
624
-
625
- test('re-apply update + delete', async () => {
626
- const sync_rules = testRules(
627
- `
628
- bucket_definitions:
629
- global:
630
- data:
631
- - SELECT id, description FROM "%"
632
- `
633
- );
634
- const storage = (await factory()).getInstance(sync_rules);
635
-
636
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
637
- const sourceTable = TEST_TABLE;
638
-
639
- await batch.save({
640
- sourceTable,
641
- tag: SaveOperationTag.INSERT,
642
- after: {
643
- id: 'test1',
644
- description: 'test1'
645
- },
646
- afterReplicaId: rid('test1')
647
- });
648
- });
649
-
650
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
651
- const sourceTable = TEST_TABLE;
652
-
653
- await batch.save({
654
- sourceTable,
655
- tag: SaveOperationTag.UPDATE,
656
- after: {
657
- id: 'test1',
658
- description: undefined
659
- },
660
- afterReplicaId: rid('test1')
661
- });
662
-
663
- await batch.save({
664
- sourceTable,
665
- tag: SaveOperationTag.UPDATE,
666
- after: {
667
- id: 'test1',
668
- description: undefined
669
- },
670
- afterReplicaId: rid('test1')
671
- });
672
-
673
- await batch.save({
674
- sourceTable,
675
- tag: SaveOperationTag.DELETE,
676
- beforeReplicaId: rid('test1')
677
- });
678
- });
679
-
680
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
681
- const sourceTable = TEST_TABLE;
682
-
683
- await batch.save({
684
- sourceTable,
685
- tag: SaveOperationTag.UPDATE,
686
- after: {
687
- id: 'test1',
688
- description: undefined
689
- },
690
- afterReplicaId: rid('test1')
691
- });
692
-
693
- await batch.save({
694
- sourceTable,
695
- tag: SaveOperationTag.UPDATE,
696
- after: {
697
- id: 'test1',
698
- description: undefined
699
- },
700
- afterReplicaId: rid('test1')
701
- });
702
-
703
- await batch.save({
704
- sourceTable,
705
- tag: SaveOperationTag.DELETE,
706
- beforeReplicaId: rid('test1')
707
- });
708
- });
709
-
710
- const checkpoint = result!.flushed_op;
711
-
712
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']])));
713
-
714
- const data = batch[0].batch.data.map((d) => {
715
- return {
716
- op: d.op,
717
- object_id: d.object_id,
718
- checksum: d.checksum
719
- };
720
- });
721
-
722
- const c1 = 2871785649;
723
- const c2 = 2872534815;
724
-
725
- expect(data).toEqual([
726
- { op: 'PUT', object_id: 'test1', checksum: c1 },
727
- { op: 'PUT', object_id: 'test1', checksum: c1 },
728
- { op: 'PUT', object_id: 'test1', checksum: c1 },
729
- { op: 'REMOVE', object_id: 'test1', checksum: c2 }
730
- ]);
731
-
732
- const checksums = [...(await storage.getChecksums(checkpoint, ['global[]'])).values()];
733
- expect(checksums).toEqual([
734
- {
735
- bucket: 'global[]',
736
- checksum: (c1 + c1 + c1 + c2) & 0xffffffff,
737
- count: 4
738
- }
739
- ]);
740
- });
741
-
742
- test('truncate parameters', async () => {
743
- const sync_rules = testRules(
744
- `
745
- bucket_definitions:
746
- mybucket:
747
- parameters:
748
- - SELECT group_id FROM test WHERE id1 = token_parameters.user_id OR id2 = token_parameters.user_id
749
- data: []
750
- `
751
- );
752
-
753
- const storage = (await factory()).getInstance(sync_rules);
754
-
755
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
756
- await batch.save({
757
- sourceTable: TEST_TABLE,
758
- tag: SaveOperationTag.INSERT,
759
- after: {
760
- id: 't2',
761
- id1: 'user3',
762
- id2: 'user4',
763
- group_id: 'group2a'
764
- },
765
- afterReplicaId: rid('t2')
766
- });
767
-
768
- await batch.truncate([TEST_TABLE]);
769
- });
770
-
771
- const { checkpoint } = await storage.getCheckpoint();
772
-
773
- const parameters = await storage.getParameterSets(checkpoint, [['mybucket', '1', 'user1']]);
774
- expect(parameters).toEqual([]);
775
- });
776
-
777
- test('batch with overlapping replica ids', async () => {
778
- // This test checks that we get the correct output when processing rows with:
779
- // 1. changing replica ids
780
- // 2. overlapping with replica ids of other rows in the same transaction (at different times)
781
- // If operations are not processing in input order, this breaks easily.
782
- // It can break at two places:
783
- // 1. Not getting the correct "current_data" state for each operation.
784
- // 2. Output order not being correct.
785
-
786
- const sync_rules = testRules(
787
- `
788
- bucket_definitions:
789
- global:
790
- data:
791
- - SELECT id, description FROM "test"
792
- `
793
- );
794
- const storage = (await factory()).getInstance(sync_rules);
795
-
796
- // Pre-setup
797
- const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
798
- const sourceTable = TEST_TABLE;
799
-
800
- await batch.save({
801
- sourceTable,
802
- tag: SaveOperationTag.INSERT,
803
- after: {
804
- id: 'test1',
805
- description: 'test1a'
806
- },
807
- afterReplicaId: rid('test1')
808
- });
809
-
810
- await batch.save({
811
- sourceTable,
812
- tag: SaveOperationTag.INSERT,
813
- after: {
814
- id: 'test2',
815
- description: 'test2a'
816
- },
817
- afterReplicaId: rid('test2')
818
- });
819
- });
820
-
821
- const checkpoint1 = result1?.flushed_op ?? '0';
822
-
823
- // Test batch
824
- const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
825
- const sourceTable = TEST_TABLE;
826
- // b
827
- await batch.save({
828
- sourceTable,
829
- tag: SaveOperationTag.INSERT,
830
- after: {
831
- id: 'test1',
832
- description: 'test1b'
833
- },
834
- afterReplicaId: rid('test1')
835
- });
836
-
837
- await batch.save({
838
- sourceTable,
839
- tag: SaveOperationTag.UPDATE,
840
- before: {
841
- id: 'test1'
842
- },
843
- beforeReplicaId: rid('test1'),
844
- after: {
845
- id: 'test2',
846
- description: 'test2b'
847
- },
848
- afterReplicaId: rid('test2')
849
- });
850
-
851
- await batch.save({
852
- sourceTable,
853
- tag: SaveOperationTag.UPDATE,
854
- before: {
855
- id: 'test2'
856
- },
857
- beforeReplicaId: rid('test2'),
858
- after: {
859
- id: 'test3',
860
- description: 'test3b'
861
- },
862
-
863
- afterReplicaId: rid('test3')
864
- });
865
-
866
- // c
867
- await batch.save({
868
- sourceTable,
869
- tag: SaveOperationTag.UPDATE,
870
- after: {
871
- id: 'test2',
872
- description: 'test2c'
873
- },
874
- afterReplicaId: rid('test2')
875
- });
876
-
877
- // d
878
- await batch.save({
879
- sourceTable,
880
- tag: SaveOperationTag.INSERT,
881
- after: {
882
- id: 'test4',
883
- description: 'test4d'
884
- },
885
- afterReplicaId: rid('test4')
886
- });
887
-
888
- await batch.save({
889
- sourceTable,
890
- tag: SaveOperationTag.UPDATE,
891
- before: {
892
- id: 'test4'
893
- },
894
- beforeReplicaId: rid('test4'),
895
- after: {
896
- id: 'test5',
897
- description: 'test5d'
898
- },
899
- afterReplicaId: rid('test5')
900
- });
901
- });
902
-
903
- const checkpoint2 = result2!.flushed_op;
904
-
905
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint2, new Map([['global[]', checkpoint1]])));
906
- const data = batch[0].batch.data.map((d) => {
907
- return {
908
- op: d.op,
909
- object_id: d.object_id,
910
- data: d.data
911
- };
912
- });
913
-
914
- // Operations must be in this order
915
- expect(data).toEqual([
916
- // b
917
- { op: 'PUT', object_id: 'test1', data: JSON.stringify({ id: 'test1', description: 'test1b' }) },
918
- { op: 'REMOVE', object_id: 'test1', data: null },
919
- { op: 'PUT', object_id: 'test2', data: JSON.stringify({ id: 'test2', description: 'test2b' }) },
920
- { op: 'REMOVE', object_id: 'test2', data: null },
921
- { op: 'PUT', object_id: 'test3', data: JSON.stringify({ id: 'test3', description: 'test3b' }) },
922
-
923
- // c
924
- { op: 'PUT', object_id: 'test2', data: JSON.stringify({ id: 'test2', description: 'test2c' }) },
925
-
926
- // d
927
- { op: 'PUT', object_id: 'test4', data: JSON.stringify({ id: 'test4', description: 'test4d' }) },
928
- { op: 'REMOVE', object_id: 'test4', data: null },
929
- { op: 'PUT', object_id: 'test5', data: JSON.stringify({ id: 'test5', description: 'test5d' }) }
930
- ]);
931
- });
932
-
933
- test('changed data with replica identity full', async () => {
934
- const sync_rules = testRules(
935
- `
936
- bucket_definitions:
937
- global:
938
- data:
939
- - SELECT id, description FROM "test"
940
- `
941
- );
942
- function rid2(id: string, description: string) {
943
- return getUuidReplicaIdentityBson({ id, description }, [
944
- { name: 'id', type: 'VARCHAR', typeId: 25 },
945
- { name: 'description', type: 'VARCHAR', typeId: 25 }
946
- ]);
947
- }
948
- const storage = (await factory()).getInstance(sync_rules);
949
-
950
- const sourceTable = makeTestTable('test', ['id', 'description']);
951
-
952
- // Pre-setup
953
- const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
954
- await batch.save({
955
- sourceTable,
956
- tag: SaveOperationTag.INSERT,
957
- after: {
958
- id: 'test1',
959
- description: 'test1a'
960
- },
961
- afterReplicaId: rid2('test1', 'test1a')
962
- });
963
- });
964
-
965
- const checkpoint1 = result1?.flushed_op ?? '0';
966
-
967
- const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
968
- // Unchanged, but has a before id
969
- await batch.save({
970
- sourceTable,
971
- tag: SaveOperationTag.UPDATE,
972
- before: {
973
- id: 'test1',
974
- description: 'test1a'
975
- },
976
- beforeReplicaId: rid2('test1', 'test1a'),
977
- after: {
978
- id: 'test1',
979
- description: 'test1b'
980
- },
981
- afterReplicaId: rid2('test1', 'test1b')
982
- });
983
- });
984
-
985
- const result3 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
986
- // Delete
987
- await batch.save({
988
- sourceTable,
989
- tag: SaveOperationTag.DELETE,
990
- before: {
991
- id: 'test1',
992
- description: 'test1b'
993
- },
994
- beforeReplicaId: rid2('test1', 'test1b'),
995
- after: undefined
996
- });
997
- });
998
-
999
- const checkpoint3 = result3!.flushed_op;
1000
-
1001
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint3, new Map([['global[]', checkpoint1]])));
1002
- const data = batch[0].batch.data.map((d) => {
1003
- return {
1004
- op: d.op,
1005
- object_id: d.object_id,
1006
- data: d.data,
1007
- subkey: d.subkey
1008
- };
1009
- });
1010
-
1011
- // Operations must be in this order
1012
- expect(data).toEqual([
1013
- // 2
1014
- // The REMOVE is expected because the subkey changes
1015
- {
1016
- op: 'REMOVE',
1017
- object_id: 'test1',
1018
- data: null,
1019
- subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
1020
- },
1021
- {
1022
- op: 'PUT',
1023
- object_id: 'test1',
1024
- data: JSON.stringify({ id: 'test1', description: 'test1b' }),
1025
- subkey: '6544e3899293153fa7b38331/500e9b68-a2fd-51ff-9c00-313e2fb9f562'
1026
- },
1027
- // 3
1028
- {
1029
- op: 'REMOVE',
1030
- object_id: 'test1',
1031
- data: null,
1032
- subkey: '6544e3899293153fa7b38331/500e9b68-a2fd-51ff-9c00-313e2fb9f562'
1033
- }
1034
- ]);
1035
- });
1036
-
1037
- test('unchanged data with replica identity full', async () => {
1038
- const sync_rules = testRules(
1039
- `
1040
- bucket_definitions:
1041
- global:
1042
- data:
1043
- - SELECT id, description FROM "test"
1044
- `
1045
- );
1046
- function rid2(id: string, description: string) {
1047
- return getUuidReplicaIdentityBson({ id, description }, [
1048
- { name: 'id', type: 'VARCHAR', typeId: 25 },
1049
- { name: 'description', type: 'VARCHAR', typeId: 25 }
1050
- ]);
1051
- }
1052
-
1053
- const storage = (await factory()).getInstance(sync_rules);
1054
-
1055
- const sourceTable = makeTestTable('test', ['id', 'description']);
1056
-
1057
- // Pre-setup
1058
- const result1 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1059
- await batch.save({
1060
- sourceTable,
1061
- tag: SaveOperationTag.INSERT,
1062
- after: {
1063
- id: 'test1',
1064
- description: 'test1a'
1065
- },
1066
- afterReplicaId: rid2('test1', 'test1a')
1067
- });
1068
- });
1069
-
1070
- const checkpoint1 = result1?.flushed_op ?? '0';
1071
-
1072
- const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1073
- // Unchanged, but has a before id
1074
- await batch.save({
1075
- sourceTable,
1076
- tag: SaveOperationTag.UPDATE,
1077
- before: {
1078
- id: 'test1',
1079
- description: 'test1a'
1080
- },
1081
- beforeReplicaId: rid2('test1', 'test1a'),
1082
- after: {
1083
- id: 'test1',
1084
- description: 'test1a'
1085
- },
1086
- afterReplicaId: rid2('test1', 'test1a')
1087
- });
1088
- });
1089
-
1090
- const result3 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1091
- // Delete
1092
- await batch.save({
1093
- sourceTable,
1094
- tag: SaveOperationTag.DELETE,
1095
- before: {
1096
- id: 'test1',
1097
- description: 'test1a'
1098
- },
1099
- beforeReplicaId: rid2('test1', 'test1a'),
1100
- after: undefined
1101
- });
1102
- });
1103
-
1104
- const checkpoint3 = result3!.flushed_op;
1105
-
1106
- const batch = await fromAsync(storage.getBucketDataBatch(checkpoint3, new Map([['global[]', checkpoint1]])));
1107
- const data = batch[0].batch.data.map((d) => {
1108
- return {
1109
- op: d.op,
1110
- object_id: d.object_id,
1111
- data: d.data,
1112
- subkey: d.subkey
1113
- };
1114
- });
1115
-
1116
- // Operations must be in this order
1117
- expect(data).toEqual([
1118
- // 2
1119
- {
1120
- op: 'PUT',
1121
- object_id: 'test1',
1122
- data: JSON.stringify({ id: 'test1', description: 'test1a' }),
1123
- subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
1124
- },
1125
- // 3
1126
- {
1127
- op: 'REMOVE',
1128
- object_id: 'test1',
1129
- data: null,
1130
- subkey: '6544e3899293153fa7b38331/740ba9f2-8b0f-53e3-bb17-5f38a9616f0e'
1131
- }
1132
- ]);
1133
- });
1134
-
1135
- test('large batch', async () => {
1136
- // Test syncing a batch of data that is small in count,
1137
- // but large enough in size to be split over multiple returned batches.
1138
- // The specific batch splits is an implementation detail of the storage driver,
1139
- // and the test will have to updated when other implementations are added.
1140
- const sync_rules = testRules(
1141
- `
1142
- bucket_definitions:
1143
- global:
1144
- data:
1145
- - SELECT id, description FROM "%"
1146
- `
1147
- );
1148
- const storage = (await factory()).getInstance(sync_rules);
1149
-
1150
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1151
- const sourceTable = TEST_TABLE;
1152
-
1153
- const largeDescription = '0123456789'.repeat(12_000_00);
1154
-
1155
- await batch.save({
1156
- sourceTable,
1157
- tag: SaveOperationTag.INSERT,
1158
- after: {
1159
- id: 'test1',
1160
- description: 'test1'
1161
- },
1162
- afterReplicaId: rid('test1')
1163
- });
1164
-
1165
- await batch.save({
1166
- sourceTable,
1167
- tag: SaveOperationTag.INSERT,
1168
- after: {
1169
- id: 'large1',
1170
- description: largeDescription
1171
- },
1172
- afterReplicaId: rid('large1')
1173
- });
1174
-
1175
- // Large enough to split the returned batch
1176
- await batch.save({
1177
- sourceTable,
1178
- tag: SaveOperationTag.INSERT,
1179
- after: {
1180
- id: 'large2',
1181
- description: largeDescription
1182
- },
1183
- afterReplicaId: rid('large2')
1184
- });
1185
-
1186
- await batch.save({
1187
- sourceTable,
1188
- tag: SaveOperationTag.INSERT,
1189
- after: {
1190
- id: 'test3',
1191
- description: 'test3'
1192
- },
1193
- afterReplicaId: rid('test3')
1194
- });
1195
- });
1196
-
1197
- const checkpoint = result!.flushed_op;
1198
-
1199
- const options: BucketDataBatchOptions = {
1200
- chunkLimitBytes: 16 * 1024 * 1024
1201
- };
1202
-
1203
- const batch1 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), options));
1204
- expect(getBatchData(batch1)).toEqual([
1205
- { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1206
- { op_id: '2', op: 'PUT', object_id: 'large1', checksum: 454746904 }
1207
- ]);
1208
- expect(getBatchMeta(batch1)).toEqual({
1209
- after: '0',
1210
- has_more: true,
1211
- next_after: '2'
1212
- });
1213
-
1214
- const batch2 = await fromAsync(
1215
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1[0].batch.next_after]]), options)
1216
- );
1217
- expect(getBatchData(batch2)).toEqual([
1218
- { op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1795508474 },
1219
- { op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }
1220
- ]);
1221
- expect(getBatchMeta(batch2)).toEqual({
1222
- after: '2',
1223
- has_more: false,
1224
- next_after: '4'
1225
- });
1226
-
1227
- const batch3 = await fromAsync(
1228
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2[0].batch.next_after]]), options)
1229
- );
1230
- expect(getBatchData(batch3)).toEqual([]);
1231
- expect(getBatchMeta(batch3)).toEqual(null);
1232
- });
1233
-
1234
- test('large batch (2)', async () => {
1235
- // Test syncing a batch of data that is small in count,
1236
- // but large enough in size to be split over multiple returned chunks.
1237
- // Similar to the above test, but splits over 1MB chunks.
1238
- const sync_rules = testRules(
1239
- `
1240
- bucket_definitions:
1241
- global:
1242
- data:
1243
- - SELECT id, description FROM "%"
1244
- `
1245
- );
1246
- const storage = (await factory()).getInstance(sync_rules);
1247
-
1248
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1249
- const sourceTable = TEST_TABLE;
1250
-
1251
- const largeDescription = '0123456789'.repeat(2_000_00);
1252
-
1253
- await batch.save({
1254
- sourceTable,
1255
- tag: SaveOperationTag.INSERT,
1256
- after: {
1257
- id: 'test1',
1258
- description: 'test1'
1259
- },
1260
- afterReplicaId: rid('test1')
1261
- });
1262
-
1263
- await batch.save({
1264
- sourceTable,
1265
- tag: SaveOperationTag.INSERT,
1266
- after: {
1267
- id: 'large1',
1268
- description: largeDescription
1269
- },
1270
- afterReplicaId: rid('large1')
1271
- });
1272
-
1273
- // Large enough to split the returned batch
1274
- await batch.save({
1275
- sourceTable,
1276
- tag: SaveOperationTag.INSERT,
1277
- after: {
1278
- id: 'large2',
1279
- description: largeDescription
1280
- },
1281
- afterReplicaId: rid('large2')
1282
- });
1283
-
1284
- await batch.save({
1285
- sourceTable,
1286
- tag: SaveOperationTag.INSERT,
1287
- after: {
1288
- id: 'test3',
1289
- description: 'test3'
1290
- },
1291
- afterReplicaId: rid('test3')
1292
- });
1293
- });
1294
-
1295
- const checkpoint = result!.flushed_op;
1296
-
1297
- const options: BucketDataBatchOptions = {};
1298
-
1299
- const batch1 = await fromAsync(storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), options));
1300
- expect(getBatchData(batch1)).toEqual([
1301
- { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1302
- { op_id: '2', op: 'PUT', object_id: 'large1', checksum: 1178768505 }
1303
- ]);
1304
- expect(getBatchMeta(batch1)).toEqual({
1305
- after: '0',
1306
- has_more: true,
1307
- next_after: '2'
1308
- });
1309
-
1310
- const batch2 = await fromAsync(
1311
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1[0].batch.next_after]]), options)
1312
- );
1313
- expect(getBatchData(batch2)).toEqual([{ op_id: '3', op: 'PUT', object_id: 'large2', checksum: 1607205872 }]);
1314
- expect(getBatchMeta(batch2)).toEqual({
1315
- after: '2',
1316
- has_more: true,
1317
- next_after: '3'
1318
- });
1319
-
1320
- const batch3 = await fromAsync(
1321
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2[0].batch.next_after]]), options)
1322
- );
1323
- expect(getBatchData(batch3)).toEqual([{ op_id: '4', op: 'PUT', object_id: 'test3', checksum: 1359888332 }]);
1324
- expect(getBatchMeta(batch3)).toEqual({
1325
- after: '3',
1326
- has_more: false,
1327
- next_after: '4'
1328
- });
1329
- });
1330
-
1331
- test('long batch', async () => {
1332
- // Test syncing a batch of data that is limited by count.
1333
- const sync_rules = testRules(
1334
- `
1335
- bucket_definitions:
1336
- global:
1337
- data:
1338
- - SELECT id, description FROM "%"
1339
- `
1340
- );
1341
- const storage = (await factory()).getInstance(sync_rules);
1342
-
1343
- const result = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1344
- const sourceTable = TEST_TABLE;
1345
-
1346
- for (let i = 1; i <= 6; i++) {
1347
- await batch.save({
1348
- sourceTable,
1349
- tag: SaveOperationTag.INSERT,
1350
- after: {
1351
- id: `test${i}`,
1352
- description: `test${i}`
1353
- },
1354
- afterReplicaId: `test${i}`
1355
- });
1356
- }
1357
- });
1358
-
1359
- const checkpoint = result!.flushed_op;
1360
-
1361
- const batch1 = await oneFromAsync(
1362
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', '0']]), { limit: 4 })
1363
- );
1364
-
1365
- expect(getBatchData(batch1)).toEqual([
1366
- { op_id: '1', op: 'PUT', object_id: 'test1', checksum: 2871785649 },
1367
- { op_id: '2', op: 'PUT', object_id: 'test2', checksum: 730027011 },
1368
- { op_id: '3', op: 'PUT', object_id: 'test3', checksum: 1359888332 },
1369
- { op_id: '4', op: 'PUT', object_id: 'test4', checksum: 2049153252 }
1370
- ]);
1371
-
1372
- expect(getBatchMeta(batch1)).toEqual({
1373
- after: '0',
1374
- has_more: true,
1375
- next_after: '4'
1376
- });
1377
-
1378
- const batch2 = await oneFromAsync(
1379
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch1.batch.next_after]]), {
1380
- limit: 4
1381
- })
1382
- );
1383
- expect(getBatchData(batch2)).toEqual([
1384
- { op_id: '5', op: 'PUT', object_id: 'test5', checksum: 3686902721 },
1385
- { op_id: '6', op: 'PUT', object_id: 'test6', checksum: 1974820016 }
1386
- ]);
1387
-
1388
- expect(getBatchMeta(batch2)).toEqual({
1389
- after: '4',
1390
- has_more: false,
1391
- next_after: '6'
1392
- });
1393
-
1394
- const batch3 = await fromAsync(
1395
- storage.getBucketDataBatch(checkpoint, new Map([['global[]', batch2.batch.next_after]]), {
1396
- limit: 4
1397
- })
1398
- );
1399
- expect(getBatchData(batch3)).toEqual([]);
1400
-
1401
- expect(getBatchMeta(batch3)).toEqual(null);
1402
- });
1403
-
1404
- test('batch should be disposed automatically', async () => {
1405
- const sync_rules = testRules(`
1406
- bucket_definitions:
1407
- global:
1408
- data: []
1409
- `);
1410
-
1411
- const storage = (await factory()).getInstance(sync_rules);
1412
-
1413
- let isDisposed = false;
1414
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1415
- batch.registerListener({
1416
- disposed: () => {
1417
- isDisposed = true;
1418
- }
1419
- });
1420
- });
1421
- expect(isDisposed).true;
1422
-
1423
- isDisposed = false;
1424
- let errorCaught = false;
1425
- try {
1426
- await storage.startBatch(BATCH_OPTIONS, async (batch) => {
1427
- batch.registerListener({
1428
- disposed: () => {
1429
- isDisposed = true;
1430
- }
1431
- });
1432
- throw new Error(`Testing exceptions`);
1433
- });
1434
- } catch (ex) {
1435
- errorCaught = true;
1436
- expect(ex.message.includes('Testing')).true;
1437
- }
1438
- expect(errorCaught).true;
1439
- expect(isDisposed).true;
1440
- });
1441
-
1442
- test('empty storage metrics', async () => {
1443
- const f = await factory({ dropAll: true });
1444
-
1445
- const metrics = await f.getStorageMetrics();
1446
- expect(metrics).toEqual({
1447
- operations_size_bytes: 0,
1448
- parameters_size_bytes: 0,
1449
- replication_size_bytes: 0
1450
- });
1451
-
1452
- const r = await f.configureSyncRules('bucket_definitions: {}');
1453
- const storage = f.getInstance(r.persisted_sync_rules!);
1454
- await storage.autoActivate();
1455
-
1456
- const metrics2 = await f.getStorageMetrics();
1457
- expect(metrics2).toEqual({
1458
- operations_size_bytes: 0,
1459
- parameters_size_bytes: 0,
1460
- replication_size_bytes: 0
1461
- });
1462
- });
1463
-
1464
- test('invalidate cached parsed sync rules', async () => {
1465
- const sync_rules_content = testRules(
1466
- `
1467
- bucket_definitions:
1468
- by_workspace:
1469
- parameters:
1470
- - SELECT id as workspace_id FROM workspace WHERE
1471
- workspace."userId" = token_parameters.user_id
1472
- data: []
1473
- `
1474
- );
1475
-
1476
- const bucketStorageFactory = await factory();
1477
- const syncBucketStorage = bucketStorageFactory.getInstance(sync_rules_content);
1478
-
1479
- const parsedSchema1 = syncBucketStorage.getParsedSyncRules({
1480
- defaultSchema: 'public'
1481
- });
1482
-
1483
- const parsedSchema2 = syncBucketStorage.getParsedSyncRules({
1484
- defaultSchema: 'public'
1485
- });
1486
-
1487
- // These should be cached, this will be the same instance
1488
- expect(parsedSchema2).equals(parsedSchema1);
1489
- expect(parsedSchema1.getSourceTables()[0].schema).equals('public');
1490
-
1491
- const parsedSchema3 = syncBucketStorage.getParsedSyncRules({
1492
- defaultSchema: 'databasename'
1493
- });
1494
-
1495
- // The cache should not be used
1496
- expect(parsedSchema3).not.equals(parsedSchema2);
1497
- expect(parsedSchema3.getSourceTables()[0].schema).equals('databasename');
1498
- });
1499
- }