Skip to content
Open
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/fix-offline-mutations-changes-field.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/offline-transactions': patch
---

Fix mutation.changes field being lost during offline transaction serialization. Previously, the changes field was not included in serialized mutations, causing it to be empty ({}) after app restart. This led to sync functions receiving incomplete data when using mutation.changes for partial updates.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import type {
import type { Collection, PendingMutation } from '@tanstack/db'

export class TransactionSerializer {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private collections: Record<string, Collection<any, any, any, any, any>>
private collectionIdToKey: Map<string, string>

// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(
collections: Record<string, Collection<any, any, any, any, any>>,
) {
Expand All @@ -32,7 +30,7 @@ export class TransactionSerializer {
),
}
// Convert the whole object to JSON, handling dates
return JSON.stringify(serialized, (key, value) => {
return JSON.stringify(serialized, (_key, value) => {
if (value instanceof Date) {
return value.toISOString()
}
Expand All @@ -43,7 +41,7 @@ export class TransactionSerializer {
deserialize(data: string): OfflineTransaction {
const parsed: SerializedOfflineTransaction = JSON.parse(
data,
(key, value) => {
(_key, value) => {
// Parse ISO date strings back to Date objects
if (
typeof value === `string` &&
Expand Down Expand Up @@ -76,6 +74,7 @@ export class TransactionSerializer {
type: mutation.type,
modified: this.serializeValue(mutation.modified),
original: this.serializeValue(mutation.original),
changes: this.serializeValue(mutation.changes),
collectionId: registryKey, // Store registry key instead of collection.id
}
}
Expand All @@ -93,11 +92,12 @@ export class TransactionSerializer {
type: data.type as any,
modified: this.deserializeValue(data.modified),
original: this.deserializeValue(data.original),
changes:
data.changes !== undefined ? this.deserializeValue(data.changes) : {},
collection,
// These fields would need to be reconstructed by the executor
mutationId: ``, // Will be regenerated
key: null, // Will be extracted from the data
changes: {}, // Will be recalculated
metadata: undefined,
syncMetadata: {},
optimistic: true,
Expand All @@ -118,7 +118,7 @@ export class TransactionSerializer {
if (typeof value === `object`) {
const result: any = Array.isArray(value) ? [] : {}
for (const key in value) {
if (value.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
result[key] = this.serializeValue(value[key])
}
}
Expand All @@ -140,7 +140,7 @@ export class TransactionSerializer {
if (typeof value === `object`) {
const result: any = Array.isArray(value) ? [] : {}
for (const key in value) {
if (value.hasOwnProperty(key)) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
result[key] = this.deserializeValue(value[key])
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/offline-transactions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SerializedMutation {
type: string
modified: any
original: any
changes: any
collectionId: string
}

Expand Down Expand Up @@ -88,7 +89,6 @@ export interface StorageDiagnostic {
}

export interface OfflineConfig {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
collections: Record<string, Collection<any, any, any, any, any>>
mutationFns: Record<string, OfflineMutationFn>
storage?: StorageAdapter
Expand Down
Loading