metal-orm 1.0.62 → 1.0.64

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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { ColumnDef } from '../../schema/column-types.js';
4
4
  import { columnOperand, valueToOperand } from '../ast/expression-builders.js';
5
- import { FunctionNode, OperandNode, isOperandNode } from '../ast/expression.js';
5
+ import { FunctionNode, OperandNode, isOperandNode, TypedExpression, asType } from '../ast/expression.js';
6
6
 
7
7
  type OperandInput = OperandNode | ColumnDef | string | number | boolean | null;
8
8
 
@@ -22,17 +22,49 @@ const fn = (key: string, args: OperandInput[]): FunctionNode => ({
22
22
  args: args.map(toOperand)
23
23
  });
24
24
 
25
- export const jsonLength = (target: OperandInput, path?: OperandInput): FunctionNode =>
26
- path === undefined ? fn('JSON_LENGTH', [target]) : fn('JSON_LENGTH', [target, path]);
25
+ const nfn = (key: string, args: OperandInput[]): TypedExpression<number> => asType<number>(fn(key, args));
26
+ const afn = <T = any>(key: string, args: OperandInput[]): TypedExpression<T> => asType<T>(fn(key, args));
27
27
 
28
- export const jsonSet = (target: OperandInput, path: OperandInput, value: OperandInput): FunctionNode =>
29
- fn('JSON_SET', [target, path, value]);
28
+ /**
29
+ * Returns the number of elements in a JSON array or object.
30
+ *
31
+ * @param target - JSON column or value.
32
+ * @param path - Optional JSON path.
33
+ * @returns A `TypedExpression<number>` representing the `JSON_LENGTH` SQL function.
34
+ */
35
+ export const jsonLength = (target: OperandInput, path?: OperandInput): TypedExpression<number> =>
36
+ path === undefined ? nfn('JSON_LENGTH', [target]) : nfn('JSON_LENGTH', [target, path]);
30
37
 
31
- export const jsonArrayAgg = (value: OperandInput): FunctionNode => fn('JSON_ARRAYAGG', [value]);
38
+ /**
39
+ * Inserts or updates a value in a JSON document.
40
+ *
41
+ * @param target - JSON column or value.
42
+ * @param path - JSON path to set.
43
+ * @param value - Value to set.
44
+ * @returns A `TypedExpression<any>` representing the `JSON_SET` SQL function.
45
+ */
46
+ export const jsonSet = (target: OperandInput, path: OperandInput, value: OperandInput): TypedExpression<any> =>
47
+ afn('JSON_SET', [target, path, value]);
32
48
 
49
+ /**
50
+ * Aggregates values into a JSON array.
51
+ *
52
+ * @param value - Column or expression to aggregate.
53
+ * @returns A `TypedExpression<any[]>` representing the `JSON_ARRAYAGG` SQL function.
54
+ */
55
+ export const jsonArrayAgg = (value: OperandInput): TypedExpression<any[]> => afn<any[]>('JSON_ARRAYAGG', [value]);
56
+
57
+ /**
58
+ * Checks if a JSON document contains a specific piece of data.
59
+ *
60
+ * @param target - JSON column or value.
61
+ * @param candidate - Data to look for.
62
+ * @param path - Optional JSON path to search within.
63
+ * @returns A `TypedExpression<boolean>` representing the `JSON_CONTAINS` SQL function.
64
+ */
33
65
  export const jsonContains = (
34
66
  target: OperandInput,
35
67
  candidate: OperandInput,
36
68
  path?: OperandInput
37
- ): FunctionNode =>
38
- path === undefined ? fn('JSON_CONTAINS', [target, candidate]) : fn('JSON_CONTAINS', [target, candidate, path]);
69
+ ): TypedExpression<boolean> =>
70
+ path === undefined ? afn<boolean>('JSON_CONTAINS', [target, candidate]) : afn<boolean>('JSON_CONTAINS', [target, candidate, path]);
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { ColumnDef } from '../../schema/column-types.js';
4
4
  import { columnOperand, valueToOperand } from '../ast/expression-builders.js';
5
- import { FunctionNode, OperandNode, isOperandNode } from '../ast/expression.js';
5
+ import { FunctionNode, OperandNode, isOperandNode, TypedExpression, asType } from '../ast/expression.js';
6
6
 
7
7
  type OperandInput = OperandNode | ColumnDef | string | number | boolean | null;
8
8
 
@@ -22,239 +22,276 @@ const fn = (key: string, args: OperandInput[]): FunctionNode => ({
22
22
  args: args.map(toOperand)
23
23
  });
24
24
 
