MCPcopy Create free account
hub / github.com/TanStack/db / applyMutations

Function applyMutations

packages/offline-transactions/tests/harness.ts:153–204  ·  view source on GitHub ↗
(mutations: Array<PendingMutation<TestItem>>)

Source from the content-addressed store, hash-verified

151 const serverState = new Map<string, TestItem>()
152
153 const applyMutations = (mutations: Array<PendingMutation<TestItem>>) => {
154 controller.begin()
155
156 for (const mutation of mutations) {
157 switch (mutation.type) {
158 case `insert`: {
159 const value = mutation.modified
160 serverState.set(value.id, value)
161 controller.write({ type: `insert`, value })
162 break
163 }
164 case `update`: {
165 const value = mutation.modified
166 serverState.set(value.id, value)
167 controller.write({ type: `update`, value })
168 break
169 }
170 case `delete`: {
171 const original = mutation.original as TestItem | undefined
172 const fallbackIdFromKey =
173 (typeof mutation.key === `string` ? mutation.key : undefined) ??
174 mutation.globalKey.split(`:`).pop()
175 const id = original?.id ?? fallbackIdFromKey
176
177 if (!id) {
178 throw new Error(
179 `Unable to determine id for delete mutation ${mutation.globalKey}`,
180 )
181 }
182
183 const previousValue = serverState.get(id)
184 serverState.delete(id)
185
186 const emittedValue: TestItem = previousValue ?? {
187 id,
188 value: original?.value ?? ``,
189 completed: original?.completed ?? false,
190 updatedAt: original?.updatedAt ?? new Date(),
191 }
192
193 controller.write({
194 type: `delete`,
195 value: emittedValue,
196 })
197 break
198 }
199 }
200 }
201
202 controller.commit()
203 controller.markReady()
204 }
205
206 const defaultMutation: TestMutationFn = (params) => {
207 const mutations = params.transaction.mutations as Array<

Callers 1

defaultMutationFunction · 0.85

Calls 5

markReadyMethod · 0.80
setMethod · 0.45
getMethod · 0.45
deleteMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected