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

Function normalizeOperations

packages/query-db-collection/src/manual-sync.ts:64–104  ·  view source on GitHub ↗
(
  ops:
    | SyncOperation<TRow, TKey, TInsertInput>
    | Array<SyncOperation<TRow, TKey, TInsertInput>>,
  ctx: SyncContext<TRow, TKey>,
)

Source from the content-addressed store, hash-verified

62
63// Normalize operations into a consistent format
64function normalizeOperations<
65 TRow extends object,
66 TKey extends string | number = string | number,
67 TInsertInput extends object = TRow,
68>(
69 ops:
70 | SyncOperation<TRow, TKey, TInsertInput>
71 | Array<SyncOperation<TRow, TKey, TInsertInput>>,
72 ctx: SyncContext<TRow, TKey>,
73): Array<NormalizedOperation<TRow, TKey>> {
74 const operations = Array.isArray(ops) ? ops : [ops]
75 const normalized: Array<NormalizedOperation<TRow, TKey>> = []
76
77 for (const op of operations) {
78 if (op.type === `delete`) {
79 const keys = Array.isArray(op.key) ? op.key : [op.key]
80 for (const key of keys) {
81 normalized.push({ type: `delete`, key })
82 }
83 } else {
84 const items = Array.isArray(op.data) ? op.data : [op.data]
85 for (const item of items) {
86 let key: TKey
87 if (op.type === `update`) {
88 // For updates, we need to get the key from the partial data
89 key = ctx.getKey(item as TRow)
90 } else {
91 // For insert/upsert, validate and resolve the full item first
92 const resolved = ctx.collection.validateData(
93 item,
94 op.type === `upsert` ? `insert` : op.type,
95 )
96 key = ctx.getKey(resolved)
97 }
98 normalized.push({ type: op.type, key, data: item })
99 }
100 }
101 }
102
103 return normalized
104}
105
106// Validate operations before executing
107function validateOperations<

Callers 1

performWriteOperationsFunction · 0.85

Calls 2

getKeyMethod · 0.45
validateDataMethod · 0.45

Tested by

no test coverage detected