25
+ const nfn = (key: string, args: OperandInput[]): TypedExpression<number> => asType<number>(fn(key, args));
26
+
25
27
  // ----------------------
26
28
  // Helper Functions
27
29
  // ----------------------
28
30
 
29
31
  /**
30
32
  * Returns the absolute value of a number.
31
- * @param value - The numeric value.
32
- * @returns A FunctionNode representing the ABS SQL function.
33
+ *
34
+ * @param value - The numeric value or column.
35
+ * @returns A `TypedExpression<number>` representing the `ABS` SQL function.
36
+ *
37
+ * @example
38
+ * abs(transactions.amount);
33
39
  */
34
- export const abs = (value: OperandInput): FunctionNode => fn('ABS', [value]);
40
+ export const abs = (value: OperandInput): TypedExpression<number> => nfn('ABS', [value]);
35
41
 
36
42
  /**
37
43
  * Returns the arccosine (inverse cosine) of a number.
38
- * @param value - The numeric value.
39
- * @returns A FunctionNode representing the ACOS SQL function.
44
+ *
45
+ * @param value - The numeric value or column.
46
+ * @returns A `TypedExpression<number>` representing the `ACOS` SQL function.
40
47
  */
41
- export const acos = (value: OperandInput): FunctionNode => fn('ACOS', [value]);
48
+ export const acos = (value: OperandInput): TypedExpression<number> => nfn('ACOS', [value]);
42
49
 
43
50
  /**
44
51
  * Returns the arcsine (inverse sine) of a number.
45
- * @param value - The numeric value.
46
- * @returns A FunctionNode representing the ASIN SQL function.
52
+ *
53
+ * @param value - The numeric value or column.
54
+ * @returns A `TypedExpression<number>` representing the `ASIN` SQL function.
47
55
  */
48
- export const asin = (value: OperandInput): FunctionNode => fn('ASIN', [value]);
56
+ export const asin = (value: OperandInput): TypedExpression<number> => nfn('ASIN', [value]);
49
57
 
50
58
  /**
51
59
  * Returns the arctangent (inverse tangent) of a number.
52
- * @param value - The numeric value.
53
- * @returns A FunctionNode representing the ATAN SQL function.
60
+ *
61
+ * @param value - The numeric value or column.
62
+ * @returns A `TypedExpression<number>` representing the `ATAN` SQL function.
54
63
  */
55
- export const atan = (value: OperandInput): FunctionNode => fn('ATAN', [value]);
64
+ export const atan = (value: OperandInput): TypedExpression<number> => nfn('ATAN', [value]);
56
65
 
57
66
  /**
58
67
  * Returns the arctangent of the two arguments.
68
+ *
59
69
  * @param y - The y-coordinate.
60
70
  * @param x - The x-coordinate.
61
- * @returns A FunctionNode representing the ATAN2 SQL function.
71
+ * @returns A `TypedExpression<number>` representing the `ATAN2` SQL function.
62
72
  */
63
- export const atan2 = (y: OperandInput, x: OperandInput): FunctionNode => fn('ATAN2', [y, x]);
73
+ export const atan2 = (y: OperandInput, x: OperandInput): TypedExpression<number> => nfn('ATAN2', [y, x]);
64
74
 
65
75
  /**
66
76
  * Returns the smallest integer greater than or equal to a number.
67
- * @param value - The numeric value.
68
- * @returns A FunctionNode representing the CEIL SQL function.
77
+ *
78
+ * @param value - The numeric value or column.
79
+ * @returns A `TypedExpression<number>` representing the `CEIL` SQL function.
69
80
  */
70
- export const ceil = (value: OperandInput): FunctionNode => fn('CEIL', [value]);
81
+ export const ceil = (value: OperandInput): TypedExpression<number> => nfn('CEIL', [value]);
71
82
 
72
83
  /**
73
84
  * Alias for ceil. Returns the smallest integer greater than or equal to a number.
74
- * @param value - The numeric value.
75
- * @returns A FunctionNode representing the CEILING SQL function.
85
+ *
86
+ * @param value - The numeric value or column.
87
+ * @returns A `TypedExpression<number>` representing the `CEILING` SQL function.
76
88
  */
77
- export const ceiling = (value: OperandInput): FunctionNode => fn('CEILING', [value]);
89
+ export const ceiling = (value: OperandInput): TypedExpression<number> => nfn('CEILING', [value]);
78
90
 
79
91
  /**
80
92
  * Returns the cosine of a number (in radians).
93
+ *
81
94
  * @param value - The numeric value in radians.
82
- * @returns A FunctionNode representing the COS SQL function.
95
+ * @returns A `TypedExpression<number>` representing the `COS` SQL function.
83
96
  */
84
- export const cos = (value: OperandInput): FunctionNode => fn('COS', [value]);
97
+ export const cos = (value: OperandInput): TypedExpression<number> => nfn('COS', [value]);
85
98
 
86
99
  /**
87
100
  * Returns the cotangent of a number.
101
+ *
88
102
  * @param value - The numeric value.
89
- * @returns A FunctionNode representing the COT SQL function.
103
+ * @returns A `TypedExpression<number>` representing the `COT` SQL function.
90
104
  */
91
- export const cot = (value: OperandInput): FunctionNode => fn('COT', [value]);
105
+ export const cot = (value: OperandInput): TypedExpression<number> => nfn('COT', [value]);
92
106
 
93
107
  /**
94
108
  * Converts radians to degrees.
109
+ *
95
110
  * @param value - The angle in radians.
96
- * @returns A FunctionNode representing the DEGREES SQL function.
111
+ * @returns A `TypedExpression<number>` representing the `DEGREES` SQL function.
97
112
  */
98
- export const degrees = (value: OperandInput): FunctionNode => fn('DEGREES', [value]);
113
+ export const degrees = (value: OperandInput): TypedExpression<number> => nfn('DEGREES', [value]);
99
114
 
100
115
  /**
101
116
  * Returns e raised to the power of the argument.
117
+ *
102
118
  * @param value - The exponent.
103
- * @returns A FunctionNode representing the EXP SQL function.
119
+ * @returns A `TypedExpression<number>` representing the `EXP` SQL function.
104
120
  */
105
- export const exp = (value: OperandInput): FunctionNode => fn('EXP', [value]);
121
+ export const exp = (value: OperandInput): TypedExpression<number> => nfn('EXP', [value]);
106
122
 
107
123
  /**
108
124
  * Returns the largest integer less than or equal to a number.
125
+ *
109
126
  * @param value - The numeric value.
110
- * @returns A FunctionNode representing the FLOOR SQL function.
127
+ * @returns A `TypedExpression<number>` representing the `FLOOR` SQL function.
111
128
  */
112
- export const floor = (value: OperandInput): FunctionNode => fn('FLOOR', [value]);
129
+ export const floor = (value: OperandInput): TypedExpression<number> => nfn('FLOOR', [value]);
113
130
 
114
131
  /**
115
132
  * Returns the natural logarithm (base e) of a number.
133
+ *
116
134
  * @param value - The numeric value.
117
- * @returns A FunctionNode representing the LN SQL function.
135
+ * @returns A `TypedExpression<number>` representing the `LN` SQL function.
118
136
  */
119
- export const ln = (value: OperandInput): FunctionNode => fn('LN', [value]);
137
+ export const ln = (value: OperandInput): TypedExpression<number> => nfn('LN', [value]);
120
138
 
121
139
  /**
122
140
  * Returns the base-10 logarithm of a number.
141
+ *
123
142
  * @param value - The numeric value.
124
- * @returns A FunctionNode representing the LOG SQL function.
143
+ * @returns A `TypedExpression<number>` representing the `LOG` SQL function.
125
144
  */
126
- export const log = (value: OperandInput): FunctionNode => fn('LOG', [value]);
145
+ export const log = (value: OperandInput): TypedExpression<number> => nfn('LOG', [value]);
127
146
 
128
147
  /**
129
148
  * Returns the base-10 logarithm of a number.
149
+ *
130
150
  * @param value - The numeric value.
131
- * @returns A FunctionNode representing the LOG10 SQL function.
151
+ * @returns A `TypedExpression<number>` representing the `LOG10` SQL function.
132
152
  */
133
- export const log10 = (value: OperandInput): FunctionNode => fn('LOG10', [value]);
153
+ export const log10 = (value: OperandInput): TypedExpression<number> => nfn('LOG10', [value]);
134
154
 
135
155
  /**
136
156
  * Returns the logarithm of a number for a specific base.
157
+ *
137
158
  * @param base - The base of the logarithm.
138
159
  * @param value - The numeric value.
139
- * @returns A FunctionNode representing the LOG_BASE SQL function.
160
+ * @returns A `TypedExpression<number>` representing the `LOG_BASE` SQL function.
140
161
  */
141
- export const logBase = (base: OperandInput, value: OperandInput): FunctionNode => fn('LOG_BASE', [base, value]);
162
+ export const logBase = (base: OperandInput, value: OperandInput): TypedExpression<number> => nfn('LOG_BASE', [base, value]);
142
163
 
