@tanstack/db 0.0.14 → 0.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/dist/cjs/collection.cjs +117 -104
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +18 -21
  4. package/dist/cjs/index.cjs +31 -13
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/index.d.cts +0 -1
  7. package/dist/cjs/query/builder/functions.cjs +107 -0
  8. package/dist/cjs/query/builder/functions.cjs.map +1 -0
  9. package/dist/cjs/query/builder/functions.d.cts +38 -0
  10. package/dist/cjs/query/builder/index.cjs +499 -0
  11. package/dist/cjs/query/builder/index.cjs.map +1 -0
  12. package/dist/cjs/query/builder/index.d.cts +324 -0
  13. package/dist/cjs/query/builder/ref-proxy.cjs +92 -0
  14. package/dist/cjs/query/builder/ref-proxy.cjs.map +1 -0
  15. package/dist/cjs/query/builder/ref-proxy.d.cts +28 -0
  16. package/dist/cjs/query/builder/types.d.cts +81 -0
  17. package/dist/cjs/query/compiler/evaluators.cjs +261 -0
  18. package/dist/cjs/query/compiler/evaluators.cjs.map +1 -0
  19. package/dist/cjs/query/compiler/evaluators.d.cts +11 -0
  20. package/dist/cjs/query/compiler/group-by.cjs +271 -0
  21. package/dist/cjs/query/compiler/group-by.cjs.map +1 -0
  22. package/dist/cjs/query/compiler/group-by.d.cts +7 -0
  23. package/dist/cjs/query/compiler/index.cjs +181 -0
  24. package/dist/cjs/query/compiler/index.cjs.map +1 -0
  25. package/dist/cjs/query/compiler/index.d.cts +15 -0
  26. package/dist/cjs/query/compiler/joins.cjs +116 -0
  27. package/dist/cjs/query/compiler/joins.cjs.map +1 -0
  28. package/dist/cjs/query/compiler/joins.d.cts +11 -0
  29. package/dist/cjs/query/compiler/order-by.cjs +89 -0
  30. package/dist/cjs/query/compiler/order-by.cjs.map +1 -0
  31. package/dist/cjs/query/compiler/order-by.d.cts +9 -0
  32. package/dist/cjs/query/compiler/select.cjs +57 -0
  33. package/dist/cjs/query/compiler/select.cjs.map +1 -0
  34. package/dist/cjs/query/compiler/select.d.cts +15 -0
  35. package/dist/cjs/query/index.d.cts +5 -5
  36. package/dist/cjs/query/ir.cjs +57 -0
  37. package/dist/cjs/query/ir.cjs.map +1 -0
  38. package/dist/cjs/query/ir.d.cts +81 -0
  39. package/dist/cjs/query/live-query-collection.cjs +224 -0
  40. package/dist/cjs/query/live-query-collection.cjs.map +1 -0
  41. package/dist/cjs/query/live-query-collection.d.cts +124 -0
  42. package/dist/cjs/transactions.cjs +20 -13
  43. package/dist/cjs/transactions.cjs.map +1 -1
  44. package/dist/cjs/transactions.d.cts +10 -1
  45. package/dist/cjs/types.d.cts +13 -0
  46. package/dist/esm/collection.d.ts +18 -21
  47. package/dist/esm/collection.js +118 -105
  48. package/dist/esm/collection.js.map +1 -1
  49. package/dist/esm/index.d.ts +0 -1
  50. package/dist/esm/index.js +30 -12
  51. package/dist/esm/query/builder/functions.d.ts +38 -0
  52. package/dist/esm/query/builder/functions.js +107 -0
  53. package/dist/esm/query/builder/functions.js.map +1 -0
  54. package/dist/esm/query/builder/index.d.ts +324 -0
  55. package/dist/esm/query/builder/index.js +499 -0
  56. package/dist/esm/query/builder/index.js.map +1 -0
  57. package/dist/esm/query/builder/ref-proxy.d.ts +28 -0
  58. package/dist/esm/query/builder/ref-proxy.js +92 -0
  59. package/dist/esm/query/builder/ref-proxy.js.map +1 -0
  60. package/dist/esm/query/builder/types.d.ts +81 -0
  61. package/dist/esm/query/compiler/evaluators.d.ts +11 -0
  62. package/dist/esm/query/compiler/evaluators.js +261 -0
  63. package/dist/esm/query/compiler/evaluators.js.map +1 -0
  64. package/dist/esm/query/compiler/group-by.d.ts +7 -0
  65. package/dist/esm/query/compiler/group-by.js +271 -0
  66. package/dist/esm/query/compiler/group-by.js.map +1 -0
  67. package/dist/esm/query/compiler/index.d.ts +15 -0
  68. package/dist/esm/query/compiler/index.js +181 -0
  69. package/dist/esm/query/compiler/index.js.map +1 -0
  70. package/dist/esm/query/compiler/joins.d.ts +11 -0
  71. package/dist/esm/query/compiler/joins.js +116 -0
  72. package/dist/esm/query/compiler/joins.js.map +1 -0
  73. package/dist/esm/query/compiler/order-by.d.ts +9 -0
  74. package/dist/esm/query/compiler/order-by.js +89 -0
  75. package/dist/esm/query/compiler/order-by.js.map +1 -0
  76. package/dist/esm/query/compiler/select.d.ts +15 -0
  77. package/dist/esm/query/compiler/select.js +57 -0
  78. package/dist/esm/query/compiler/select.js.map +1 -0
  79. package/dist/esm/query/index.d.ts +5 -5
  80. package/dist/esm/query/ir.d.ts +81 -0
  81. package/dist/esm/query/ir.js +57 -0
  82. package/dist/esm/query/ir.js.map +1 -0
  83. package/dist/esm/query/live-query-collection.d.ts +124 -0
  84. package/dist/esm/query/live-query-collection.js +224 -0
  85. package/dist/esm/query/live-query-collection.js.map +1 -0
  86. package/dist/esm/transactions.d.ts +10 -1
  87. package/dist/esm/transactions.js +20 -13
  88. package/dist/esm/transactions.js.map +1 -1
  89. package/dist/esm/types.d.ts +13 -0
  90. package/package.json +3 -4
  91. package/src/collection.ts +152 -129
  92. package/src/index.ts +0 -1
  93. package/src/query/builder/functions.ts +267 -0
  94. package/src/query/builder/index.ts +648 -0
  95. package/src/query/builder/ref-proxy.ts +156 -0
  96. package/src/query/builder/types.ts +282 -0
  97. package/src/query/compiler/evaluators.ts +315 -0
  98. package/src/query/compiler/group-by.ts +428 -0
  99. package/src/query/compiler/index.ts +276 -0
  100. package/src/query/compiler/joins.ts +228 -0
  101. package/src/query/compiler/order-by.ts +139 -0
  102. package/src/query/compiler/select.ts +173 -0
  103. package/src/query/index.ts +54 -5
  104. package/src/query/ir.ts +128 -0
  105. package/src/query/live-query-collection.ts +512 -0
  106. package/src/transactions.ts +27 -16
  107. package/src/types.ts +15 -0
  108. package/dist/cjs/query/compiled-query.cjs +0 -160
  109. package/dist/cjs/query/compiled-query.cjs.map +0 -1
  110. package/dist/cjs/query/compiled-query.d.cts +0 -20
  111. package/dist/cjs/query/evaluators.cjs +0 -161
  112. package/dist/cjs/query/evaluators.cjs.map +0 -1
  113. package/dist/cjs/query/evaluators.d.cts +0 -14
  114. package/dist/cjs/query/extractors.cjs +0 -122
  115. package/dist/cjs/query/extractors.cjs.map +0 -1
  116. package/dist/cjs/query/extractors.d.cts +0 -22
  117. package/dist/cjs/query/functions.cjs +0 -152
  118. package/dist/cjs/query/functions.cjs.map +0 -1
  119. package/dist/cjs/query/functions.d.cts +0 -21
  120. package/dist/cjs/query/group-by.cjs +0 -88
  121. package/dist/cjs/query/group-by.cjs.map +0 -1
  122. package/dist/cjs/query/group-by.d.cts +0 -40
  123. package/dist/cjs/query/joins.cjs +0 -141
  124. package/dist/cjs/query/joins.cjs.map +0 -1
  125. package/dist/cjs/query/joins.d.cts +0 -14
  126. package/dist/cjs/query/order-by.cjs +0 -185
  127. package/dist/cjs/query/order-by.cjs.map +0 -1
  128. package/dist/cjs/query/order-by.d.cts +0 -3
  129. package/dist/cjs/query/pipeline-compiler.cjs +0 -89
  130. package/dist/cjs/query/pipeline-compiler.cjs.map +0 -1
  131. package/dist/cjs/query/pipeline-compiler.d.cts +0 -10
  132. package/dist/cjs/query/query-builder.cjs +0 -307
  133. package/dist/cjs/query/query-builder.cjs.map +0 -1
  134. package/dist/cjs/query/query-builder.d.cts +0 -225
  135. package/dist/cjs/query/schema.d.cts +0 -100
  136. package/dist/cjs/query/select.cjs +0 -130
  137. package/dist/cjs/query/select.cjs.map +0 -1
  138. package/dist/cjs/query/select.d.cts +0 -3
  139. package/dist/cjs/query/types.d.cts +0 -189
  140. package/dist/cjs/query/utils.cjs +0 -154
  141. package/dist/cjs/query/utils.cjs.map +0 -1
  142. package/dist/cjs/query/utils.d.cts +0 -37
  143. package/dist/cjs/utils.cjs +0 -17
  144. package/dist/cjs/utils.cjs.map +0 -1
  145. package/dist/cjs/utils.d.cts +0 -3
  146. package/dist/esm/query/compiled-query.d.ts +0 -20
  147. package/dist/esm/query/compiled-query.js +0 -160
  148. package/dist/esm/query/compiled-query.js.map +0 -1
  149. package/dist/esm/query/evaluators.d.ts +0 -14
  150. package/dist/esm/query/evaluators.js +0 -161
  151. package/dist/esm/query/evaluators.js.map +0 -1
  152. package/dist/esm/query/extractors.d.ts +0 -22
  153. package/dist/esm/query/extractors.js +0 -122
  154. package/dist/esm/query/extractors.js.map +0 -1
  155. package/dist/esm/query/functions.d.ts +0 -21
  156. package/dist/esm/query/functions.js +0 -152
  157. package/dist/esm/query/functions.js.map +0 -1
  158. package/dist/esm/query/group-by.d.ts +0 -40
  159. package/dist/esm/query/group-by.js +0 -88
  160. package/dist/esm/query/group-by.js.map +0 -1
  161. package/dist/esm/query/joins.d.ts +0 -14
  162. package/dist/esm/query/joins.js +0 -141
  163. package/dist/esm/query/joins.js.map +0 -1
  164. package/dist/esm/query/order-by.d.ts +0 -3
  165. package/dist/esm/query/order-by.js +0 -185
  166. package/dist/esm/query/order-by.js.map +0 -1
  167. package/dist/esm/query/pipeline-compiler.d.ts +0 -10
  168. package/dist/esm/query/pipeline-compiler.js +0 -89
  169. package/dist/esm/query/pipeline-compiler.js.map +0 -1
  170. package/dist/esm/query/query-builder.d.ts +0 -225
  171. package/dist/esm/query/query-builder.js +0 -307
  172. package/dist/esm/query/query-builder.js.map +0 -1
  173. package/dist/esm/query/schema.d.ts +0 -100
  174. package/dist/esm/query/select.d.ts +0 -3
  175. package/dist/esm/query/select.js +0 -130
  176. package/dist/esm/query/select.js.map +0 -1
  177. package/dist/esm/query/types.d.ts +0 -189
  178. package/dist/esm/query/utils.d.ts +0 -37
  179. package/dist/esm/query/utils.js +0 -154
  180. package/dist/esm/query/utils.js.map +0 -1
  181. package/dist/esm/utils.d.ts +0 -3
  182. package/dist/esm/utils.js +0 -17
  183. package/dist/esm/utils.js.map +0 -1
  184. package/src/query/compiled-query.ts +0 -234
  185. package/src/query/evaluators.ts +0 -250
  186. package/src/query/extractors.ts +0 -214
  187. package/src/query/functions.ts +0 -297
  188. package/src/query/group-by.ts +0 -139
  189. package/src/query/joins.ts +0 -260
  190. package/src/query/order-by.ts +0 -264
  191. package/src/query/pipeline-compiler.ts +0 -149
  192. package/src/query/query-builder.ts +0 -902
  193. package/src/query/schema.ts +0 -268
  194. package/src/query/select.ts +0 -208
  195. package/src/query/types.ts +0 -418
  196. package/src/query/utils.ts +0 -245
  197. package/src/utils.ts +0 -15
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.js","sources":["../../../../src/query/builder/functions.ts"],"sourcesContent":["import { Aggregate, Func } from \"../ir\"\nimport { toExpression } from \"./ref-proxy.js\"\nimport type { BasicExpression } from \"../ir\"\nimport type { RefProxy } from \"./ref-proxy.js\"\n\n// Helper type for any expression-like value\ntype ExpressionLike = BasicExpression | RefProxy<any> | any\n\n// Operators\n\nexport function eq<T>(\n left: RefProxy<T>,\n right: T | RefProxy<T> | BasicExpression<T>\n): BasicExpression<boolean>\nexport function eq<T extends string | number | boolean>(\n left: T | BasicExpression<T>,\n right: T | BasicExpression<T>\n): BasicExpression<boolean>\nexport function eq<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>\nexport function eq(left: any, right: any): BasicExpression<boolean> {\n return new Func(`eq`, [toExpression(left), toExpression(right)])\n}\n\nexport function gt<T>(\n left: RefProxy<T>,\n right: T | RefProxy<T> | BasicExpression<T>\n): BasicExpression<boolean>\nexport function gt<T extends string | number>(\n left: T | BasicExpression<T>,\n right: T | BasicExpression<T>\n): BasicExpression<boolean>\nexport function gt<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>\nexport function gt(left: any, right: any): BasicExpression<boolean> {\n return new Func(`gt`, [toExpression(left), toExpression(right)])\n}\n\nexport function gte<T>(\n left: RefProxy<T>,\n right: T | RefProxy<T> | BasicExpression<T>\n): BasicExpression<boolean>\nexport function gte<T extends string | number>(\n left: T | BasicExpression<T>,\n right: T | BasicExpression<T>\n): BasicExpression<boolean>\nexport function gte<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>\nexport function gte(left: any, right: any): BasicExpression<boolean> {\n return new Func(`gte`, [toExpression(left), toExpression(right)])\n}\n\nexport function lt<T>(\n left: RefProxy<T>,\n right: T | RefProxy<T> | BasicExpression<T>\n): BasicExpression<boolean>\nexport function lt<T extends string | number>(\n left: T | BasicExpression<T>,\n right: T | BasicExpression<T>\n): BasicExpression<boolean>\nexport function lt<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>\nexport function lt(left: any, right: any): BasicExpression<boolean> {\n return new Func(`lt`, [toExpression(left), toExpression(right)])\n}\n\nexport function lte<T>(\n left: RefProxy<T>,\n right: T | RefProxy<T> | BasicExpression<T>\n): BasicExpression<boolean>\nexport function lte<T extends string | number>(\n left: T | BasicExpression<T>,\n right: T | BasicExpression<T>\n): BasicExpression<boolean>\nexport function lte<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>\nexport function lte(left: any, right: any): BasicExpression<boolean> {\n return new Func(`lte`, [toExpression(left), toExpression(right)])\n}\n\n// Overloads for and() - support 2 or more arguments\nexport function and(\n left: ExpressionLike,\n right: ExpressionLike\n): BasicExpression<boolean>\nexport function and(\n left: ExpressionLike,\n right: ExpressionLike,\n ...rest: Array<ExpressionLike>\n): BasicExpression<boolean>\nexport function and(\n left: ExpressionLike,\n right: ExpressionLike,\n ...rest: Array<ExpressionLike>\n): BasicExpression<boolean> {\n const allArgs = [left, right, ...rest]\n return new Func(\n `and`,\n allArgs.map((arg) => toExpression(arg))\n )\n}\n\n// Overloads for or() - support 2 or more arguments\nexport function or(\n left: ExpressionLike,\n right: ExpressionLike\n): BasicExpression<boolean>\nexport function or(\n left: ExpressionLike,\n right: ExpressionLike,\n ...rest: Array<ExpressionLike>\n): BasicExpression<boolean>\nexport function or(\n left: ExpressionLike,\n right: ExpressionLike,\n ...rest: Array<ExpressionLike>\n): BasicExpression<boolean> {\n const allArgs = [left, right, ...rest]\n return new Func(\n `or`,\n allArgs.map((arg) => toExpression(arg))\n )\n}\n\nexport function not(value: ExpressionLike): BasicExpression<boolean> {\n return new Func(`not`, [toExpression(value)])\n}\n\nexport function inArray(\n value: ExpressionLike,\n array: ExpressionLike\n): BasicExpression<boolean> {\n return new Func(`in`, [toExpression(value), toExpression(array)])\n}\n\nexport function like(\n left:\n | RefProxy<string>\n | RefProxy<string | null>\n | RefProxy<string | undefined>\n | string\n | BasicExpression<string>,\n right: string | RefProxy<string> | BasicExpression<string>\n): BasicExpression<boolean>\nexport function like(left: any, right: any): BasicExpression<boolean> {\n return new Func(`like`, [toExpression(left), toExpression(right)])\n}\n\nexport function ilike(\n left:\n | RefProxy<string>\n | RefProxy<string | null>\n | RefProxy<string | undefined>\n | string\n | BasicExpression<string>,\n right: string | RefProxy<string> | BasicExpression<string>\n): BasicExpression<boolean> {\n return new Func(`ilike`, [toExpression(left), toExpression(right)])\n}\n\n// Functions\n\nexport function upper(\n arg:\n | RefProxy<string>\n | RefProxy<string | undefined>\n | string\n | BasicExpression<string>\n): BasicExpression<string> {\n return new Func(`upper`, [toExpression(arg)])\n}\n\nexport function lower(\n arg:\n | RefProxy<string>\n | RefProxy<string | undefined>\n | string\n | BasicExpression<string>\n): BasicExpression<string> {\n return new Func(`lower`, [toExpression(arg)])\n}\n\nexport function length(\n arg:\n | RefProxy<string>\n | RefProxy<string | undefined>\n | RefProxy<Array<any>>\n | RefProxy<Array<any> | undefined>\n | string\n | Array<any>\n | BasicExpression<string>\n | BasicExpression<Array<any>>\n): BasicExpression<number> {\n return new Func(`length`, [toExpression(arg)])\n}\n\nexport function concat(\n ...args: Array<ExpressionLike>\n): BasicExpression<string> {\n return new Func(\n `concat`,\n args.map((arg) => toExpression(arg))\n )\n}\n\nexport function coalesce(...args: Array<ExpressionLike>): BasicExpression<any> {\n return new Func(\n `coalesce`,\n args.map((arg) => toExpression(arg))\n )\n}\n\nexport function add(\n left:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>,\n right:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>\n): BasicExpression<number> {\n return new Func(`add`, [toExpression(left), toExpression(right)])\n}\n\n// Aggregates\n\nexport function count(arg: ExpressionLike): Aggregate<number> {\n return new Aggregate(`count`, [toExpression(arg)])\n}\n\nexport function avg(\n arg:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>\n): Aggregate<number> {\n return new Aggregate(`avg`, [toExpression(arg)])\n}\n\nexport function sum(\n arg:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>\n): Aggregate<number> {\n return new Aggregate(`sum`, [toExpression(arg)])\n}\n\nexport function min(\n arg:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>\n): Aggregate<number> {\n return new Aggregate(`min`, [toExpression(arg)])\n}\n\nexport function max(\n arg:\n | RefProxy<number>\n | RefProxy<number | undefined>\n | number\n | BasicExpression<number>\n): Aggregate<number> {\n return new Aggregate(`max`, [toExpression(arg)])\n}\n"],"names":[],"mappings":"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"}
@@ -0,0 +1,324 @@
1
+ import { OrderByDirection, QueryIR } from '../ir.js';
2
+ import { Context, GroupByCallback, JoinOnCallback, MergeContext, MergeContextWithJoinType, OrderByCallback, RefProxyForContext, ResultTypeFromSelect, SchemaFromSource, SelectObject, Source, WhereCallback, WithResult } from './types.js';
3
+ export declare class BaseQueryBuilder<TContext extends Context = Context> {
4
+ private readonly query;
5
+ constructor(query?: Partial<QueryIR>);
6
+ /**
7
+ * Creates a CollectionRef or QueryRef from a source object
8
+ * @param source - An object with a single key-value pair
9
+ * @param context - Context string for error messages (e.g., "from clause", "join clause")
10
+ * @returns A tuple of [alias, ref] where alias is the source key and ref is the created reference
11
+ */
12
+ private _createRefForSource;
13
+ /**
14
+ * Specify the source table or subquery for the query
15
+ *
16
+ * @param source - An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
17
+ * @returns A QueryBuilder with the specified source
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * // Query from a collection
22
+ * query.from({ users: usersCollection })
23
+ *
24
+ * // Query from a subquery
25
+ * const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
26
+ * query.from({ activeUsers })
27
+ * ```
28
+ */
29
+ from<TSource extends Source>(source: TSource): QueryBuilder<{
30
+ baseSchema: SchemaFromSource<TSource>;
31
+ schema: SchemaFromSource<TSource>;
32
+ fromSourceName: keyof TSource & string;
33
+ hasJoins: false;
34
+ }>;
35
+ /**
36
+ * Join another table or subquery to the current query
37
+ *
38
+ * @param source - An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
39
+ * @param onCallback - A function that receives table references and returns the join condition
40
+ * @param type - The type of join: 'inner', 'left', 'right', or 'full' (defaults to 'left')
41
+ * @returns A QueryBuilder with the joined table available
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * // Left join users with posts
46
+ * query
47
+ * .from({ users: usersCollection })
48
+ * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
49
+ *
50
+ * // Inner join with explicit type
51
+ * query
52
+ * .from({ u: usersCollection })
53
+ * .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId), 'inner')
54
+ * ```
55
+ *
56
+ * // Join with a subquery
57
+ * const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
58
+ * query
59
+ * .from({ activeUsers })
60
+ * .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId))
61
+ */
62
+ join<TSource extends Source, TJoinType extends `inner` | `left` | `right` | `full` = `left`>(source: TSource, onCallback: JoinOnCallback<MergeContext<TContext, SchemaFromSource<TSource>>>, type?: TJoinType): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, TJoinType>>;
63
+ /**
64
+ * Filter rows based on a condition
65
+ *
66
+ * @param callback - A function that receives table references and returns an expression
67
+ * @returns A QueryBuilder with the where condition applied
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * // Simple condition
72
+ * query
73
+ * .from({ users: usersCollection })
74
+ * .where(({users}) => gt(users.age, 18))
75
+ *
76
+ * // Multiple conditions
77
+ * query
78
+ * .from({ users: usersCollection })
79
+ * .where(({users}) => and(
80
+ * gt(users.age, 18),
81
+ * eq(users.active, true)
82
+ * ))
83
+ *
84
+ * // Multiple where calls are ANDed together
85
+ * query
86
+ * .from({ users: usersCollection })
87
+ * .where(({users}) => gt(users.age, 18))
88
+ * .where(({users}) => eq(users.active, true))
89
+ * ```
90
+ */
91
+ where(callback: WhereCallback<TContext>): QueryBuilder<TContext>;
92
+ /**
93
+ * Filter grouped rows based on aggregate conditions
94
+ *
95
+ * @param callback - A function that receives table references and returns an expression
96
+ * @returns A QueryBuilder with the having condition applied
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * // Filter groups by count
101
+ * query
102
+ * .from({ posts: postsCollection })
103
+ * .groupBy(({posts}) => posts.userId)
104
+ * .having(({posts}) => gt(count(posts.id), 5))
105
+ *
106
+ * // Filter by average
107
+ * query
108
+ * .from({ orders: ordersCollection })
109
+ * .groupBy(({orders}) => orders.customerId)
110
+ * .having(({orders}) => gt(avg(orders.total), 100))
111
+ *
112
+ * // Multiple having calls are ANDed together
113
+ * query
114
+ * .from({ orders: ordersCollection })
115
+ * .groupBy(({orders}) => orders.customerId)
116
+ * .having(({orders}) => gt(count(orders.id), 5))
117
+ * .having(({orders}) => gt(avg(orders.total), 100))
118
+ * ```
119
+ */
120
+ having(callback: WhereCallback<TContext>): QueryBuilder<TContext>;
121
+ /**
122
+ * Select specific columns or computed values from the query
123
+ *
124
+ * @param callback - A function that receives table references and returns an object with selected fields or expressions
125
+ * @returns A QueryBuilder that returns only the selected fields
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * // Select specific columns
130
+ * query
131
+ * .from({ users: usersCollection })
132
+ * .select(({users}) => ({
133
+ * name: users.name,
134
+ * email: users.email
135
+ * }))
136
+ *
137
+ * // Select with computed values
138
+ * query
139
+ * .from({ users: usersCollection })
140
+ * .select(({users}) => ({
141
+ * fullName: concat(users.firstName, ' ', users.lastName),
142
+ * ageInMonths: mul(users.age, 12)
143
+ * }))
144
+ *
145
+ * // Select with aggregates (requires GROUP BY)
146
+ * query
147
+ * .from({ posts: postsCollection })
148
+ * .groupBy(({posts}) => posts.userId)
149
+ * .select(({posts, count}) => ({
150
+ * userId: posts.userId,
151
+ * postCount: count(posts.id)
152
+ * }))
153
+ * ```
154
+ */
155
+ select<TSelectObject extends SelectObject>(callback: (refs: RefProxyForContext<TContext>) => TSelectObject): QueryBuilder<WithResult<TContext, ResultTypeFromSelect<TSelectObject>>>;
156
+ /**
157
+ * Sort the query results by one or more columns
158
+ *
159
+ * @param callback - A function that receives table references and returns the field to sort by
160
+ * @param direction - Sort direction: 'asc' for ascending, 'desc' for descending (defaults to 'asc')
161
+ * @returns A QueryBuilder with the ordering applied
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * // Sort by a single column
166
+ * query
167
+ * .from({ users: usersCollection })
168
+ * .orderBy(({users}) => users.name)
169
+ *
170
+ * // Sort descending
171
+ * query
172
+ * .from({ users: usersCollection })
173
+ * .orderBy(({users}) => users.createdAt, 'desc')
174
+ *
175
+ * // Multiple sorts (chain orderBy calls)
176
+ * query
177
+ * .from({ users: usersCollection })
178
+ * .orderBy(({users}) => users.lastName)
179
+ * .orderBy(({users}) => users.firstName)
180
+ * ```
181
+ */
182
+ orderBy(callback: OrderByCallback<TContext>, direction?: OrderByDirection): QueryBuilder<TContext>;
183
+ /**
184
+ * Group rows by one or more columns for aggregation
185
+ *
186
+ * @param callback - A function that receives table references and returns the field(s) to group by
187
+ * @returns A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING)
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * // Group by a single column
192
+ * query
193
+ * .from({ posts: postsCollection })
194
+ * .groupBy(({posts}) => posts.userId)
195
+ * .select(({posts, count}) => ({
196
+ * userId: posts.userId,
197
+ * postCount: count()
198
+ * }))
199
+ *
200
+ * // Group by multiple columns
201
+ * query
202
+ * .from({ sales: salesCollection })
203
+ * .groupBy(({sales}) => [sales.region, sales.category])
204
+ * .select(({sales, sum}) => ({
205
+ * region: sales.region,
206
+ * category: sales.category,
207
+ * totalSales: sum(sales.amount)
208
+ * }))
209
+ * ```
210
+ */
211
+ groupBy(callback: GroupByCallback<TContext>): QueryBuilder<TContext>;
212
+ /**
213
+ * Limit the number of rows returned by the query
214
+ * `orderBy` is required for `limit`
215
+ *
216
+ * @param count - Maximum number of rows to return
217
+ * @returns A QueryBuilder with the limit applied
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * // Get top 5 posts by likes
222
+ * query
223
+ * .from({ posts: postsCollection })
224
+ * .orderBy(({posts}) => posts.likes, 'desc')
225
+ * .limit(5)
226
+ * ```
227
+ */
228
+ limit(count: number): QueryBuilder<TContext>;
229
+ /**
230
+ * Skip a number of rows before returning results
231
+ * `orderBy` is required for `offset`
232
+ *
233
+ * @param count - Number of rows to skip
234
+ * @returns A QueryBuilder with the offset applied
235
+ *
236
+ * @example
237
+ * ```ts
238
+ * // Get second page of results
239
+ * query
240
+ * .from({ posts: postsCollection })
241
+ * .orderBy(({posts}) => posts.createdAt, 'desc')
242
+ * .offset(page * pageSize)
243
+ * .limit(pageSize)
244
+ * ```
245
+ */
246
+ offset(count: number): QueryBuilder<TContext>;
247
+ private _getCurrentAliases;
248
+ /**
249
+ * Functional variants of the query builder
250
+ * These are imperative function that are called for ery row.
251
+ * Warning: that these cannot be optimized by the query compiler, and may prevent
252
+ * some type of optimizations being possible.
253
+ * @example
254
+ * ```ts
255
+ * q.fn.select((row) => ({
256
+ * name: row.user.name.toUpperCase(),
257
+ * age: row.user.age + 1,
258
+ * }))
259
+ * ```
260
+ */
261
+ get fn(): {
262
+ /**
263
+ * Select fields using a function that operates on each row
264
+ * Warning: This cannot be optimized by the query compiler
265
+ *
266
+ * @param callback - A function that receives a row and returns the selected value
267
+ * @returns A QueryBuilder with functional selection applied
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * // Functional select (not optimized)
272
+ * query
273
+ * .from({ users: usersCollection })
274
+ * .fn.select(row => ({
275
+ * name: row.users.name.toUpperCase(),
276
+ * age: row.users.age + 1,
277
+ * }))
278
+ * ```
279
+ */
280
+ select<TFuncSelectResult>(callback: (row: TContext[`schema`]) => TFuncSelectResult): QueryBuilder<WithResult<TContext, TFuncSelectResult>>;
281
+ /**
282
+ * Filter rows using a function that operates on each row
283
+ * Warning: This cannot be optimized by the query compiler
284
+ *
285
+ * @param callback - A function that receives a row and returns a boolean
286
+ * @returns A QueryBuilder with functional filtering applied
287
+ *
288
+ * @example
289
+ * ```ts
290
+ * // Functional where (not optimized)
291
+ * query
292
+ * .from({ users: usersCollection })
293
+ * .fn.where(row => row.users.name.startsWith('A'))
294
+ * ```
295
+ */
296
+ where(callback: (row: TContext[`schema`]) => any): QueryBuilder<TContext>;
297
+ /**
298
+ * Filter grouped rows using a function that operates on each aggregated row
299
+ * Warning: This cannot be optimized by the query compiler
300
+ *
301
+ * @param callback - A function that receives an aggregated row and returns a boolean
302
+ * @returns A QueryBuilder with functional having filter applied
303
+ *
304
+ * @example
305
+ * ```ts
306
+ * // Functional having (not optimized)
307
+ * query
308
+ * .from({ posts: postsCollection })
309
+ * .groupBy(({posts}) => posts.userId)
310
+ * .fn.having(row => row.count > 5)
311
+ * ```
312
+ */
313
+ having(callback: (row: TContext[`schema`]) => any): QueryBuilder<TContext>;
314
+ };
315
+ _getQuery(): QueryIR;
316
+ }
317
+ export declare function buildQuery<TContext extends Context>(fn: (builder: InitialQueryBuilder) => QueryBuilder<TContext>): QueryIR;
318
+ export declare function getQueryIR(builder: BaseQueryBuilder | QueryBuilder<any> | InitialQueryBuilder): QueryIR;
319
+ export type InitialQueryBuilder = Pick<BaseQueryBuilder<Context>, `from`>;
320
+ export type InitialQueryBuilderConstructor = new () => InitialQueryBuilder;
321
+ export type QueryBuilder<TContext extends Context> = Omit<BaseQueryBuilder<TContext>, `from` | `_getQuery`>;
322
+ export declare const Query: InitialQueryBuilderConstructor;
323
+ export type ExtractContext<T> = T extends BaseQueryBuilder<infer TContext> ? TContext : T extends QueryBuilder<infer TContext> ? TContext : never;
324
+ export type { Context, Source, GetResult } from './types.js';