Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-hotels-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/service-core': patch
---

Fix bucket parameters grouping.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class MongoSyncBucketStorage
},
{
$group: {
_id: '$key',
_id: { key: '$key', lookup: '$lookup' },
bucket_parameters: {
$first: '$bucket_parameters'
}
Expand Down
70 changes: 70 additions & 0 deletions packages/service-core/test/src/data_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,76 @@ bucket_definitions:
]);
});

test('it should use the latest version after updates', async () => {
const sync_rules = testRules(
`
bucket_definitions:
mybucket:
parameters:
- SELECT id AS todo_id
FROM todos
WHERE list_id IN token_parameters.list_id
data: []
`
);

const storage = (await factory()).getInstance(sync_rules);

const table = makeTestTable('todos', ['id', 'list_id']);

await storage.startBatch(BATCH_OPTIONS, async (batch) => {
// Create two todos which initially belong to different lists
await batch.save({
sourceTable: table,
tag: SaveOperationTag.INSERT,
after: {
id: 'todo1',
list_id: 'list1'
},
afterReplicaId: rid('todo1')
});
await batch.save({
sourceTable: table,
tag: SaveOperationTag.INSERT,
after: {
id: 'todo2',
list_id: 'list2'
},
afterReplicaId: rid('todo2')
});
});

const result2 = await storage.startBatch(BATCH_OPTIONS, async (batch) => {
// Update the second todo item to now belong to list 1
await batch.save({
sourceTable: table,
tag: SaveOperationTag.UPDATE,
after: {
id: 'todo2',
list_id: 'list1'
},
afterReplicaId: rid('todo2')
});
});

// We specifically request the todo_ids for both lists.
// There removal operation for the association of `list2`::`todo2` should not interfere with the new
// association of `list1`::`todo2`
const parameters = await storage.getParameterSets(BigInt(result2!.flushed_op).toString(), [
['mybucket', '1', 'list1'],
['mybucket', '1', 'list2']
]);

expect(parameters.sort((a, b) => (a.todo_id as string).localeCompare(b.todo_id as string))).toEqual([
{
todo_id: 'todo1'
},
{
todo_id: 'todo2'
}
]);
});

test('save and load parameters with different number types', async () => {
const sync_rules = testRules(
`
Expand Down
Loading