143
164
  /**
144
165
  * Returns the remainder of dividing x by y.
166
+ *
145
167
  * @param x - The dividend.
146
168
  * @param y - The divisor.
147
- * @returns A FunctionNode representing the MOD SQL function.
169
+ * @returns A `TypedExpression<number>` representing the `MOD` SQL function.
148
170
  */
149
- export const mod = (x: OperandInput, y: OperandInput): FunctionNode => fn('MOD', [x, y]);
171
+ export const mod = (x: OperandInput, y: OperandInput): TypedExpression<number> => nfn('MOD', [x, y]);
150
172
 
151
173
  /**
152
174
  * Returns the value of PI (approximately 3.14159...).
153
- * @returns A FunctionNode representing the PI SQL function.
175
+ *
176
+ * @returns A `TypedExpression<number>` representing the `PI` SQL function.
154
177
  */
155
- export const pi = (): FunctionNode => fn('PI', []);
178
+ export const pi = (): TypedExpression<number> => nfn('PI', []);
156
179
 
157
180
  /**
158
181
  * Returns x raised to the power of y.
182
+ *
159
183
  * @param x - The base.
160
184
  * @param y - The exponent.
161
- * @returns A FunctionNode representing the POWER SQL function.
185
+ * @returns A `TypedExpression<number>` representing the `POWER` SQL function.
162
186
  */
163
- export const power = (x: OperandInput, y: OperandInput): FunctionNode => fn('POWER', [x, y]);
187
+ export const power = (x: OperandInput, y: OperandInput): TypedExpression<number> => nfn('POWER', [x, y]);
164
188
 
165
189
  /**
166
190
  * Alias for power. Returns x raised to the power of y.
191
+ *
167
192
  * @param x - The base.
168
193
  * @param y - The exponent.
169
- * @returns A FunctionNode representing the POW SQL function.
194
+ * @returns A `TypedExpression<number>` representing the `POW` SQL function.
170
195
  */
171
- export const pow = (x: OperandInput, y: OperandInput): FunctionNode => fn('POW', [x, y]);
196
+ export const pow = (x: OperandInput, y: OperandInput): TypedExpression<number> => nfn('POW', [x, y]);
172
197
 
173
198
  /**
174
199
  * Converts degrees to radians.
200
+ *
175
201
  * @param value - The angle in degrees.
176
- * @returns A FunctionNode representing the RADIANS SQL function.
202
+ * @returns A `TypedExpression<number>` representing the `RADIANS` SQL function.
177
203
  */
178
- export const radians = (value: OperandInput): FunctionNode => fn('RADIANS', [value]);
204
+ export const radians = (value: OperandInput): TypedExpression<number> => nfn('RADIANS', [value]);
179
205
 
180
206
  /**
181
207
  * Returns a random number between 0 and 1.
182
- * @returns A FunctionNode representing the RANDOM SQL function.
208
+ *
209
+ * @returns A `TypedExpression<number>` representing the `RANDOM` SQL function.
183
210
  */
184
- export const random = (): FunctionNode => fn('RANDOM', []);
211
+ export const random = (): TypedExpression<number> => nfn('RANDOM', []);
185
212
 
186
213
  /**
187
214
  * Alias for random. Returns a random number between 0 and 1.
188
- * @returns A FunctionNode representing the RAND SQL function.
215
+ *
216
+ * @returns A `TypedExpression<number>` representing the `RAND` SQL function.
189
217
  */
190
- export const rand = (): FunctionNode => fn('RAND', []);
218
+ export const rand = (): TypedExpression<number> => nfn('RAND', []);
191
219
 
192
220
  /**
193
221
  * Rounds a number to a specified number of decimal places.
222
+ *
194
223
  * @param value - The numeric value to round.
195
- * @param decimals - The number of decimal places (optional).
196
- * @returns A FunctionNode representing the ROUND SQL function.
224
+ * @param decimals - Optional number of decimal places.
225
+ * @returns A `TypedExpression<number>` representing the `ROUND` SQL function.
197
226
  */
198
- export const round = (value: OperandInput, decimals?: OperandInput): FunctionNode =>
199
- decimals === undefined ? fn('ROUND', [value]) : fn('ROUND', [value, decimals]);
227
+ export const round = (value: OperandInput, decimals?: OperandInput): TypedExpression<number> =>
228
+ decimals === undefined ? nfn('ROUND', [value]) : nfn('ROUND', [value, decimals]);
200
229
 
201
230
  /**
202
231
  * Returns the sign of a number (-1 for negative, 0 for zero, 1 for positive).
232
+ *
203
233
  * @param value - The numeric value.
204
- * @returns A FunctionNode representing the SIGN SQL function.
234
+ * @returns A `TypedExpression<number>` representing the `SIGN` SQL function.
205
235
  */
