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

Class BaseQueryBuilder

packages/db/src/query/builder/index.ts:78–943  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76const UNION_ALL_SOURCE_CONTEXT = `unionAll clause` satisfies SourceClauseContext
77
78export class BaseQueryBuilder<TContext extends Context = Context> {
79 private readonly query: Partial<QueryIR> = {}
80
81 constructor(query: Partial<QueryIR> = {}) {
82 this.query = { ...query }
83 }
84
85 /**
86 * Creates a CollectionRef or QueryRef from a source object
87 * @param source - An object with a single key-value pair
88 * @param context - Context string for error messages (e.g., "from clause", "join clause")
89 * @returns A tuple of [alias, ref] where alias is the source key and ref is the created reference
90 */
91 private _createRefForSource<TSource extends Source>(
92 source: TSource,
93 context: SourceClauseContext,
94 ): [string, CollectionRef | QueryRef] {
95 const refs = this._createRefsForSource(source, context)
96 if (refs.length !== 1) {
97 throw new OnlyOneSourceAllowedError(context)
98 }
99 return refs[0]!
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) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected