MCPcopy Index your code
hub / github.com/TanStack/db / processMerge

Function processMerge

packages/db/src/query/compiler/select.ts:58–94  ·  view source on GitHub ↗

* Processes a merge operation by merging source values into the target path

(
  op: Extract<SelectOp, { kind: `merge` }>,
  namespacedRow: NamespacedRow,
  selectResults: Record<string, any>,
)

Source from the content-addressed store, hash-verified

56 * Processes a merge operation by merging source values into the target path
57 */
58function processMerge(
59 op: Extract<SelectOp, { kind: `merge` }>,
60 namespacedRow: NamespacedRow,
61 selectResults: Record<string, any>,
62): void {
63 assertSafeAliasSegments(op.targetPath)
64 const value = op.source(namespacedRow)
65 if (value && typeof value === `object`) {
66 // Ensure target object exists
67 let cursor: any = selectResults
68 const path = op.targetPath
69 if (path.length === 0) {
70 // Top-level merge
71 for (const [k, v] of Object.entries(value)) {
72 selectResults[k] = unwrapVal(v)
73 }
74 } else {
75 for (let i = 0; i < path.length; i++) {
76 const seg = path[i]!
77 if (i === path.length - 1) {
78 const dest = (cursor[seg] ??= {})
79 if (typeof dest === `object`) {
80 for (const [k, v] of Object.entries(value)) {
81 dest[k] = unwrapVal(v)
82 }
83 }
84 } else {
85 const next = cursor[seg]
86 if (next == null || typeof next !== `object`) {
87 cursor[seg] = {}
88 }
89 cursor = cursor[seg]
90 }
91 }
92 }
93 }
94}
95
96/**
97 * Processes a non-merge operation by setting the field value at the specified alias path

Callers 2

processRowFunction · 0.85
compileSelectObjectFunction · 0.85

Calls 3

assertSafeAliasSegmentsFunction · 0.85
unwrapValFunction · 0.85
entriesMethod · 0.45

Tested by

no test coverage detected