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

Function acceptMutations

packages/db/src/local-storage.ts:550–608  ·  view source on GitHub ↗
(transaction: {
    mutations: Array<PendingMutation<Record<string, unknown>>>
  })

Source from the content-addressed store, hash-verified

548 * Accepts mutations from a transaction that belong to this collection and persists them to storage
549 */
550 const acceptMutations = (transaction: {
551 mutations: Array<PendingMutation<Record<string, unknown>>>
552 }) => {
553 // Filter mutations that belong to this collection
554 // Use collection ID for filtering if collection reference isn't available yet
555 const collectionMutations = transaction.mutations.filter((m) => {
556 // Try to match by collection reference first
557 if (sync.collection && m.collection === sync.collection) {
558 return true
559 }
560 // Fall back to matching by collection ID
561 return m.collection.id === collectionId
562 })
563
564 if (collectionMutations.length === 0) {
565 return
566 }
567
568 // Validate all mutations can be serialized before modifying storage
569 for (const mutation of collectionMutations) {
570 switch (mutation.type) {
571 case `insert`:
572 case `update`:
573 validateJsonSerializable(parser, mutation.modified, mutation.type)
574 break
575 case `delete`:
576 validateJsonSerializable(parser, mutation.original, mutation.type)
577 break
578 }
579 }
580
581 // Use lastKnownData (in-memory cache) instead of reading from storage
582 // Apply each mutation
583 for (const mutation of collectionMutations) {
584 // Use the engine's pre-computed key to avoid key derivation issues
585 switch (mutation.type) {
586 case `insert`:
587 case `update`: {
588 const storedItem: StoredItem<Record<string, unknown>> = {
589 versionKey: generateUuid(),
590 data: mutation.modified,
591 }
592 lastKnownData.set(mutation.key, storedItem)
593 break
594 }
595 case `delete`: {
596 lastKnownData.delete(mutation.key)
597 break
598 }
599 }
600 }
601
602 // Save to storage
603 saveToStorage(lastKnownData)
604
605 // Confirm the mutations in the collection to move them from optimistic to synced state
606 // This writes them through the sync interface to make them "synced" instead of "optimistic"
607 sync.confirmOperationsSync(collectionMutations)

Callers

nothing calls this directly

Calls 6

validateJsonSerializableFunction · 0.85
generateUuidFunction · 0.85
saveToStorageFunction · 0.85
filterMethod · 0.80
setMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected