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