@tanstack/db 0.0.4 → 0.0.6

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 (102) hide show
  1. package/dist/cjs/collection.cjs +182 -113
  2. package/dist/cjs/collection.cjs.map +1 -1
  3. package/dist/cjs/collection.d.cts +43 -15
  4. package/dist/cjs/index.cjs +1 -0
  5. package/dist/cjs/index.cjs.map +1 -1
  6. package/dist/cjs/proxy.cjs +87 -248
  7. package/dist/cjs/proxy.cjs.map +1 -1
  8. package/dist/cjs/proxy.d.cts +5 -5
  9. package/dist/cjs/query/compiled-query.cjs +23 -14
  10. package/dist/cjs/query/compiled-query.cjs.map +1 -1
  11. package/dist/cjs/query/compiled-query.d.cts +3 -1
  12. package/dist/cjs/query/evaluators.cjs +35 -20
  13. package/dist/cjs/query/evaluators.cjs.map +1 -1
  14. package/dist/cjs/query/evaluators.d.cts +8 -3
  15. package/dist/cjs/query/extractors.cjs +20 -20
  16. package/dist/cjs/query/extractors.cjs.map +1 -1
  17. package/dist/cjs/query/extractors.d.cts +3 -3
  18. package/dist/cjs/query/group-by.cjs +12 -15
  19. package/dist/cjs/query/group-by.cjs.map +1 -1
  20. package/dist/cjs/query/group-by.d.cts +7 -7
  21. package/dist/cjs/query/joins.cjs +41 -55
  22. package/dist/cjs/query/joins.cjs.map +1 -1
  23. package/dist/cjs/query/joins.d.cts +3 -3
  24. package/dist/cjs/query/order-by.cjs +37 -84
  25. package/dist/cjs/query/order-by.cjs.map +1 -1
  26. package/dist/cjs/query/order-by.d.cts +2 -2
  27. package/dist/cjs/query/pipeline-compiler.cjs +13 -18
  28. package/dist/cjs/query/pipeline-compiler.cjs.map +1 -1
  29. package/dist/cjs/query/pipeline-compiler.d.cts +2 -1
  30. package/dist/cjs/query/query-builder.cjs +22 -29
  31. package/dist/cjs/query/query-builder.cjs.map +1 -1
  32. package/dist/cjs/query/query-builder.d.cts +16 -10
  33. package/dist/cjs/query/schema.d.cts +12 -11
  34. package/dist/cjs/query/select.cjs +47 -24
  35. package/dist/cjs/query/select.cjs.map +1 -1
  36. package/dist/cjs/query/select.d.cts +2 -2
  37. package/dist/cjs/query/types.d.cts +1 -0
  38. package/dist/cjs/transactions.cjs +20 -9
  39. package/dist/cjs/transactions.cjs.map +1 -1
  40. package/dist/cjs/types.d.cts +66 -7
  41. package/dist/esm/collection.d.ts +43 -15
  42. package/dist/esm/collection.js +183 -114
  43. package/dist/esm/collection.js.map +1 -1
  44. package/dist/esm/index.js +2 -1
  45. package/dist/esm/proxy.d.ts +5 -5
  46. package/dist/esm/proxy.js +87 -248
  47. package/dist/esm/proxy.js.map +1 -1
  48. package/dist/esm/query/compiled-query.d.ts +3 -1
  49. package/dist/esm/query/compiled-query.js +23 -14
  50. package/dist/esm/query/compiled-query.js.map +1 -1
  51. package/dist/esm/query/evaluators.d.ts +8 -3
  52. package/dist/esm/query/evaluators.js +36 -21
  53. package/dist/esm/query/evaluators.js.map +1 -1
  54. package/dist/esm/query/extractors.d.ts +3 -3
  55. package/dist/esm/query/extractors.js +20 -20
  56. package/dist/esm/query/extractors.js.map +1 -1
  57. package/dist/esm/query/group-by.d.ts +7 -7
  58. package/dist/esm/query/group-by.js +14 -17
  59. package/dist/esm/query/group-by.js.map +1 -1
  60. package/dist/esm/query/joins.d.ts +3 -3
  61. package/dist/esm/query/joins.js +42 -56
  62. package/dist/esm/query/joins.js.map +1 -1
  63. package/dist/esm/query/order-by.d.ts +2 -2
  64. package/dist/esm/query/order-by.js +39 -86
  65. package/dist/esm/query/order-by.js.map +1 -1
  66. package/dist/esm/query/pipeline-compiler.d.ts +2 -1
  67. package/dist/esm/query/pipeline-compiler.js +14 -19
  68. package/dist/esm/query/pipeline-compiler.js.map +1 -1
  69. package/dist/esm/query/query-builder.d.ts +16 -10
  70. package/dist/esm/query/query-builder.js +22 -29
  71. package/dist/esm/query/query-builder.js.map +1 -1
  72. package/dist/esm/query/schema.d.ts +12 -11
  73. package/dist/esm/query/select.d.ts +2 -2
  74. package/dist/esm/query/select.js +48 -25
  75. package/dist/esm/query/select.js.map +1 -1
  76. package/dist/esm/query/types.d.ts +1 -0
  77. package/dist/esm/transactions.js +20 -9
  78. package/dist/esm/transactions.js.map +1 -1
  79. package/dist/esm/types.d.ts +66 -7
  80. package/package.json +2 -2
  81. package/src/collection.ts +286 -146
  82. package/src/proxy.ts +141 -358
  83. package/src/query/compiled-query.ts +30 -15
  84. package/src/query/evaluators.ts +49 -21
  85. package/src/query/extractors.ts +24 -21
  86. package/src/query/group-by.ts +24 -22
  87. package/src/query/joins.ts +88 -75
  88. package/src/query/order-by.ts +56 -106
  89. package/src/query/pipeline-compiler.ts +34 -37
  90. package/src/query/query-builder.ts +49 -46
  91. package/src/query/schema.ts +18 -15
  92. package/src/query/select.ts +68 -33
  93. package/src/query/types.ts +1 -0
  94. package/src/transactions.ts +30 -14
  95. package/src/types.ts +76 -7
  96. package/dist/cjs/query/key-by.cjs +0 -43
  97. package/dist/cjs/query/key-by.cjs.map +0 -1
  98. package/dist/cjs/query/key-by.d.cts +0 -3
  99. package/dist/esm/query/key-by.d.ts +0 -3
  100. package/dist/esm/query/key-by.js +0 -43
  101. package/dist/esm/query/key-by.js.map +0 -1
  102. package/src/query/key-by.ts +0 -61
@@ -1,5 +1,5 @@
1
- import { orderByWithIndex, map, topKWithIndex, orderByWithFractionalIndex, topKWithFractionalIndex, orderBy, topK } from "@electric-sql/d2ts";
2
- import { evaluateOperandOnNestedRow } from "./extractors.js";
1
+ import { orderByWithIndex, map, orderByWithFractionalIndex, orderBy } from "@electric-sql/d2ts";
2
+ import { evaluateOperandOnNamespacedRow } from "./extractors.js";
3
3
  import { isOrderIndexFunctionCall } from "./utils.js";
4
4
  function processOrderBy(resultPipeline, query, mainTableAlias) {
5
5
  let hasOrderIndexColumn = false;
@@ -48,13 +48,11 @@ function processOrderBy(resultPipeline, query, mainTableAlias) {
48
48
  });
49
49
  }
50
50
  }
51
- const valueExtractor = (value) => {
52
- const row = value;
53
- const nestedRow = { [mainTableAlias]: row };
51
+ const valueExtractor = (namespacedRow) => {
54
52
  if (orderByItems.length > 1) {
55
53
  return orderByItems.map((item) => {
56
- const val = evaluateOperandOnNestedRow(
57
- nestedRow,
54
+ const val = evaluateOperandOnNamespacedRow(
55
+ namespacedRow,
58
56
  item.operand,
59
57
  mainTableAlias
60
58
  );
@@ -64,8 +62,8 @@ function processOrderBy(resultPipeline, query, mainTableAlias) {
64
62
  });
65
63
  } else if (orderByItems.length === 1) {
66
64
  const item = orderByItems[0];
67
- const val = evaluateOperandOnNestedRow(
68
- nestedRow,
65
+ const val = evaluateOperandOnNamespacedRow(
66
+ namespacedRow,
69
67
  item.operand,
70
68
  mainTableAlias
71
69
  );
@@ -116,97 +114,52 @@ function processOrderBy(resultPipeline, query, mainTableAlias) {
116
114
  }
117
115
  return a.toString().localeCompare(b.toString());
118
116
  };
119
- let topKComparator;
120
- if (!query.keyBy) {
121
- topKComparator = (a, b) => {
122
- const aValue = valueExtractor(a);
123
- const bValue = valueExtractor(b);
124
- return comparator(aValue, bValue);
125
- };
126
- }
127
117
  if (hasOrderIndexColumn) {
128
118
  if (orderIndexType === `numeric`) {
129
- if (query.keyBy) {
130
- resultPipeline = resultPipeline.pipe(
131
- orderByWithIndex(valueExtractor, {
132
- limit: query.limit,
133
- offset: query.offset,
134
- comparator
135
- }),
136
- map(([key, [value, index]]) => {
137
- const result = {
138
- ...value,
139
- [orderIndexAlias]: index
140
- };
141
- return [key, result];
142
- })
143
- );
144
- } else {
145
- resultPipeline = resultPipeline.pipe(
146
- map((value) => [null, value]),
147
- topKWithIndex(topKComparator, {
148
- limit: query.limit,
149
- offset: query.offset
150
- }),
151
- map(([_, [value, index]]) => {
152
- return {
153
- ...value,
154
- [orderIndexAlias]: index
155
- };
156
- })
157
- );
158
- }
159
- } else {
160
- if (query.keyBy) {
161
- resultPipeline = resultPipeline.pipe(
162
- orderByWithFractionalIndex(valueExtractor, {
163
- limit: query.limit,
164
- offset: query.offset,
165
- comparator
166
- }),
167
- map(([key, [value, index]]) => {
168
- const result = {
169
- ...value,
170
- [orderIndexAlias]: index
171
- };
172
- return [key, result];
173
- })
174
- );
175
- } else {
176
- resultPipeline = resultPipeline.pipe(
177
- map((value) => [null, value]),
178
- topKWithFractionalIndex(topKComparator, {
179
- limit: query.limit,
180
- offset: query.offset
181
- }),
182
- map(([_, [value, index]]) => {
183
- return {
184
- ...value,
185
- [orderIndexAlias]: index
186
- };
187
- })
188
- );
189
- }
190
- }
191
- } else {
192
- if (query.keyBy) {
193
119
  resultPipeline = resultPipeline.pipe(
194
- orderBy(valueExtractor, {
120
+ orderByWithIndex(valueExtractor, {
195
121
  limit: query.limit,
196
122
  offset: query.offset,
197
123
  comparator
124
+ }),
125
+ map(([key, [value, index]]) => {
126
+ const result = {
127
+ ...value,
128
+ [mainTableAlias]: {
129
+ ...value[mainTableAlias],
130
+ [orderIndexAlias]: index
131
+ }
132
+ };
133
+ return [key, result];
198
134
  })
199
135
  );
200
136
  } else {
201
137
  resultPipeline = resultPipeline.pipe(
202
- map((value) => [null, value]),
203
- topK(topKComparator, {
138
+ orderByWithFractionalIndex(valueExtractor, {
204
139
  limit: query.limit,
205
- offset: query.offset
140
+ offset: query.offset,
141
+ comparator
206
142
  }),
207
- map(([_, value]) => value)
143
+ map(([key, [value, index]]) => {
144
+ const result = {
145
+ ...value,
146
+ [mainTableAlias]: {
147
+ ...value[mainTableAlias],
148
+ [orderIndexAlias]: index
149
+ }
150
+ };
151
+ return [key, result];
152
+ })
208
153
  );
209
154
  }
155
+ } else {
156
+ resultPipeline = resultPipeline.pipe(
157
+ orderBy(valueExtractor, {
158
+ limit: query.limit,
159
+ offset: query.offset,
160
+ comparator
161
+ })
162
+ );
210
163
  }
211
164
  return resultPipeline;
212
165
  }
@@ -1 +1 @@
1
- {"version":3,"file":"order-by.js","sources":["../../../src/query/order-by.ts"],"sourcesContent":["import {\n map,\n orderBy,\n orderByWithFractionalIndex,\n orderByWithIndex,\n topK,\n topKWithFractionalIndex,\n topKWithIndex,\n} from \"@electric-sql/d2ts\"\nimport { evaluateOperandOnNestedRow } from \"./extractors\"\nimport { isOrderIndexFunctionCall } from \"./utils\"\nimport type { ConditionOperand, Query } from \"./schema\"\nimport type { IStreamBuilder } from \"@electric-sql/d2ts\"\n\nexport function processOrderBy(\n resultPipeline: IStreamBuilder<\n Record<string, unknown> | [string | number, Record<string, unknown>]\n >,\n query: Query,\n mainTableAlias: string\n) {\n // Check if any column in the SELECT clause is an ORDER_INDEX function call\n let hasOrderIndexColumn = false\n let orderIndexType: `numeric` | `fractional` = `numeric`\n let orderIndexAlias = ``\n\n // Scan the SELECT clause for ORDER_INDEX functions\n for (const item of query.select) {\n if (typeof item === `object`) {\n for (const [alias, expr] of Object.entries(item)) {\n if (typeof expr === `object` && isOrderIndexFunctionCall(expr)) {\n hasOrderIndexColumn = true\n orderIndexAlias = alias\n orderIndexType = getOrderIndexType(expr)\n break\n }\n }\n }\n if (hasOrderIndexColumn) break\n }\n\n // Normalize orderBy to an array of objects\n const orderByItems: Array<{\n operand: ConditionOperand\n direction: `asc` | `desc`\n }> = []\n\n if (typeof query.orderBy === `string`) {\n // Handle string format: '@column'\n orderByItems.push({\n operand: query.orderBy,\n direction: `asc`,\n })\n } else if (Array.isArray(query.orderBy)) {\n // Handle array format: ['@column1', { '@column2': 'desc' }]\n for (const item of query.orderBy) {\n if (typeof item === `string`) {\n orderByItems.push({\n operand: item,\n direction: `asc`,\n })\n } else if (typeof item === `object`) {\n for (const [column, direction] of Object.entries(item)) {\n orderByItems.push({\n operand: column,\n direction: direction as `asc` | `desc`,\n })\n }\n }\n }\n } else if (typeof query.orderBy === `object`) {\n // Handle object format: { '@column': 'desc' }\n for (const [column, direction] of Object.entries(query.orderBy)) {\n orderByItems.push({\n operand: column,\n direction: direction as `asc` | `desc`,\n })\n }\n }\n\n // Create a value extractor function for the orderBy operator\n const valueExtractor = (value: unknown) => {\n const row = value as Record<string, unknown>\n\n // Create a nested row structure for evaluateOperandOnNestedRow\n const nestedRow: Record<string, unknown> = { [mainTableAlias]: row }\n\n // For multiple orderBy columns, create a composite key\n if (orderByItems.length > 1) {\n return orderByItems.map((item) => {\n const val = evaluateOperandOnNestedRow(\n nestedRow,\n item.operand,\n mainTableAlias\n )\n\n // Reverse the value for 'desc' ordering\n return item.direction === `desc` && typeof val === `number`\n ? -val\n : item.direction === `desc` && typeof val === `string`\n ? String.fromCharCode(\n ...[...val].map((c) => 0xffff - c.charCodeAt(0))\n )\n : val\n })\n } else if (orderByItems.length === 1) {\n // For a single orderBy column, use the value directly\n const item = orderByItems[0]\n const val = evaluateOperandOnNestedRow(\n nestedRow,\n item!.operand,\n mainTableAlias\n )\n\n // Reverse the value for 'desc' ordering\n return item!.direction === `desc` && typeof val === `number`\n ? -val\n : item!.direction === `desc` && typeof val === `string`\n ? String.fromCharCode(\n ...[...val].map((c) => 0xffff - c.charCodeAt(0))\n )\n : val\n }\n\n // Default case - no ordering\n return null\n }\n\n const comparator = (a: unknown, b: unknown): number => {\n // if a and b are both numbers compare them directly\n if (typeof a === `number` && typeof b === `number`) {\n return a - b\n }\n // if a and b are both strings, compare them lexicographically\n if (typeof a === `string` && typeof b === `string`) {\n return a.localeCompare(b)\n }\n // if a and b are both booleans, compare them\n if (typeof a === `boolean` && typeof b === `boolean`) {\n return a === b ? 0 : a ? 1 : -1\n }\n // if a and b are both dates, compare them\n if (a instanceof Date && b instanceof Date) {\n return a.getTime() - b.getTime()\n }\n // if a and b are both null, return 0\n if (a === null || b === null) {\n return 0\n }\n\n // if a and b are both arrays, compare them element by element\n if (Array.isArray(a) && Array.isArray(b)) {\n for (let i = 0; i < Math.min(a.length, b.length); i++) {\n // Get the values from the array\n const aVal = a[i]\n const bVal = b[i]\n\n // Compare the values\n let result: number\n\n if (typeof aVal === `boolean` && typeof bVal === `boolean`) {\n // Special handling for booleans - false comes before true\n result = aVal === bVal ? 0 : aVal ? 1 : -1\n } else if (typeof aVal === `number` && typeof bVal === `number`) {\n // Numeric comparison\n result = aVal - bVal\n } else if (typeof aVal === `string` && typeof bVal === `string`) {\n // String comparison\n result = aVal.localeCompare(bVal)\n } else {\n // Default comparison using the general comparator\n result = comparator(aVal, bVal)\n }\n\n if (result !== 0) {\n return result\n }\n }\n // All elements are equal up to the minimum length\n return a.length - b.length\n }\n // if a and b are both null/undefined, return 0\n if (a == null && b == null) {\n return 0\n }\n // Fallback to string comparison for all other cases\n return (a as any).toString().localeCompare((b as any).toString())\n }\n\n let topKComparator: (a: unknown, b: unknown) => number\n if (!query.keyBy) {\n topKComparator = (a, b) => {\n const aValue = valueExtractor(a)\n const bValue = valueExtractor(b)\n return comparator(aValue, bValue)\n }\n }\n\n // Apply the appropriate orderBy operator based on whether an ORDER_INDEX column is requested\n if (hasOrderIndexColumn) {\n if (orderIndexType === `numeric`) {\n if (query.keyBy) {\n // Use orderByWithIndex for numeric indices\n resultPipeline = resultPipeline.pipe(\n orderByWithIndex(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n }),\n map(([key, [value, index]]) => {\n // Add the index to the result\n const result = {\n ...(value as Record<string, unknown>),\n [orderIndexAlias]: index,\n }\n return [key, result]\n })\n )\n } else {\n // Use topKWithIndex for numeric indices\n resultPipeline = resultPipeline.pipe(\n map((value) => [null, value]),\n topKWithIndex(topKComparator!, {\n limit: query.limit,\n offset: query.offset,\n }),\n map(([_, [value, index]]) => {\n // Add the index to the result\n return {\n ...(value as Record<string, unknown>),\n [orderIndexAlias]: index,\n }\n })\n )\n }\n } else {\n if (query.keyBy) {\n // Use orderByWithFractionalIndex for fractional indices\n resultPipeline = resultPipeline.pipe(\n orderByWithFractionalIndex(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n }),\n map(([key, [value, index]]) => {\n // Add the index to the result\n const result = {\n ...(value as Record<string, unknown>),\n [orderIndexAlias]: index,\n }\n return [key, result]\n })\n )\n } else {\n // Use topKWithFractionalIndex for fractional indices\n resultPipeline = resultPipeline.pipe(\n map((value) => [null, value]),\n topKWithFractionalIndex(topKComparator!, {\n limit: query.limit,\n offset: query.offset,\n }),\n map(([_, [value, index]]) => {\n // Add the index to the result\n return {\n ...(value as Record<string, unknown>),\n [orderIndexAlias]: index,\n }\n })\n )\n }\n }\n } else {\n if (query.keyBy) {\n // Use regular orderBy if no index column is requested and but a keyBy is requested\n resultPipeline = resultPipeline.pipe(\n orderBy(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n })\n )\n } else {\n // Use topK if no index column is requested and no keyBy is requested\n resultPipeline = resultPipeline.pipe(\n map((value) => [null, value]),\n topK(topKComparator!, {\n limit: query.limit,\n offset: query.offset,\n }),\n map(([_, value]) => value as Record<string, unknown>)\n )\n }\n }\n\n return resultPipeline\n}\n\n// Helper function to extract the ORDER_INDEX type from a function call\nfunction getOrderIndexType(obj: any): `numeric` | `fractional` {\n if (!isOrderIndexFunctionCall(obj)) {\n throw new Error(`Not an ORDER_INDEX function call`)\n }\n\n const arg = obj[`ORDER_INDEX`]\n if (arg === `numeric` || arg === true || arg === `default`) {\n return `numeric`\n } else if (arg === `fractional`) {\n return `fractional`\n } else {\n throw new Error(`Invalid ORDER_INDEX type: ` + arg)\n }\n}\n"],"names":[],"mappings":";;;AAcgB,SAAA,eACd,gBAGA,OACA,gBACA;AAEA,MAAI,sBAAsB;AAC1B,MAAI,iBAA2C;AAC/C,MAAI,kBAAkB;AAGX,aAAA,QAAQ,MAAM,QAAQ;AAC3B,QAAA,OAAO,SAAS,UAAU;AAC5B,iBAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,IAAI,GAAG;AAChD,YAAI,OAAO,SAAS,YAAY,yBAAyB,IAAI,GAAG;AACxC,gCAAA;AACJ,4BAAA;AAClB,2BAAiB,kBAAkB,IAAI;AACvC;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAEF,QAAI,oBAAqB;AAAA,EAAA;AAI3B,QAAM,eAGD,CAAC;AAEF,MAAA,OAAO,MAAM,YAAY,U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C;AACzD,MAAA,CAAC,yBAAyB,GAAG,GAAG;AAC5B,UAAA,IAAI,MAAM,kCAAkC;AAAA,EAAA;AAG9C,QAAA,MAAM,IAAI,aAAa;AAC7B,MAAI,QAAQ,aAAa,QAAQ,QAAQ,QAAQ,WAAW;AACnD,WAAA;AAAA,EAAA,WACE,QAAQ,cAAc;AACxB,WAAA;AAAA,EAAA,OACF;AACC,UAAA,IAAI,MAAM,+BAA+B,GAAG;AAAA,EAAA;AAEtD;"}
1
+ {"version":3,"file":"order-by.js","sources":["../../../src/query/order-by.ts"],"sourcesContent":["import {\n map,\n orderBy,\n orderByWithFractionalIndex,\n orderByWithIndex,\n} from \"@electric-sql/d2ts\"\nimport { evaluateOperandOnNamespacedRow } from \"./extractors\"\nimport { isOrderIndexFunctionCall } from \"./utils\"\nimport type { ConditionOperand, Query } from \"./schema\"\nimport type {\n KeyedNamespacedRow,\n NamespacedAndKeyedStream,\n NamespacedRow,\n} from \"../types\"\n\nexport function processOrderBy(\n resultPipeline: NamespacedAndKeyedStream,\n query: Query,\n mainTableAlias: string\n) {\n // Check if any column in the SELECT clause is an ORDER_INDEX function call\n let hasOrderIndexColumn = false\n let orderIndexType: `numeric` | `fractional` = `numeric`\n let orderIndexAlias = ``\n\n // Scan the SELECT clause for ORDER_INDEX functions\n // TODO: Select is going to be optional in future - we will automatically add an\n // attribute for the index column\n for (const item of query.select!) {\n if (typeof item === `object`) {\n for (const [alias, expr] of Object.entries(item)) {\n if (typeof expr === `object` && isOrderIndexFunctionCall(expr)) {\n hasOrderIndexColumn = true\n orderIndexAlias = alias\n orderIndexType = getOrderIndexType(expr)\n break\n }\n }\n }\n if (hasOrderIndexColumn) break\n }\n\n // Normalize orderBy to an array of objects\n const orderByItems: Array<{\n operand: ConditionOperand\n direction: `asc` | `desc`\n }> = []\n\n if (typeof query.orderBy === `string`) {\n // Handle string format: '@column'\n orderByItems.push({\n operand: query.orderBy,\n direction: `asc`,\n })\n } else if (Array.isArray(query.orderBy)) {\n // Handle array format: ['@column1', { '@column2': 'desc' }]\n for (const item of query.orderBy) {\n if (typeof item === `string`) {\n orderByItems.push({\n operand: item,\n direction: `asc`,\n })\n } else if (typeof item === `object`) {\n for (const [column, direction] of Object.entries(item)) {\n orderByItems.push({\n operand: column,\n direction: direction as `asc` | `desc`,\n })\n }\n }\n }\n } else if (typeof query.orderBy === `object`) {\n // Handle object format: { '@column': 'desc' }\n for (const [column, direction] of Object.entries(query.orderBy)) {\n orderByItems.push({\n operand: column,\n direction: direction as `asc` | `desc`,\n })\n }\n }\n\n // Create a value extractor function for the orderBy operator\n // const valueExtractor = ([key, namespacedRow]: [\n const valueExtractor = (namespacedRow: NamespacedRow) => {\n // For multiple orderBy columns, create a composite key\n if (orderByItems.length > 1) {\n return orderByItems.map((item) => {\n const val = evaluateOperandOnNamespacedRow(\n namespacedRow,\n item.operand,\n mainTableAlias\n )\n\n // Reverse the value for 'desc' ordering\n return item.direction === `desc` && typeof val === `number`\n ? -val\n : item.direction === `desc` && typeof val === `string`\n ? String.fromCharCode(\n ...[...val].map((c) => 0xffff - c.charCodeAt(0))\n )\n : val\n })\n } else if (orderByItems.length === 1) {\n // For a single orderBy column, use the value directly\n const item = orderByItems[0]\n const val = evaluateOperandOnNamespacedRow(\n namespacedRow,\n item!.operand,\n mainTableAlias\n )\n\n // Reverse the value for 'desc' ordering\n return item!.direction === `desc` && typeof val === `number`\n ? -val\n : item!.direction === `desc` && typeof val === `string`\n ? String.fromCharCode(\n ...[...val].map((c) => 0xffff - c.charCodeAt(0))\n )\n : val\n }\n\n // Default case - no ordering\n return null\n }\n\n const comparator = (a: unknown, b: unknown): number => {\n // if a and b are both numbers compare them directly\n if (typeof a === `number` && typeof b === `number`) {\n return a - b\n }\n // if a and b are both strings, compare them lexicographically\n if (typeof a === `string` && typeof b === `string`) {\n return a.localeCompare(b)\n }\n // if a and b are both booleans, compare them\n if (typeof a === `boolean` && typeof b === `boolean`) {\n return a === b ? 0 : a ? 1 : -1\n }\n // if a and b are both dates, compare them\n if (a instanceof Date && b instanceof Date) {\n return a.getTime() - b.getTime()\n }\n // if a and b are both null, return 0\n if (a === null || b === null) {\n return 0\n }\n\n // if a and b are both arrays, compare them element by element\n if (Array.isArray(a) && Array.isArray(b)) {\n for (let i = 0; i < Math.min(a.length, b.length); i++) {\n // Get the values from the array\n const aVal = a[i]\n const bVal = b[i]\n\n // Compare the values\n let result: number\n\n if (typeof aVal === `boolean` && typeof bVal === `boolean`) {\n // Special handling for booleans - false comes before true\n result = aVal === bVal ? 0 : aVal ? 1 : -1\n } else if (typeof aVal === `number` && typeof bVal === `number`) {\n // Numeric comparison\n result = aVal - bVal\n } else if (typeof aVal === `string` && typeof bVal === `string`) {\n // String comparison\n result = aVal.localeCompare(bVal)\n } else {\n // Default comparison using the general comparator\n result = comparator(aVal, bVal)\n }\n\n if (result !== 0) {\n return result\n }\n }\n // All elements are equal up to the minimum length\n return a.length - b.length\n }\n // if a and b are both null/undefined, return 0\n if (a == null && b == null) {\n return 0\n }\n // Fallback to string comparison for all other cases\n return (a as any).toString().localeCompare((b as any).toString())\n }\n\n // Apply the appropriate orderBy operator based on whether an ORDER_INDEX column is requested\n if (hasOrderIndexColumn) {\n if (orderIndexType === `numeric`) {\n // Use orderByWithIndex for numeric indices\n resultPipeline = resultPipeline.pipe(\n orderByWithIndex(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n }),\n map(([key, [value, index]]) => {\n // Add the index to the result\n // We add this to the main table alias for now\n // TODO: re are going to need to refactor the whole order by pipeline\n const result = {\n ...(value as Record<string, unknown>),\n [mainTableAlias]: {\n ...value[mainTableAlias],\n [orderIndexAlias]: index,\n },\n }\n return [key, result] as KeyedNamespacedRow\n })\n )\n } else {\n // Use orderByWithFractionalIndex for fractional indices\n resultPipeline = resultPipeline.pipe(\n orderByWithFractionalIndex(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n }),\n map(([key, [value, index]]) => {\n // Add the index to the result\n // We add this to the main table alias for now\n // TODO: re are going to need to refactor the whole order by pipeline\n const result = {\n ...(value as Record<string, unknown>),\n [mainTableAlias]: {\n ...value[mainTableAlias],\n [orderIndexAlias]: index,\n },\n }\n return [key, result] as KeyedNamespacedRow\n })\n )\n }\n } else {\n // Use regular orderBy if no index column is requested\n resultPipeline = resultPipeline.pipe(\n orderBy(valueExtractor, {\n limit: query.limit,\n offset: query.offset,\n comparator,\n })\n )\n }\n\n return resultPipeline\n}\n\n// Helper function to extract the ORDER_INDEX type from a function call\nfunction getOrderIndexType(obj: any): `numeric` | `fractional` {\n if (!isOrderIndexFunctionCall(obj)) {\n throw new Error(`Not an ORDER_INDEX function call`)\n }\n\n const arg = obj[`ORDER_INDEX`]\n if (arg === `numeric` || arg === true || arg === `default`) {\n return `numeric`\n } else if (arg === `fractional`) {\n return `fractional`\n } else {\n throw new Error(`Invalid ORDER_INDEX type: ` + arg)\n }\n}\n"],"names":[],"mappings":";;;AAegB,SAAA,eACd,gBACA,OACA,gBACA;AAEA,MAAI,sBAAsB;AAC1B,MAAI,iBAA2C;AAC/C,MAAI,kBAAkB;AAKX,aAAA,QAAQ,MAAM,QAAS;AAC5B,QAAA,OAAO,SAAS,UAAU;AAC5B,iBAAW,CAAC,OAAO,IAAI,KAAK,OAAO,QAAQ,IAAI,GAAG;AAChD,YAAI,OAAO,SAAS,YAAY,yBAAyB,IAAI,GAAG;AACxC,gCAAA;AACJ,4BAAA;AAClB,2BAAiB,kBAAkB,IAAI;AACvC;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAEF,QAAI,oBAAqB;AAAA,EAAA;AAI3B,QAAM,eAGD,CAAC;AAEF,MAAA,OAAO,MAAM,YAAY,UAAU;AAErC,iBAAa,KAAK;AAAA,MAChB,SAAS,MAAM;AAAA,MACf,WAAW;AAAA,IAAA,CACZ;AAAA,EACQ,WAAA,MAAM,QAAQ,MAAM,OAAO,GAAG;AAE5B,eAAA,QAAQ,MAAM,SAAS;AAC5B,UAAA,OAAO,SAAS,UA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MAAI,QAAQ,aAAa,QAAQ,QAAQ,QAAQ,WAAW;AACnD,WAAA;AAAA,EAAA,WACE,QAAQ,cAAc;AACxB,WAAA;AAAA,EAAA,OACF;AACC,UAAA,IAAI,MAAM,+BAA+B,GAAG;AAAA,EAAA;AAEtD;"}
@@ -1,9 +1,10 @@
1
1
  import { Query } from './schema.js';
2
2
  import { IStreamBuilder } from '@electric-sql/d2ts';
3
+ import { KeyedStream } from '../types.js';
3
4
  /**
4
5
  * Compiles a query into a D2 pipeline
5
6
  * @param query The query to compile
6
7
  * @param inputs Mapping of table names to input streams
7
8
  * @returns A stream builder representing the compiled query
8
9
  */
9
- export declare function compileQueryPipeline<T extends IStreamBuilder<unknown>>(query: Query, inputs: Record<string, IStreamBuilder<Record<string, unknown>>>): T;
10
+ export declare function compileQueryPipeline<T extends IStreamBuilder<unknown>>(query: Query, inputs: Record<string, KeyedStream>): T;
@@ -1,9 +1,8 @@
1
1
  import { map, filter } from "@electric-sql/d2ts";
2
- import { evaluateConditionOnNestedRow } from "./evaluators.js";
2
+ import { evaluateWhereOnNamespacedRow } from "./evaluators.js";
3
3
  import { processJoinClause } from "./joins.js";
4
4
  import { processGroupBy } from "./group-by.js";
5
5
  import { processOrderBy } from "./order-by.js";
6
- import { processKeyBy } from "./key-by.js";
7
6
  import { processSelect } from "./select.js";
8
7
  function compileQueryPipeline(query, inputs) {
9
8
  const allInputs = { ...inputs };
@@ -12,9 +11,6 @@ function compileQueryPipeline(query, inputs) {
12
11
  if (!withQuery.as) {
13
12
  throw new Error(`WITH query must have an "as" property`);
14
13
  }
15
- if (withQuery.keyBy !== void 0) {
16
- throw new Error(`WITH query cannot have a "keyBy" property`);
17
- }
18
14
  if (allInputs[withQuery.as]) {
19
15
  throw new Error(`CTE with name "${withQuery.as}" already exists`);
20
16
  }
@@ -34,8 +30,9 @@ function compileQueryPipeline(query, inputs) {
34
30
  }
35
31
  tables[mainTableAlias] = input;
36
32
  let pipeline = input.pipe(
37
- map((row) => {
38
- return { [mainTableAlias]: row };
33
+ map(([key, row]) => {
34
+ const ret = [key, { [mainTableAlias]: row }];
35
+ return ret;
39
36
  })
40
37
  );
41
38
  if (query.join) {
@@ -49,9 +46,9 @@ function compileQueryPipeline(query, inputs) {
49
46
  }
50
47
  if (query.where) {
51
48
  pipeline = pipeline.pipe(
52
- filter((nestedRow) => {
53
- const result = evaluateConditionOnNestedRow(
54
- nestedRow,
49
+ filter(([_key, row]) => {
50
+ const result = evaluateWhereOnNamespacedRow(
51
+ row,
55
52
  query.where,
56
53
  mainTableAlias
57
54
  );
@@ -64,9 +61,9 @@ function compileQueryPipeline(query, inputs) {
64
61
  }
65
62
  if (query.having) {
66
63
  pipeline = pipeline.pipe(
67
- filter((row) => {
68
- const result = evaluateConditionOnNestedRow(
69
- { [mainTableAlias]: row, ...row },
64
+ filter(([_key, row]) => {
65
+ const result = evaluateWhereOnNamespacedRow(
66
+ row,
70
67
  query.having,
71
68
  mainTableAlias
72
69
  );
@@ -74,18 +71,16 @@ function compileQueryPipeline(query, inputs) {
74
71
  })
75
72
  );
76
73
  }
77
- pipeline = processSelect(pipeline, query, mainTableAlias, allInputs);
78
- let resultPipeline = pipeline;
79
- if (query.keyBy) {
80
- resultPipeline = processKeyBy(resultPipeline, query);
81
- }
82
74
  if (query.orderBy) {
83
- resultPipeline = processOrderBy(resultPipeline, query, mainTableAlias);
75
+ pipeline = processOrderBy(pipeline, query, mainTableAlias);
84
76
  } else if (query.limit !== void 0 || query.offset !== void 0) {
85
77
  throw new Error(
86
78
  `LIMIT and OFFSET require an ORDER BY clause to ensure deterministic results`
87
79
  );
88
80
  }
81
+ const resultPipeline = query.select ? processSelect(pipeline, query, mainTableAlias, allInputs) : !query.join && !query.groupBy ? pipeline.pipe(
82
+ map(([key, row]) => [key, row[mainTableAlias]])
83
+ ) : pipeline;
89
84
  return resultPipeline;
90
85
  }
91
86
  export {
@@ -1 +1 @@
1
- {"version":3,"file":"pipeline-compiler.js","sources":["../../../src/query/pipeline-compiler.ts"],"sourcesContent":["import { filter, map } from \"@electric-sql/d2ts\"\nimport { evaluateConditionOnNestedRow } from \"./evaluators.js\"\nimport { processJoinClause } from \"./joins.js\"\nimport { processGroupBy } from \"./group-by.js\"\nimport { processOrderBy } from \"./order-by.js\"\nimport { processKeyBy } from \"./key-by.js\"\nimport { processSelect } from \"./select.js\"\nimport type { Condition, Query } from \"./schema.js\"\nimport type { IStreamBuilder } from \"@electric-sql/d2ts\"\n\n/**\n * Compiles a query into a D2 pipeline\n * @param query The query to compile\n * @param inputs Mapping of table names to input streams\n * @returns A stream builder representing the compiled query\n */\nexport function compileQueryPipeline<T extends IStreamBuilder<unknown>>(\n query: Query,\n inputs: Record<string, IStreamBuilder<Record<string, unknown>>>\n): T {\n // Create a copy of the inputs map to avoid modifying the original\n const allInputs = { ...inputs }\n\n // Process WITH queries if they exist\n if (query.with && query.with.length > 0) {\n // Process each WITH query in order\n for (const withQuery of query.with) {\n // Ensure the WITH query has an alias\n if (!withQuery.as) {\n throw new Error(`WITH query must have an \"as\" property`)\n }\n\n // Ensure the WITH query is not keyed\n if ((withQuery as Query).keyBy !== undefined) {\n throw new Error(`WITH query cannot have a \"keyBy\" property`)\n }\n\n // Check if this CTE name already exists in the inputs\n if (allInputs[withQuery.as]) {\n throw new Error(`CTE with name \"${withQuery.as}\" already exists`)\n }\n\n // Create a new query without the 'with' property to avoid circular references\n const withQueryWithoutWith = { ...withQuery, with: undefined }\n\n // Compile the WITH query using the current set of inputs\n // (which includes previously compiled WITH queries)\n const compiledWithQuery = compileQueryPipeline(\n withQueryWithoutWith,\n allInputs\n )\n\n // Add the compiled query to the inputs map using its alias\n allInputs[withQuery.as] = compiledWithQuery as IStreamBuilder<\n Record<string, unknown>\n >\n }\n }\n\n // Create a map of table aliases to inputs\n const tables: Record<string, IStreamBuilder<Record<string, unknown>>> = {}\n\n // The main table is the one in the FROM clause\n const mainTableAlias = query.as || query.from\n\n // Get the main input from the inputs map (now including CTEs)\n const input = allInputs[query.from]\n if (!input) {\n throw new Error(`Input for table \"${query.from}\" not found in inputs map`)\n }\n\n tables[mainTableAlias] = input\n\n // Prepare the initial pipeline with the main table wrapped in its alias\n let pipeline = input.pipe(\n map((row: unknown) => {\n // Initialize the record with a nested structure\n return { [mainTableAlias]: row } as Record<string, unknown>\n })\n )\n\n // Process JOIN clauses if they exist\n if (query.join) {\n pipeline = processJoinClause(\n pipeline,\n query,\n tables,\n mainTableAlias,\n allInputs\n )\n }\n\n // Process the WHERE clause if it exists\n if (query.where) {\n pipeline = pipeline.pipe(\n filter((nestedRow) => {\n const result = evaluateConditionOnNestedRow(\n nestedRow,\n query.where as Condition,\n mainTableAlias\n )\n return result\n })\n )\n }\n\n // Process the GROUP BY clause if it exists\n if (query.groupBy) {\n pipeline = processGroupBy(pipeline, query, mainTableAlias)\n }\n\n // Process the HAVING clause if it exists\n // This works similarly to WHERE but is applied after any aggregations\n if (query.having) {\n pipeline = pipeline.pipe(\n filter((row) => {\n // For HAVING, we're working with the flattened row that contains both\n // the group by keys and the aggregate results directly\n const result = evaluateConditionOnNestedRow(\n { [mainTableAlias]: row, ...row } as Record<string, unknown>,\n query.having as Condition,\n mainTableAlias\n )\n return result\n })\n )\n }\n\n // Process the SELECT clause - this is where we flatten the structure\n pipeline = processSelect(pipeline, query, mainTableAlias, allInputs)\n\n let resultPipeline: IStreamBuilder<\n Record<string, unknown> | [string | number, Record<string, unknown>]\n > = pipeline\n\n // Process keyBy parameter if it exists\n if (query.keyBy) {\n resultPipeline = processKeyBy(resultPipeline, query)\n }\n\n // Process orderBy parameter if it exists\n if (query.orderBy) {\n resultPipeline = processOrderBy(resultPipeline, query, mainTableAlias)\n } else if (query.limit !== undefined || query.offset !== undefined) {\n // If there's a limit or offset without orderBy, throw an error\n throw new Error(\n `LIMIT and OFFSET require an ORDER BY clause to ensure deterministic results`\n )\n }\n\n return resultPipeline as T\n}\n"],"names":[],"mappings":";;;;;;;AAgBgB,SAAA,qBACd,OACA,QACG;AAEG,QAAA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AAA,EAAA;AAK3D,MAAI,MAAM,QAAQ;AAChB,eAAW,SAAS;AAAA,MAClB,OAAO,CAAC,QAAQ;AAGd,cAAM,SAAS;AAAA,UACb,EAAE,CAAC,cAAc,GAAG,KAAK,GAAG,IAAI;AAAA,UAChC,MAAM;AAAA,UACN;AAAA,QACF;AACO,eAAA;AAAA,MACR,CAAA;AAAA,IACH;AAAA,EAAA;AAIF,aAAW,cAAc,UAAU,OAAO,gBAAgB,SAAS;AAEnE,MAAI,iBAEA;AAGJ,MAAI,MAAM,OAAO;AACE,qBAAA,aAAa,gBAAgB,KAAK;AAAA,EAAA;AAIrD,MAAI,MAAM,SAAS;AACA,qBAAA,eAAe,gBAAgB,OAAO,cAAc;AAAA,EAAA,WAC5D,MAAM,UAAU,UAAa,MAAM,WAAW,QAAW;AAElE,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EAAA;AAGK,SAAA;AACT;"}
1
+ {"version":3,"file":"pipeline-compiler.js","sources":["../../../src/query/pipeline-compiler.ts"],"sourcesContent":["import { filter, map } from \"@electric-sql/d2ts\"\nimport { evaluateWhereOnNamespacedRow } from \"./evaluators.js\"\nimport { processJoinClause } from \"./joins.js\"\nimport { processGroupBy } from \"./group-by.js\"\nimport { processOrderBy } from \"./order-by.js\"\nimport { processSelect } from \"./select.js\"\nimport type { Query } from \"./schema.js\"\nimport type { IStreamBuilder } from \"@electric-sql/d2ts\"\nimport type {\n InputRow,\n KeyedStream,\n NamespacedAndKeyedStream,\n} from \"../types.js\"\n\n/**\n * Compiles a query into a D2 pipeline\n * @param query The query to compile\n * @param inputs Mapping of table names to input streams\n * @returns A stream builder representing the compiled query\n */\nexport function compileQueryPipeline<T extends IStreamBuilder<unknown>>(\n query: Query,\n inputs: Record<string, KeyedStream>\n): T {\n // Create a copy of the inputs map to avoid modifying the original\n const allInputs = { ...inputs }\n\n // Process WITH queries if they exist\n if (query.with && query.with.length > 0) {\n // Process each WITH query in order\n for (const withQuery of query.with) {\n // Ensure the WITH query has an alias\n if (!withQuery.as) {\n throw new Error(`WITH query must have an \"as\" property`)\n }\n\n // Check if this CTE name already exists in the inputs\n if (allInputs[withQuery.as]) {\n throw new Error(`CTE with name \"${withQuery.as}\" already exists`)\n }\n\n // Create a new query without the 'with' property to avoid circular references\n const withQueryWithoutWith = { ...withQuery, with: undefined }\n\n // Compile the WITH query using the current set of inputs\n // (which includes previously compiled WITH queries)\n const compiledWithQuery = compileQueryPipeline(\n withQueryWithoutWith,\n allInputs\n )\n\n // Add the compiled query to the inputs map using its alias\n allInputs[withQuery.as] = compiledWithQuery as KeyedStream\n }\n }\n\n // Create a map of table aliases to inputs\n const tables: Record<string, KeyedStream> = {}\n\n // The main table is the one in the FROM clause\n const mainTableAlias = query.as || query.from\n\n // Get the main input from the inputs map (now including CTEs)\n const input = allInputs[query.from]\n if (!input) {\n throw new Error(`Input for table \"${query.from}\" not found in inputs map`)\n }\n\n tables[mainTableAlias] = input\n\n // Prepare the initial pipeline with the main table wrapped in its alias\n let pipeline: NamespacedAndKeyedStream = input.pipe(\n map(([key, row]) => {\n // Initialize the record with a nested structure\n const ret = [key, { [mainTableAlias]: row }] as [\n string,\n Record<string, typeof row>,\n ]\n return ret\n })\n )\n\n // Process JOIN clauses if they exist\n if (query.join) {\n pipeline = processJoinClause(\n pipeline,\n query,\n tables,\n mainTableAlias,\n allInputs\n )\n }\n\n // Process the WHERE clause if it exists\n if (query.where) {\n pipeline = pipeline.pipe(\n filter(([_key, row]) => {\n const result = evaluateWhereOnNamespacedRow(\n row,\n query.where!,\n mainTableAlias\n )\n return result\n })\n )\n }\n\n // Process the GROUP BY clause if it exists\n if (query.groupBy) {\n pipeline = processGroupBy(pipeline, query, mainTableAlias)\n }\n\n // Process the HAVING clause if it exists\n // This works similarly to WHERE but is applied after any aggregations\n if (query.having) {\n pipeline = pipeline.pipe(\n filter(([_key, row]) => {\n // For HAVING, we're working with the flattened row that contains both\n // the group by keys and the aggregate results directly\n const result = evaluateWhereOnNamespacedRow(\n row,\n query.having!,\n mainTableAlias\n )\n return result\n })\n )\n }\n\n // Process orderBy parameter if it exists\n if (query.orderBy) {\n pipeline = processOrderBy(pipeline, query, mainTableAlias)\n } else if (query.limit !== undefined || query.offset !== undefined) {\n // If there's a limit or offset without orderBy, throw an error\n throw new Error(\n `LIMIT and OFFSET require an ORDER BY clause to ensure deterministic results`\n )\n }\n\n // Process the SELECT clause - this is where we flatten the structure\n const resultPipeline: KeyedStream | NamespacedAndKeyedStream = query.select\n ? processSelect(pipeline, query, mainTableAlias, allInputs)\n : !query.join && !query.groupBy\n ? pipeline.pipe(\n map(([key, row]) => [key, row[mainTableAlias]] as InputRow)\n )\n : pipeline\n return resultPipeline as T\n}\n"],"names":[],"mappings":";;;;;;AAoBgB,SAAA,qBACd,OACA,QACG;AAEG,QAAA,YAAY,EAAE,GAAG,OAAO;AAG9B,MAAI,MAAM,QAAQ,MAAM,KAAK,SAAS,GAAG;AAE5B,eAAA,aAAa,MAAM,MAAM;AAE9B,UAAA,CAAC,UAAU,IAAI;AACX,cAAA,IAAI,MAAM,uCAAuC;AAAA,MAAA;AAIrD,UAAA,UAAU,UAAU,EAAE,GAAG;AAC3B,cAAM,IAAI,MAAM,kBAAkB,UAAU,EAAE,kBAAkB;AAAA,MAAA;AAIlE,YAAM,uBAAuB,EAAE,GAAG,WAAW,MAAM,OAAU;AAI7D,YAAM,oBAAoB;AAAA,QACxB;AAAA,QACA;AAAA,MACF;AAGU,gBAAA,UAAU,EAAE,IAAI;AAAA,IAAA;AAAA,EAC5B;AAIF,QAAM,SAAsC,CAAC;AAGvC,QAAA,iBAAiB,MAAM,MAAM,MAAM;AAGnC,QAAA,QAAQ,UAAU,MAAM,IAAI;AAClC,MAAI,CAAC,OAAO;AACV,UAAM,IAAI,MAAM,oBAAoB,MAAM,IAAI,2BAA2B;AAAA,EAAA;AAG3E,SAAO,cAAc,IAAI;AAGzB,MAAI,WAAqC,MAAM;AAAA,IAC7C,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM;AAEZ,YAAA,MAAM,CAAC,KAAK,EAAE,CAAC,cAAc,GAAG,KAAK;AAIpC,aAAA;AAAA,IACR,CAAA;AAAA,EACH;AAGA,MAAI,MAAM,MAAM;AACH,eAAA;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF;AAAA,EAAA;AAIF,MAAI,MAAM,OAAO;AACf,eAAW,SAAS;AAAA,MAClB,OAAO,CAAC,CAAC,MAAM,GAAG,MAAM;AACtB,cAAM,SAAS;AAAA,UACb;AAAA,UACA,MAAM;AAAA,UACN;AAAA,QACF;AACO,eAAA;AAAA,MACR,CAAA;AAAA,IACH;AAAA,EAAA;AAIF,MAAI,MAAM,SAAS;AACN,eAAA,eAAe,UAAU,OAAO,cAAc;AAAA,EAAA;AAK3D,MAAI,MAAM,QAAQ;AAChB,eAAW,SAAS;AAAA,MAClB,OAAO,CAAC,CAAC,MAAM,GAAG,MAAM;AAGtB,cAAM,SAAS;AAAA,UACb;AAAA,UACA,MAAM;AAAA,UACN;AAAA,QACF;AACO,eAAA;AAAA,MACR,CAAA;AAAA,IACH;AAAA,EAAA;AAIF,MAAI,MAAM,SAAS;AACN,eAAA,eAAe,UAAU,OAAO,cAAc;AAAA,EAAA,WAChD,MAAM,UAAU,UAAa,MAAM,WAAW,QAAW;AAElE,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EAAA;AAIF,QAAM,iBAAyD,MAAM,SACjE,cAAc,UAAU,OAAO,gBAAgB,SAAS,IACxD,CAAC,MAAM,QAAQ,CAAC,MAAM,UACpB,SAAS;AAAA,IACP,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,cAAc,CAAC,CAAa;AAAA,EAAA,IAE5D;AACC,SAAA;AACT;"}
@@ -1,5 +1,5 @@
1
1
  import { Collection } from '../collection.js';
2
- import { Comparator, Condition, Limit, LiteralValue, Offset, OrderBy, Query, Select } from './schema.js';
2
+ import { Comparator, Condition, Limit, LiteralValue, Offset, OrderBy, Query, Select, WhereCallback } from './schema.js';
3
3
  import { Context, Flatten, InferResultTypeFromSelectTuple, Input, InputReference, PropertyReference, PropertyReferenceString, RemoveIndexSignature, Schema } from './types.js';
4
4
  type CollectionRef = {
5
5
  [K: string]: Collection<any>;
@@ -44,8 +44,9 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
44
44
  /**
45
45
  * Specify what columns to select.
46
46
  * Overwrites any previous select clause.
47
+ * Also supports callback functions that receive the row context and return selected data.
47
48
  *
48
- * @param selects The columns to select
49
+ * @param selects The columns to select (can include callbacks)
49
50
  * @returns A new QueryBuilder with the select clause set
50
51
  */
51
52
  select<TSelects extends Array<Select<TContext>>>(this: QueryBuilder<TContext>, ...selects: TSelects): QueryBuilder<Flatten<Omit<TContext, `result`> & {
@@ -59,6 +60,10 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
59
60
  * Add a where clause with a complete condition object.
60
61
  */
61
62
  where(condition: Condition<TContext>): QueryBuilder<TContext>;
63
+ /**
64
+ * Add a where clause with a callback function.
65
+ */
66
+ where(callback: WhereCallback<TContext>): QueryBuilder<TContext>;
62
67
  /**
63
68
  * Add a having clause comparing two values.
64
69
  * For filtering results after they have been grouped.
@@ -69,6 +74,11 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
69
74
  * For filtering results after they have been grouped.
70
75
  */
71
76
  having(condition: Condition<TContext>): QueryBuilder<TContext>;
77
+ /**
78
+ * Add a having clause with a callback function.
79
+ * For filtering results after they have been grouped.
80
+ */
81
+ having(callback: WhereCallback<TContext>): QueryBuilder<TContext>;
72
82
  /**
73
83
  * Add a join clause to the query using a CollectionRef.
74
84
  */
@@ -91,6 +101,7 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
91
101
  schema: TContext[`schema`] & {
92
102
  [K in keyof TCollectionRef & string]: RemoveIndexSignature<(TCollectionRef[keyof TCollectionRef] extends Collection<infer T> ? T : never) & Input>;
93
103
  };
104
+ hasJoin: true;
94
105
  }>>;
95
106
  /**
96
107
  * Add a join clause to the query without specifying an alias.
@@ -118,6 +129,7 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
118
129
  schema: TContext[`schema`] & {
119
130
  [K in T]: RemoveIndexSignature<TContext[`baseSchema`][T]>;
120
131
  };
132
+ hasJoin: true;
121
133
  }>>;
122
134
  /**
123
135
  * Add a join clause to the query with a specified alias.
@@ -145,6 +157,7 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
145
157
  schema: TContext[`schema`] & {
146
158
  [K in TAs]: RemoveIndexSignature<TContext[`baseSchema`][TFrom]>;
147
159
  };
160
+ hasJoin: true;
148
161
  }>>;
149
162
  private joinCollectionRef;
150
163
  private joinInputReference;
@@ -170,13 +183,6 @@ export declare class BaseQueryBuilder<TContext extends Context<Schema>> {
170
183
  * @returns A new QueryBuilder with the offset set
171
184
  */
172
185
  offset(offset: Offset<TContext>): QueryBuilder<TContext>;
173
- /**
174
- * Specify which column(s) to use as keys in the output keyed stream.
175
- *
176
- * @param keyBy The column(s) to use as keys
177
- * @returns A new QueryBuilder with the keyBy clause set
178
- */
179
- keyBy(keyBy: PropertyReference<TContext> | Array<PropertyReference<TContext>>): QueryBuilder<TContext>;
180
186
  /**
181
187
  * Add a groupBy clause to group the results by one or more columns.
182
188
  *
@@ -212,7 +218,7 @@ export declare function queryBuilder<TBaseSchema extends Schema = {}>(): Initial
212
218
  baseSchema: TBaseSchema;
213
219
  schema: {};
214
220
  }>;
215
- export type ResultsFromContext<TContext extends Context<Schema>> = Flatten<TContext[`result`] extends object ? TContext[`result`] : TContext[`result`] extends undefined ? TContext[`schema`] : object>;
221
+ export type ResultsFromContext<TContext extends Context<Schema>> = Flatten<TContext[`result`] extends object ? TContext[`result`] : TContext[`hasJoin`] extends true ? TContext[`schema`] : TContext[`default`] extends keyof TContext[`schema`] ? TContext[`schema`][TContext[`default`]] : never>;
216
222
  export type ResultFromQueryBuilder<TQueryBuilder> = Flatten<TQueryBuilder extends QueryBuilder<infer C> ? C extends {
217
223
  result: infer R;
218
224
  } ? R : never : never>;
@@ -53,8 +53,9 @@ class BaseQueryBuilder {
53
53
  /**
54
54
  * Specify what columns to select.
55
55
  * Overwrites any previous select clause.
56
+ * Also supports callback functions that receive the row context and return selected data.
56
57
  *
57
- * @param selects The columns to select
58
+ * @param selects The columns to select (can include callbacks)
58
59
  * @returns A new QueryBuilder with the select clause set
59
60
  */
60
61
  select(...selects) {
@@ -108,52 +109,56 @@ class BaseQueryBuilder {
108
109
  /**
109
110
  * Add a where clause to filter the results.
110
111
  * Can be called multiple times to add AND conditions.
112
+ * Also supports callback functions that receive the row context.
111
113
  *
112
- * @param leftOrCondition The left operand or complete condition
114
+ * @param leftOrConditionOrCallback The left operand, complete condition, or callback function
113
115
  * @param operator Optional comparison operator
114
116
  * @param right Optional right operand
115
117
  * @returns A new QueryBuilder with the where clause added
116
118
  */
117
- where(leftOrCondition, operator, right) {
119
+ where(leftOrConditionOrCallback, operator, right) {
118
120
  const newBuilder = new BaseQueryBuilder();
119
121
  Object.assign(newBuilder.query, this.query);
120
122
  let condition;
121
- if (operator !== void 0 && right !== void 0) {
122
- condition = [leftOrCondition, operator, right];
123
+ if (typeof leftOrConditionOrCallback === `function`) {
124
+ condition = leftOrConditionOrCallback;
125
+ } else if (operator !== void 0 && right !== void 0) {
126
+ condition = [leftOrConditionOrCallback, operator, right];
123
127
  } else {
124
- condition = leftOrCondition;
128
+ condition = leftOrConditionOrCallback;
125
129
  }
126
130
  if (!newBuilder.query.where) {
127
- newBuilder.query.where = condition;
131
+ newBuilder.query.where = [condition];
128
132
  } else {
129
- const andArray = [newBuilder.query.where, `and`, condition];
130
- newBuilder.query.where = andArray;
133
+ newBuilder.query.where = [...newBuilder.query.where, condition];
131
134
  }
132
135
  return newBuilder;
133
136
  }
134
137
  /**
135
138
  * Add a having clause to filter the grouped results.
136
139
  * Can be called multiple times to add AND conditions.
140
+ * Also supports callback functions that receive the row context.
137
141
  *
138
- * @param leftOrCondition The left operand or complete condition
142
+ * @param leftOrConditionOrCallback The left operand, complete condition, or callback function
139
143
  * @param operator Optional comparison operator
140
144
  * @param right Optional right operand
141
145
  * @returns A new QueryBuilder with the having clause added
142
146
  */
143
- having(leftOrCondition, operator, right) {
147
+ having(leftOrConditionOrCallback, operator, right) {
144
148
  const newBuilder = new BaseQueryBuilder();
145
149
  Object.assign(newBuilder.query, this.query);
146
150
  let condition;
147
- if (operator !== void 0 && right !== void 0) {
148
- condition = [leftOrCondition, operator, right];
151
+ if (typeof leftOrConditionOrCallback === `function`) {
152
+ condition = leftOrConditionOrCallback;
153
+ } else if (operator !== void 0 && right !== void 0) {
154
+ condition = [leftOrConditionOrCallback, operator, right];
149
155
  } else {
150
- condition = leftOrCondition;
156
+ condition = leftOrConditionOrCallback;
151
157
  }
152
158
  if (!newBuilder.query.having) {
153
- newBuilder.query.having = condition;
159
+ newBuilder.query.having = [condition];
154
160
  } else {
155
- const andArray = [newBuilder.query.having, `and`, condition];
156
- newBuilder.query.having = andArray;
161
+ newBuilder.query.having = [...newBuilder.query.having, condition];
157
162
  }
158
163
  return newBuilder;
159
164
  }
@@ -249,18 +254,6 @@ class BaseQueryBuilder {
249
254
  newBuilder.query.offset = offset;
250
255
  return newBuilder;
251
256
  }
252
- /**
253
- * Specify which column(s) to use as keys in the output keyed stream.
254
- *
255
- * @param keyBy The column(s) to use as keys
256
- * @returns A new QueryBuilder with the keyBy clause set
257
- */
258
- keyBy(keyBy) {
259
- const newBuilder = new BaseQueryBuilder();
260
- Object.assign(newBuilder.query, this.query);
261
- newBuilder.query.keyBy = keyBy;
262
- return newBuilder;
263
- }
264
257
  /**
265
258
  * Add a groupBy clause to group the results by one or more columns.
266
259
  *