206
- export const sign = (value: OperandInput): FunctionNode => fn('SIGN', [value]);
236
+ export const sign = (value: OperandInput): TypedExpression<number> => nfn('SIGN', [value]);
207
237
 
208
238
  /**
209
239
  * Returns the sine of a number (in radians).
240
+ *
210
241
  * @param value - The numeric value in radians.
211
- * @returns A FunctionNode representing the SIN SQL function.
242
+ * @returns A `TypedExpression<number>` representing the `SIN` SQL function.
212
243
  */
213
- export const sin = (value: OperandInput): FunctionNode => fn('SIN', [value]);
244
+ export const sin = (value: OperandInput): TypedExpression<number> => nfn('SIN', [value]);
214
245
 
215
246
  /**
216
247
  * Returns the square root of a number.
217
- * @param value - The numeric value.
218
- * @returns A FunctionNode representing the SQRT SQL function.
248
+ *
249
+ * @param value - The numeric value or column.
250
+ * @returns A `TypedExpression<number>` representing the `SQRT` SQL function.
219
251
  */
220
- export const sqrt = (value: OperandInput): FunctionNode => fn('SQRT', [value]);
252
+ export const sqrt = (value: OperandInput): TypedExpression<number> => nfn('SQRT', [value]);
221
253
 
222
254
  /**
223
255
  * Returns the tangent of a number (in radians).
256
+ *
224
257
  * @param value - The numeric value in radians.
225
- * @returns A FunctionNode representing the TAN SQL function.
258
+ * @returns A `TypedExpression<number>` representing the `TAN` SQL function.
226
259
  */
227
- export const tan = (value: OperandInput): FunctionNode => fn('TAN', [value]);
260
+ export const tan = (value: OperandInput): TypedExpression<number> => nfn('TAN', [value]);
228
261
 
229
262
  /**
230
263
  * Truncates a number to a specified number of decimal places without rounding.
264
+ *
231
265
  * @param value - The numeric value to truncate.
232
- * @param decimals - The number of decimal places (optional).
233
- * @returns A FunctionNode representing the TRUNC SQL function.
266
+ * @param decimals - Optional number of decimal places.
267
+ * @returns A `TypedExpression<number>` representing the `TRUNC` SQL function.
234
268
  */
235
- export const trunc = (value: OperandInput, decimals?: OperandInput): FunctionNode =>
236
- decimals === undefined ? fn('TRUNC', [value]) : fn('TRUNC', [value, decimals]);
269
+ export const trunc = (value: OperandInput, decimals?: OperandInput): TypedExpression<number> =>
270
+ decimals === undefined ? nfn('TRUNC', [value]) : nfn('TRUNC', [value, decimals]);
237
271
 
238
272
  /**
239
273
  * Alias for trunc. Truncates a number to a specified number of decimal places without rounding.
274
+ *
240
275
  * @param value - The numeric value to truncate.
241
276
  * @param decimals - The number of decimal places.
242
- * @returns A FunctionNode representing the TRUNCATE SQL function.
277
+ * @returns A `TypedExpression<number>` representing the `TRUNCATE` SQL function.
243
278
  */
244
- export const truncate = (value: OperandInput, decimals: OperandInput): FunctionNode =>
245
- fn('TRUNCATE', [value, decimals]);
279
+ export const truncate = (value: OperandInput, decimals: OperandInput): TypedExpression<number> =>
280
+ nfn('TRUNCATE', [value, decimals]);
246
281
 
247
282
  /**
248
283
  * Returns the base-2 logarithm of a number.
249
- * @param value - The numeric value.
250
- * @returns A FunctionNode representing the LOG2 SQL function.
284
+ *
285
+ * @param value - The numeric value or column.
286
+ * @returns A `TypedExpression<number>` representing the `LOG2` SQL function.
251
287
  */
252
- export const log2 = (value: OperandInput): FunctionNode => fn('LOG2', [value]);
288
+ export const log2 = (value: OperandInput): TypedExpression<number> => nfn('LOG2', [value]);
253
289
 
254
290
  /**
255
291
  * Returns the cube root of a number.
256
- * @param value - The numeric value.
257
- * @returns A FunctionNode representing the CBRT SQL function.
292
+ *
293
+ * @param value - The numeric value or column.
294
+ * @returns A `TypedExpression<number>` representing the `CBRT` SQL function.
258
295
  */
259
- export const cbrt = (value: OperandInput): FunctionNode => fn('CBRT', [value]);
296
+ export const cbrt = (value: OperandInput): TypedExpression<number> => nfn('CBRT', [value]);
260
297