@tanstack/db 0.0.13 → 0.0.15

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 (198) 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 +19 -22
  4. package/dist/cjs/index.cjs +35 -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 +96 -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 +80 -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 +6 -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 +13 -4
  45. package/dist/cjs/types.d.cts +14 -1
  46. package/dist/esm/collection.d.ts +19 -22
  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 +34 -12
  51. package/dist/esm/index.js.map +1 -1
  52. package/dist/esm/query/builder/functions.d.ts +38 -0
  53. package/dist/esm/query/builder/functions.js +107 -0
  54. package/dist/esm/query/builder/functions.js.map +1 -0
  55. package/dist/esm/query/builder/index.d.ts +324 -0
  56. package/dist/esm/query/builder/index.js +499 -0
  57. package/dist/esm/query/builder/index.js.map +1 -0
  58. package/dist/esm/query/builder/ref-proxy.d.ts +28 -0
  59. package/dist/esm/query/builder/ref-proxy.js +96 -0
  60. package/dist/esm/query/builder/ref-proxy.js.map +1 -0
  61. package/dist/esm/query/builder/types.d.ts +80 -0
  62. package/dist/esm/query/compiler/evaluators.d.ts +11 -0
  63. package/dist/esm/query/compiler/evaluators.js +261 -0
  64. package/dist/esm/query/compiler/evaluators.js.map +1 -0
  65. package/dist/esm/query/compiler/group-by.d.ts +7 -0
  66. package/dist/esm/query/compiler/group-by.js +271 -0
  67. package/dist/esm/query/compiler/group-by.js.map +1 -0
  68. package/dist/esm/query/compiler/index.d.ts +15 -0
  69. package/dist/esm/query/compiler/index.js +181 -0
  70. package/dist/esm/query/compiler/index.js.map +1 -0
  71. package/dist/esm/query/compiler/joins.d.ts +11 -0
  72. package/dist/esm/query/compiler/joins.js +116 -0
  73. package/dist/esm/query/compiler/joins.js.map +1 -0
  74. package/dist/esm/query/compiler/order-by.d.ts +9 -0
  75. package/dist/esm/query/compiler/order-by.js +89 -0
  76. package/dist/esm/query/compiler/order-by.js.map +1 -0
  77. package/dist/esm/query/compiler/select.d.ts +15 -0
  78. package/dist/esm/query/compiler/select.js +57 -0
  79. package/dist/esm/query/compiler/select.js.map +1 -0
  80. package/dist/esm/query/index.d.ts +6 -5
  81. package/dist/esm/query/ir.d.ts +81 -0
  82. package/dist/esm/query/ir.js +57 -0
  83. package/dist/esm/query/ir.js.map +1 -0
  84. package/dist/esm/query/live-query-collection.d.ts +124 -0
  85. package/dist/esm/query/live-query-collection.js +224 -0
  86. package/dist/esm/query/live-query-collection.js.map +1 -0
  87. package/dist/esm/transactions.d.ts +13 -4
  88. package/dist/esm/transactions.js +20 -13
  89. package/dist/esm/transactions.js.map +1 -1
  90. package/dist/esm/types.d.ts +14 -1
  91. package/package.json +3 -4
  92. package/src/collection.ts +152 -129
  93. package/src/index.ts +0 -1
  94. package/src/query/builder/functions.ts +267 -0
  95. package/src/query/builder/index.ts +648 -0
  96. package/src/query/builder/ref-proxy.ts +156 -0
  97. package/src/query/builder/types.ts +278 -0
  98. package/src/query/compiler/evaluators.ts +315 -0
  99. package/src/query/compiler/group-by.ts +428 -0
  100. package/src/query/compiler/index.ts +276 -0
  101. package/src/query/compiler/joins.ts +228 -0
  102. package/src/query/compiler/order-by.ts +139 -0
  103. package/src/query/compiler/select.ts +173 -0
  104. package/src/query/index.ts +64 -5
  105. package/src/query/ir.ts +128 -0
  106. package/src/query/live-query-collection.ts +509 -0
  107. package/src/transactions.ts +34 -19
  108. package/src/types.ts +16 -1
  109. package/dist/cjs/query/compiled-query.cjs +0 -160
  110. package/dist/cjs/query/compiled-query.cjs.map +0 -1
  111. package/dist/cjs/query/compiled-query.d.cts +0 -20
  112. package/dist/cjs/query/evaluators.cjs +0 -161
  113. package/dist/cjs/query/evaluators.cjs.map +0 -1
  114. package/dist/cjs/query/evaluators.d.cts +0 -14
  115. package/dist/cjs/query/extractors.cjs +0 -122
  116. package/dist/cjs/query/extractors.cjs.map +0 -1
  117. package/dist/cjs/query/extractors.d.cts +0 -22
  118. package/dist/cjs/query/functions.cjs +0 -152
  119. package/dist/cjs/query/functions.cjs.map +0 -1
  120. package/dist/cjs/query/functions.d.cts +0 -21
  121. package/dist/cjs/query/group-by.cjs +0 -88
  122. package/dist/cjs/query/group-by.cjs.map +0 -1
  123. package/dist/cjs/query/group-by.d.cts +0 -40
  124. package/dist/cjs/query/joins.cjs +0 -141
  125. package/dist/cjs/query/joins.cjs.map +0 -1
  126. package/dist/cjs/query/joins.d.cts +0 -14
  127. package/dist/cjs/query/order-by.cjs +0 -185
  128. package/dist/cjs/query/order-by.cjs.map +0 -1
  129. package/dist/cjs/query/order-by.d.cts +0 -3
  130. package/dist/cjs/query/pipeline-compiler.cjs +0 -89
  131. package/dist/cjs/query/pipeline-compiler.cjs.map +0 -1
  132. package/dist/cjs/query/pipeline-compiler.d.cts +0 -10
  133. package/dist/cjs/query/query-builder.cjs +0 -307
  134. package/dist/cjs/query/query-builder.cjs.map +0 -1
  135. package/dist/cjs/query/query-builder.d.cts +0 -225
  136. package/dist/cjs/query/schema.d.cts +0 -100
  137. package/dist/cjs/query/select.cjs +0 -130
  138. package/dist/cjs/query/select.cjs.map +0 -1
  139. package/dist/cjs/query/select.d.cts +0 -3
  140. package/dist/cjs/query/types.d.cts +0 -189
  141. package/dist/cjs/query/utils.cjs +0 -154
  142. package/dist/cjs/query/utils.cjs.map +0 -1
  143. package/dist/cjs/query/utils.d.cts +0 -37
  144. package/dist/cjs/utils.cjs +0 -17
  145. package/dist/cjs/utils.cjs.map +0 -1
  146. package/dist/cjs/utils.d.cts +0 -3
  147. package/dist/esm/query/compiled-query.d.ts +0 -20
  148. package/dist/esm/query/compiled-query.js +0 -160
  149. package/dist/esm/query/compiled-query.js.map +0 -1
  150. package/dist/esm/query/evaluators.d.ts +0 -14
  151. package/dist/esm/query/evaluators.js +0 -161
  152. package/dist/esm/query/evaluators.js.map +0 -1
  153. package/dist/esm/query/extractors.d.ts +0 -22
  154. package/dist/esm/query/extractors.js +0 -122
  155. package/dist/esm/query/extractors.js.map +0 -1
  156. package/dist/esm/query/functions.d.ts +0 -21
  157. package/dist/esm/query/functions.js +0 -152
  158. package/dist/esm/query/functions.js.map +0 -1
  159. package/dist/esm/query/group-by.d.ts +0 -40
  160. package/dist/esm/query/group-by.js +0 -88
  161. package/dist/esm/query/group-by.js.map +0 -1
  162. package/dist/esm/query/joins.d.ts +0 -14
  163. package/dist/esm/query/joins.js +0 -141
  164. package/dist/esm/query/joins.js.map +0 -1
  165. package/dist/esm/query/order-by.d.ts +0 -3
  166. package/dist/esm/query/order-by.js +0 -185
  167. package/dist/esm/query/order-by.js.map +0 -1
  168. package/dist/esm/query/pipeline-compiler.d.ts +0 -10
  169. package/dist/esm/query/pipeline-compiler.js +0 -89
  170. package/dist/esm/query/pipeline-compiler.js.map +0 -1
  171. package/dist/esm/query/query-builder.d.ts +0 -225
  172. package/dist/esm/query/query-builder.js +0 -307
  173. package/dist/esm/query/query-builder.js.map +0 -1
  174. package/dist/esm/query/schema.d.ts +0 -100
  175. package/dist/esm/query/select.d.ts +0 -3
  176. package/dist/esm/query/select.js +0 -130
  177. package/dist/esm/query/select.js.map +0 -1
  178. package/dist/esm/query/types.d.ts +0 -189
  179. package/dist/esm/query/utils.d.ts +0 -37
  180. package/dist/esm/query/utils.js +0 -154
  181. package/dist/esm/query/utils.js.map +0 -1
  182. package/dist/esm/utils.d.ts +0 -3
  183. package/dist/esm/utils.js +0 -17
  184. package/dist/esm/utils.js.map +0 -1
  185. package/src/query/compiled-query.ts +0 -234
  186. package/src/query/evaluators.ts +0 -250
  187. package/src/query/extractors.ts +0 -214
  188. package/src/query/functions.ts +0 -297
  189. package/src/query/group-by.ts +0 -139
  190. package/src/query/joins.ts +0 -260
  191. package/src/query/order-by.ts +0 -264
  192. package/src/query/pipeline-compiler.ts +0 -149
  193. package/src/query/query-builder.ts +0 -902
  194. package/src/query/schema.ts +0 -268
  195. package/src/query/select.ts +0 -208
  196. package/src/query/types.ts +0 -418
  197. package/src/query/utils.ts +0 -245
  198. package/src/utils.ts +0 -15
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const ir = require("../ir.cjs");
4
+ const refProxy = require("./ref-proxy.cjs");
5
+ function eq(left, right) {
6
+ return new ir.Func(`eq`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
7
+ }
8
+ function gt(left, right) {
9
+ return new ir.Func(`gt`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
10
+ }
11
+ function gte(left, right) {
12
+ return new ir.Func(`gte`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
13
+ }
14
+ function lt(left, right) {
15
+ return new ir.Func(`lt`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
16
+ }
17
+ function lte(left, right) {
18
+ return new ir.Func(`lte`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
19
+ }
20
+ function and(left, right, ...rest) {
21
+ const allArgs = [left, right, ...rest];
22
+ return new ir.Func(
23
+ `and`,
24
+ allArgs.map((arg) => refProxy.toExpression(arg))
25
+ );
26
+ }
27
+ function or(left, right, ...rest) {
28
+ const allArgs = [left, right, ...rest];
29
+ return new ir.Func(
30
+ `or`,
31
+ allArgs.map((arg) => refProxy.toExpression(arg))
32
+ );
33
+ }
34
+ function not(value) {
35
+ return new ir.Func(`not`, [refProxy.toExpression(value)]);
36
+ }
37
+ function inArray(value, array) {
38
+ return new ir.Func(`in`, [refProxy.toExpression(value), refProxy.toExpression(array)]);
39
+ }
40
+ function like(left, right) {
41
+ return new ir.Func(`like`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
42
+ }
43
+ function ilike(left, right) {
44
+ return new ir.Func(`ilike`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
45
+ }
46
+ function upper(arg) {
47
+ return new ir.Func(`upper`, [refProxy.toExpression(arg)]);
48
+ }
49
+ function lower(arg) {
50
+ return new ir.Func(`lower`, [refProxy.toExpression(arg)]);
51
+ }
52
+ function length(arg) {
53
+ return new ir.Func(`length`, [refProxy.toExpression(arg)]);
54
+ }
55
+ function concat(...args) {
56
+ return new ir.Func(
57
+ `concat`,
58
+ args.map((arg) => refProxy.toExpression(arg))
59
+ );
60
+ }
61
+ function coalesce(...args) {
62
+ return new ir.Func(
63
+ `coalesce`,
64
+ args.map((arg) => refProxy.toExpression(arg))
65
+ );
66
+ }
67
+ function add(left, right) {
68
+ return new ir.Func(`add`, [refProxy.toExpression(left), refProxy.toExpression(right)]);
69
+ }
70
+ function count(arg) {
71
+ return new ir.Aggregate(`count`, [refProxy.toExpression(arg)]);
72
+ }
73
+ function avg(arg) {
74
+ return new ir.Aggregate(`avg`, [refProxy.toExpression(arg)]);
75
+ }
76
+ function sum(arg) {
77
+ return new ir.Aggregate(`sum`, [refProxy.toExpression(arg)]);
78
+ }
79
+ function min(arg) {
80
+ return new ir.Aggregate(`min`, [refProxy.toExpression(arg)]);
81
+ }
82
+ function max(arg) {
83
+ return new ir.Aggregate(`max`, [refProxy.toExpression(arg)]);
84
+ }
85
+ exports.add = add;
86
+ exports.and = and;
87
+ exports.avg = avg;
88
+ exports.coalesce = coalesce;
89
+ exports.concat = concat;
90
+ exports.count = count;
91
+ exports.eq = eq;
92
+ exports.gt = gt;
93
+ exports.gte = gte;
94
+ exports.ilike = ilike;
95
+ exports.inArray = inArray;
96
+ exports.length = length;
97
+ exports.like = like;
98
+ exports.lower = lower;
99
+ exports.lt = lt;
100
+ exports.lte = lte;
101
+ exports.max = max;
102
+ exports.min = min;
103
+ exports.not = not;
104
+ exports.or = or;
105
+ exports.sum = sum;
106
+ exports.upper = upper;
107
+ //# sourceMappingURL=functions.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functions.cjs","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":["Func","toExpression","Aggregate"],"mappings":"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"}
@@ -0,0 +1,38 @@
1
+ import { Aggregate, BasicExpression } from '../ir.cjs';
2
+ import { RefProxy } from './ref-proxy.js';
3
+ type ExpressionLike = BasicExpression | RefProxy<any> | any;
4
+ export declare function eq<T>(left: RefProxy<T>, right: T | RefProxy<T> | BasicExpression<T>): BasicExpression<boolean>;
5
+ export declare function eq<T extends string | number | boolean>(left: T | BasicExpression<T>, right: T | BasicExpression<T>): BasicExpression<boolean>;
6
+ export declare function eq<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>;
7
+ export declare function gt<T>(left: RefProxy<T>, right: T | RefProxy<T> | BasicExpression<T>): BasicExpression<boolean>;
8
+ export declare function gt<T extends string | number>(left: T | BasicExpression<T>, right: T | BasicExpression<T>): BasicExpression<boolean>;
9
+ export declare function gt<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>;
10
+ export declare function gte<T>(left: RefProxy<T>, right: T | RefProxy<T> | BasicExpression<T>): BasicExpression<boolean>;
11
+ export declare function gte<T extends string | number>(left: T | BasicExpression<T>, right: T | BasicExpression<T>): BasicExpression<boolean>;
12
+ export declare function gte<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>;
13
+ export declare function lt<T>(left: RefProxy<T>, right: T | RefProxy<T> | BasicExpression<T>): BasicExpression<boolean>;
14
+ export declare function lt<T extends string | number>(left: T | BasicExpression<T>, right: T | BasicExpression<T>): BasicExpression<boolean>;
15
+ export declare function lt<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>;
16
+ export declare function lte<T>(left: RefProxy<T>, right: T | RefProxy<T> | BasicExpression<T>): BasicExpression<boolean>;
17
+ export declare function lte<T extends string | number>(left: T | BasicExpression<T>, right: T | BasicExpression<T>): BasicExpression<boolean>;
18
+ export declare function lte<T>(left: Aggregate<T>, right: any): BasicExpression<boolean>;
19
+ export declare function and(left: ExpressionLike, right: ExpressionLike): BasicExpression<boolean>;
20
+ export declare function and(left: ExpressionLike, right: ExpressionLike, ...rest: Array<ExpressionLike>): BasicExpression<boolean>;
21
+ export declare function or(left: ExpressionLike, right: ExpressionLike): BasicExpression<boolean>;
22
+ export declare function or(left: ExpressionLike, right: ExpressionLike, ...rest: Array<ExpressionLike>): BasicExpression<boolean>;
23
+ export declare function not(value: ExpressionLike): BasicExpression<boolean>;
24
+ export declare function inArray(value: ExpressionLike, array: ExpressionLike): BasicExpression<boolean>;
25
+ export declare function like(left: RefProxy<string> | RefProxy<string | null> | RefProxy<string | undefined> | string | BasicExpression<string>, right: string | RefProxy<string> | BasicExpression<string>): BasicExpression<boolean>;
26
+ export declare function ilike(left: RefProxy<string> | RefProxy<string | null> | RefProxy<string | undefined> | string | BasicExpression<string>, right: string | RefProxy<string> | BasicExpression<string>): BasicExpression<boolean>;
27
+ export declare function upper(arg: RefProxy<string> | RefProxy<string | undefined> | string | BasicExpression<string>): BasicExpression<string>;
28
+ export declare function lower(arg: RefProxy<string> | RefProxy<string | undefined> | string | BasicExpression<string>): BasicExpression<string>;
29
+ export declare function length(arg: RefProxy<string> | RefProxy<string | undefined> | RefProxy<Array<any>> | RefProxy<Array<any> | undefined> | string | Array<any> | BasicExpression<string> | BasicExpression<Array<any>>): BasicExpression<number>;
30
+ export declare function concat(...args: Array<ExpressionLike>): BasicExpression<string>;
31
+ export declare function coalesce(...args: Array<ExpressionLike>): BasicExpression<any>;
32
+ export declare function add(left: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>, right: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>): BasicExpression<number>;
33
+ export declare function count(arg: ExpressionLike): Aggregate<number>;
34
+ export declare function avg(arg: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>): Aggregate<number>;
35
+ export declare function sum(arg: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>): Aggregate<number>;
36
+ export declare function min(arg: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>): Aggregate<number>;
37
+ export declare function max(arg: RefProxy<number> | RefProxy<number | undefined> | number | BasicExpression<number>): Aggregate<number>;
38
+ export {};
@@ -0,0 +1,499 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const collection = require("../../collection.cjs");
4
+ const ir = require("../ir.cjs");
5
+ const refProxy = require("./ref-proxy.cjs");
6
+ class BaseQueryBuilder {
7
+ constructor(query = {}) {
8
+ this.query = {};
9
+ this.query = { ...query };
10
+ }
11
+ /**
12
+ * Creates a CollectionRef or QueryRef from a source object
13
+ * @param source - An object with a single key-value pair
14
+ * @param context - Context string for error messages (e.g., "from clause", "join clause")
15
+ * @returns A tuple of [alias, ref] where alias is the source key and ref is the created reference
16
+ */
17
+ _createRefForSource(source, context) {
18
+ if (Object.keys(source).length !== 1) {
19
+ throw new Error(`Only one source is allowed in the ${context}`);
20
+ }
21
+ const alias = Object.keys(source)[0];
22
+ const sourceValue = source[alias];
23
+ let ref;
24
+ if (sourceValue instanceof collection.CollectionImpl) {
25
+ ref = new ir.CollectionRef(sourceValue, alias);
26
+ } else if (sourceValue instanceof BaseQueryBuilder) {
27
+ const subQuery = sourceValue._getQuery();
28
+ if (!subQuery.from) {
29
+ throw new Error(
30
+ `A sub query passed to a ${context} must have a from clause itself`
31
+ );
32
+ }
33
+ ref = new ir.QueryRef(subQuery, alias);
34
+ } else {
35
+ throw new Error(`Invalid source`);
36
+ }
37
+ return [alias, ref];
38
+ }
39
+ /**
40
+ * Specify the source table or subquery for the query
41
+ *
42
+ * @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
43
+ * @returns A QueryBuilder with the specified source
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * // Query from a collection
48
+ * query.from({ users: usersCollection })
49
+ *
50
+ * // Query from a subquery
51
+ * const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
52
+ * query.from({ activeUsers })
53
+ * ```
54
+ */
55
+ from(source) {
56
+ const [, from] = this._createRefForSource(source, `from clause`);
57
+ return new BaseQueryBuilder({
58
+ ...this.query,
59
+ from
60
+ });
61
+ }
62
+ /**
63
+ * Join another table or subquery to the current query
64
+ *
65
+ * @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
66
+ * @param onCallback - A function that receives table references and returns the join condition
67
+ * @param type - The type of join: 'inner', 'left', 'right', or 'full' (defaults to 'left')
68
+ * @returns A QueryBuilder with the joined table available
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * // Left join users with posts
73
+ * query
74
+ * .from({ users: usersCollection })
75
+ * .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
76
+ *
77
+ * // Inner join with explicit type
78
+ * query
79
+ * .from({ u: usersCollection })
80
+ * .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId), 'inner')
81
+ * ```
82
+ *
83
+ * // Join with a subquery
84
+ * const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
85
+ * query
86
+ * .from({ activeUsers })
87
+ * .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId))
88
+ */
89
+ join(source, onCallback, type = `left`) {
90
+ const [alias, from] = this._createRefForSource(source, `join clause`);
91
+ const currentAliases = this._getCurrentAliases();
92
+ const newAliases = [...currentAliases, alias];
93
+ const refProxy$1 = refProxy.createRefProxy(newAliases);
94
+ const onExpression = onCallback(refProxy$1);
95
+ let left;
96
+ let right;
97
+ if (onExpression.type === `func` && onExpression.name === `eq` && onExpression.args.length === 2) {
98
+ left = onExpression.args[0];
99
+ right = onExpression.args[1];
100
+ } else {
101
+ throw new Error(`Join condition must be an equality expression`);
102
+ }
103
+ const joinClause = {
104
+ from,
105
+ type,
106
+ left,
107
+ right
108
+ };
109
+ const existingJoins = this.query.join || [];
110
+ return new BaseQueryBuilder({
111
+ ...this.query,
112
+ join: [...existingJoins, joinClause]
113
+ });
114
+ }
115
+ /**
116
+ * Filter rows based on a condition
117
+ *
118
+ * @param callback - A function that receives table references and returns an expression
119
+ * @returns A QueryBuilder with the where condition applied
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * // Simple condition
124
+ * query
125
+ * .from({ users: usersCollection })
126
+ * .where(({users}) => gt(users.age, 18))
127
+ *
128
+ * // Multiple conditions
129
+ * query
130
+ * .from({ users: usersCollection })
131
+ * .where(({users}) => and(
132
+ * gt(users.age, 18),
133
+ * eq(users.active, true)
134
+ * ))
135
+ *
136
+ * // Multiple where calls are ANDed together
137
+ * query
138
+ * .from({ users: usersCollection })
139
+ * .where(({users}) => gt(users.age, 18))
140
+ * .where(({users}) => eq(users.active, true))
141
+ * ```
142
+ */
143
+ where(callback) {
144
+ const aliases = this._getCurrentAliases();
145
+ const refProxy$1 = refProxy.createRefProxy(aliases);
146
+ const expression = callback(refProxy$1);
147
+ const existingWhere = this.query.where || [];
148
+ return new BaseQueryBuilder({
149
+ ...this.query,
150
+ where: [...existingWhere, expression]
151
+ });
152
+ }
153
+ /**
154
+ * Filter grouped rows based on aggregate conditions
155
+ *
156
+ * @param callback - A function that receives table references and returns an expression
157
+ * @returns A QueryBuilder with the having condition applied
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * // Filter groups by count
162
+ * query
163
+ * .from({ posts: postsCollection })
164
+ * .groupBy(({posts}) => posts.userId)
165
+ * .having(({posts}) => gt(count(posts.id), 5))
166
+ *
167
+ * // Filter by average
168
+ * query
169
+ * .from({ orders: ordersCollection })
170
+ * .groupBy(({orders}) => orders.customerId)
171
+ * .having(({orders}) => gt(avg(orders.total), 100))
172
+ *
173
+ * // Multiple having calls are ANDed together
174
+ * query
175
+ * .from({ orders: ordersCollection })
176
+ * .groupBy(({orders}) => orders.customerId)
177
+ * .having(({orders}) => gt(count(orders.id), 5))
178
+ * .having(({orders}) => gt(avg(orders.total), 100))
179
+ * ```
180
+ */
181
+ having(callback) {
182
+ const aliases = this._getCurrentAliases();
183
+ const refProxy$1 = refProxy.createRefProxy(aliases);
184
+ const expression = callback(refProxy$1);
185
+ const existingHaving = this.query.having || [];
186
+ return new BaseQueryBuilder({
187
+ ...this.query,
188
+ having: [...existingHaving, expression]
189
+ });
190
+ }
191
+ /**
192
+ * Select specific columns or computed values from the query
193
+ *
194
+ * @param callback - A function that receives table references and returns an object with selected fields or expressions
195
+ * @returns A QueryBuilder that returns only the selected fields
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * // Select specific columns
200
+ * query
201
+ * .from({ users: usersCollection })
202
+ * .select(({users}) => ({
203
+ * name: users.name,
204
+ * email: users.email
205
+ * }))
206
+ *
207
+ * // Select with computed values
208
+ * query
209
+ * .from({ users: usersCollection })
210
+ * .select(({users}) => ({
211
+ * fullName: concat(users.firstName, ' ', users.lastName),
212
+ * ageInMonths: mul(users.age, 12)
213
+ * }))
214
+ *
215
+ * // Select with aggregates (requires GROUP BY)
216
+ * query
217
+ * .from({ posts: postsCollection })
218
+ * .groupBy(({posts}) => posts.userId)
219
+ * .select(({posts, count}) => ({
220
+ * userId: posts.userId,
221
+ * postCount: count(posts.id)
222
+ * }))
223
+ * ```
224
+ */
225
+ select(callback) {
226
+ const aliases = this._getCurrentAliases();
227
+ const refProxy$1 = refProxy.createRefProxy(aliases);
228
+ const selectObject = callback(refProxy$1);
229
+ const spreadSentinels = refProxy$1.__spreadSentinels;
230
+ const select = {};
231
+ for (const spreadAlias of spreadSentinels) {
232
+ const sentinelKey = `__SPREAD_SENTINEL__${spreadAlias}`;
233
+ select[sentinelKey] = refProxy.toExpression(spreadAlias);
234
+ }
235
+ for (const [key, value] of Object.entries(selectObject)) {
236
+ if (refProxy.isRefProxy(value)) {
237
+ select[key] = refProxy.toExpression(value);
238
+ } else if (typeof value === `object` && `type` in value && (value.type === `agg` || value.type === `func`)) {
239
+ select[key] = value;
240
+ } else {
241
+ select[key] = refProxy.toExpression(value);
242
+ }
243
+ }
244
+ return new BaseQueryBuilder({
245
+ ...this.query,
246
+ select,
247
+ fnSelect: void 0
248
+ // remove the fnSelect clause if it exists
249
+ });
250
+ }
251
+ /**
252
+ * Sort the query results by one or more columns
253
+ *
254
+ * @param callback - A function that receives table references and returns the field to sort by
255
+ * @param direction - Sort direction: 'asc' for ascending, 'desc' for descending (defaults to 'asc')
256
+ * @returns A QueryBuilder with the ordering applied
257
+ *
258
+ * @example
259
+ * ```ts
260
+ * // Sort by a single column
261
+ * query
262
+ * .from({ users: usersCollection })
263
+ * .orderBy(({users}) => users.name)
264
+ *
265
+ * // Sort descending
266
+ * query
267
+ * .from({ users: usersCollection })
268
+ * .orderBy(({users}) => users.createdAt, 'desc')
269
+ *
270
+ * // Multiple sorts (chain orderBy calls)
271
+ * query
272
+ * .from({ users: usersCollection })
273
+ * .orderBy(({users}) => users.lastName)
274
+ * .orderBy(({users}) => users.firstName)
275
+ * ```
276
+ */
277
+ orderBy(callback, direction = `asc`) {
278
+ const aliases = this._getCurrentAliases();
279
+ const refProxy$1 = refProxy.createRefProxy(aliases);
280
+ const result = callback(refProxy$1);
281
+ const orderByClause = {
282
+ expression: refProxy.toExpression(result),
283
+ direction
284
+ };
285
+ const existingOrderBy = this.query.orderBy || [];
286
+ return new BaseQueryBuilder({
287
+ ...this.query,
288
+ orderBy: [...existingOrderBy, orderByClause]
289
+ });
290
+ }
291
+ /**
292
+ * Group rows by one or more columns for aggregation
293
+ *
294
+ * @param callback - A function that receives table references and returns the field(s) to group by
295
+ * @returns A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING)
296
+ *
297
+ * @example
298
+ * ```ts
299
+ * // Group by a single column
300
+ * query
301
+ * .from({ posts: postsCollection })
302
+ * .groupBy(({posts}) => posts.userId)
303
+ * .select(({posts, count}) => ({
304
+ * userId: posts.userId,
305
+ * postCount: count()
306
+ * }))
307
+ *
308
+ * // Group by multiple columns
309
+ * query
310
+ * .from({ sales: salesCollection })
311
+ * .groupBy(({sales}) => [sales.region, sales.category])
312
+ * .select(({sales, sum}) => ({
313
+ * region: sales.region,
314
+ * category: sales.category,
315
+ * totalSales: sum(sales.amount)
316
+ * }))
317
+ * ```
318
+ */
319
+ groupBy(callback) {
320
+ const aliases = this._getCurrentAliases();
321
+ const refProxy$1 = refProxy.createRefProxy(aliases);
322
+ const result = callback(refProxy$1);
323
+ const newExpressions = Array.isArray(result) ? result.map((r) => refProxy.toExpression(r)) : [refProxy.toExpression(result)];
324
+ return new BaseQueryBuilder({
325
+ ...this.query,
326
+ groupBy: newExpressions
327
+ });
328
+ }
329
+ /**
330
+ * Limit the number of rows returned by the query
331
+ * `orderBy` is required for `limit`
332
+ *
333
+ * @param count - Maximum number of rows to return
334
+ * @returns A QueryBuilder with the limit applied
335
+ *
336
+ * @example
337
+ * ```ts
338
+ * // Get top 5 posts by likes
339
+ * query
340
+ * .from({ posts: postsCollection })
341
+ * .orderBy(({posts}) => posts.likes, 'desc')
342
+ * .limit(5)
343
+ * ```
344
+ */
345
+ limit(count) {
346
+ return new BaseQueryBuilder({
347
+ ...this.query,
348
+ limit: count
349
+ });
350
+ }
351
+ /**
352
+ * Skip a number of rows before returning results
353
+ * `orderBy` is required for `offset`
354
+ *
355
+ * @param count - Number of rows to skip
356
+ * @returns A QueryBuilder with the offset applied
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * // Get second page of results
361
+ * query
362
+ * .from({ posts: postsCollection })
363
+ * .orderBy(({posts}) => posts.createdAt, 'desc')
364
+ * .offset(page * pageSize)
365
+ * .limit(pageSize)
366
+ * ```
367
+ */
368
+ offset(count) {
369
+ return new BaseQueryBuilder({
370
+ ...this.query,
371
+ offset: count
372
+ });
373
+ }
374
+ // Helper methods
375
+ _getCurrentAliases() {
376
+ const aliases = [];
377
+ if (this.query.from) {
378
+ aliases.push(this.query.from.alias);
379
+ }
380
+ if (this.query.join) {
381
+ for (const join of this.query.join) {
382
+ aliases.push(join.from.alias);
383
+ }
384
+ }
385
+ return aliases;
386
+ }
387
+ /**
388
+ * Functional variants of the query builder
389
+ * These are imperative function that are called for ery row.
390
+ * Warning: that these cannot be optimized by the query compiler, and may prevent
391
+ * some type of optimizations being possible.
392
+ * @example
393
+ * ```ts
394
+ * q.fn.select((row) => ({
395
+ * name: row.user.name.toUpperCase(),
396
+ * age: row.user.age + 1,
397
+ * }))
398
+ * ```
399
+ */
400
+ get fn() {
401
+ const builder = this;
402
+ return {
403
+ /**
404
+ * Select fields using a function that operates on each row
405
+ * Warning: This cannot be optimized by the query compiler
406
+ *
407
+ * @param callback - A function that receives a row and returns the selected value
408
+ * @returns A QueryBuilder with functional selection applied
409
+ *
410
+ * @example
411
+ * ```ts
412
+ * // Functional select (not optimized)
413
+ * query
414
+ * .from({ users: usersCollection })
415
+ * .fn.select(row => ({
416
+ * name: row.users.name.toUpperCase(),
417
+ * age: row.users.age + 1,
418
+ * }))
419
+ * ```
420
+ */
421
+ select(callback) {
422
+ return new BaseQueryBuilder({
423
+ ...builder.query,
424
+ select: void 0,
425
+ // remove the select clause if it exists
426
+ fnSelect: callback
427
+ });
428
+ },
429
+ /**
430
+ * Filter rows using a function that operates on each row
431
+ * Warning: This cannot be optimized by the query compiler
432
+ *
433
+ * @param callback - A function that receives a row and returns a boolean
434
+ * @returns A QueryBuilder with functional filtering applied
435
+ *
436
+ * @example
437
+ * ```ts
438
+ * // Functional where (not optimized)
439
+ * query
440
+ * .from({ users: usersCollection })
441
+ * .fn.where(row => row.users.name.startsWith('A'))
442
+ * ```
443
+ */
444
+ where(callback) {
445
+ return new BaseQueryBuilder({
446
+ ...builder.query,
447
+ fnWhere: [
448
+ ...builder.query.fnWhere || [],
449
+ callback
450
+ ]
451
+ });
452
+ },
453
+ /**
454
+ * Filter grouped rows using a function that operates on each aggregated row
455
+ * Warning: This cannot be optimized by the query compiler
456
+ *
457
+ * @param callback - A function that receives an aggregated row and returns a boolean
458
+ * @returns A QueryBuilder with functional having filter applied
459
+ *
460
+ * @example
461
+ * ```ts
462
+ * // Functional having (not optimized)
463
+ * query
464
+ * .from({ posts: postsCollection })
465
+ * .groupBy(({posts}) => posts.userId)
466
+ * .fn.having(row => row.count > 5)
467
+ * ```
468
+ */
469
+ having(callback) {
470
+ return new BaseQueryBuilder({
471
+ ...builder.query,
472
+ fnHaving: [
473
+ ...builder.query.fnHaving || [],
474
+ callback
475
+ ]
476
+ });
477
+ }
478
+ };
479
+ }
480
+ _getQuery() {
481
+ if (!this.query.from) {
482
+ throw new Error(`Query must have a from clause`);
483
+ }
484
+ return this.query;
485
+ }
486
+ }
487
+ function buildQuery(fn) {
488
+ const result = fn(new BaseQueryBuilder());
489
+ return getQueryIR(result);
490
+ }
491
+ function getQueryIR(builder) {
492
+ return builder._getQuery();
493
+ }
494
+ const Query = BaseQueryBuilder;
495
+ exports.BaseQueryBuilder = BaseQueryBuilder;
496
+ exports.Query = Query;
497
+ exports.buildQuery = buildQuery;
498
+ exports.getQueryIR = getQueryIR;
499
+ //# sourceMappingURL=index.cjs.map