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

Method _createRefsForSource

packages/db/src/query/builder/index.ts:102–157  ·  view source on GitHub ↗
(
    source: TSource,
    context: SourceClauseContext,
  )

Source from the content-addressed store, hash-verified

100 }
101
102 private _createRefsForSource<TSource extends Source>(
103 source: TSource,
104 context: SourceClauseContext,
105 ): Array<[string, CollectionRef | QueryRef]> {
106 if (typeof source === `string`) {
107 throw new InvalidSourceTypeError(context, `string`)
108 }
109
110 // Validate source is a plain object (not null, array, string, etc.)
111 // We use try-catch to handle null/undefined gracefully
112 let keys: Array<string>
113 try {
114 keys = Object.keys(source)
115 } catch {
116 // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
117 const type = source === null ? `null` : `undefined`
118 throw new InvalidSourceTypeError(context, type)
119 }
120
121 // Check if it's an array (arrays pass Object.keys but aren't valid sources)
122 if (Array.isArray(source)) {
123 throw new InvalidSourceTypeError(context, `array`)
124 }
125
126 if (keys.length === 0) {
127 throw new InvalidSourceTypeError(context, `empty object`)
128 }
129
130 if (context !== UNION_ALL_SOURCE_CONTEXT && keys.length !== 1) {
131 throw new OnlyOneSourceAllowedError(context)
132 }
133
134 const refs: Array<[string, CollectionRef | QueryRef]> = []
135 for (const alias of keys) {
136 const sourceValue = source[alias]
137
138 // Validate the value is a Collection or QueryBuilder
139 let ref: CollectionRef | QueryRef
140
141 if (sourceValue instanceof CollectionImpl) {
142 ref = new CollectionRef(sourceValue, alias)
143 } else if (sourceValue instanceof BaseQueryBuilder) {
144 const subQuery = sourceValue._getQuery()
145 if (!(subQuery as Partial<QueryIR>).from) {
146 throw new SubQueryMustHaveFromClauseError(context)
147 }
148 ref = new QueryRef(subQuery, alias)
149 } else {
150 throw new InvalidSourceError(alias)
151 }
152
153 refs.push([alias, ref])
154 }
155
156 return refs
157 }
158
159 /**

Callers 2

_createRefForSourceMethod · 0.95
unionAllMethod · 0.95

Calls 2

_getQueryMethod · 0.80
keysMethod · 0.45

Tested by

no test coverage detected