Describe the bug
TLDR: after the onInsert runs, optimistic state passed into collection.insert isn't cleared out
I have a simple onInsert handler
onInsert: async ({ transaction }) => {
return Promise.all(
transaction.mutations.map(async (mutation) => {
const { modified } = mutation;
return postApi({
endpoint: '/locations',
body: {
name: modified.name ?? '',
},
});
})
);
},
Which is run when I do the following
const tx = locationCollection.insert({
created: String(Date.now()),
deleted: null,
id: String(Math.random() * 10000),
name: value.name.trim(),
});
await tx.isPersisted.promise;
After onInsert runs, I have both of these entities listed in the collection
Here is the query that I am using to display that collection data
const locationsQuery = useLiveQuery((q) =>
q
.from({ location: locationsCollection })
.where(({ location }) => isNull(location.deleted))
);
To Reproduce
Here's the order of events locally
- Load the query
- Perform an action to add
location (the entity I am working with)
onInsert runs
- optimistic + new synced entity remain in the collection (or at least the return value from the
useLiveQuery)
Expected behavior
"The optimistic state is held until the handler resolves, at which point the data is persisted to the server and synced back" I expect the server data to be synced back, replacing the optimistic data, not appended adjacent to it
Desktop (please complete the following information):
- Device: Apple m3 pro
- OS: Tahoe 26.3.1
- Browser: Brave
- Version:
- Brave 1.87.191
- Chromium: 145.0.7632.120
Smartphone (please complete the following information):
n/a
Additional context
This is my getKey function (if relevant)
getKey: (location) => location.id,
Describe the bug
TLDR: after the
onInsertruns, optimistic state passed intocollection.insertisn't cleared outI have a simple
onInserthandlerWhich is run when I do the following
After
onInsertruns, I have both of these entities listed in the collectionHere is the query that I am using to display that collection data
To Reproduce
Here's the order of events locally
location(the entity I am working with)onInsertrunsuseLiveQuery)Expected behavior
"The optimistic state is held until the handler resolves, at which point the data is persisted to the server and synced back" I expect the server data to be synced back, replacing the optimistic data, not appended adjacent to it
Desktop (please complete the following information):
Smartphone (please complete the following information):
n/a
Additional context
This is my
getKeyfunction (if relevant)getKey: (location) => location.id,