xcraft-core-pickaxe 0.1.5 → 0.1.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.
- package/lib/operator-to-sql.js +18 -0
- package/lib/operators.js +27 -0
- package/lib/picks.js +29 -2
- package/lib/query-builder.js +4 -1
- package/package.json +1 -1
package/lib/operator-to-sql.js
CHANGED
|
@@ -124,6 +124,24 @@ const operators = {
|
|
|
124
124
|
return `(${sqlConditions.join(' OR ')})`;
|
|
125
125
|
},
|
|
126
126
|
|
|
127
|
+
abs({value}, values) {
|
|
128
|
+
return `ABS(${sql(value, values)})`;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
plus({values: list}, values) {
|
|
132
|
+
return `(${list
|
|
133
|
+
.map((value) => sql(value, values))
|
|
134
|
+
.filter(Boolean)
|
|
135
|
+
.join(' + ')})`;
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
minus({values: list}, values) {
|
|
139
|
+
return `(${list
|
|
140
|
+
.map((value) => sql(value, values))
|
|
141
|
+
.filter(Boolean)
|
|
142
|
+
.join(' - ')})`;
|
|
143
|
+
},
|
|
144
|
+
|
|
127
145
|
length({list}, values) {
|
|
128
146
|
return `json_array_length(${sql(list, values)})`;
|
|
129
147
|
},
|
package/lib/operators.js
CHANGED
|
@@ -168,6 +168,33 @@ const operators = {
|
|
|
168
168
|
});
|
|
169
169
|
},
|
|
170
170
|
|
|
171
|
+
abs(value) {
|
|
172
|
+
value = op(value);
|
|
173
|
+
return /** @type {const} */ ({
|
|
174
|
+
operator: 'abs',
|
|
175
|
+
type: number,
|
|
176
|
+
value,
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
plus(...values) {
|
|
181
|
+
values = values.map(op);
|
|
182
|
+
return /** @type {const} */ ({
|
|
183
|
+
operator: 'plus',
|
|
184
|
+
type: number,
|
|
185
|
+
values,
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
minus(...values) {
|
|
190
|
+
values = values.map(op);
|
|
191
|
+
return /** @type {const} */ ({
|
|
192
|
+
operator: 'minus',
|
|
193
|
+
type: number,
|
|
194
|
+
values,
|
|
195
|
+
});
|
|
196
|
+
},
|
|
197
|
+
|
|
171
198
|
length(list) {
|
|
172
199
|
return /** @type {const} */ ({
|
|
173
200
|
operator: 'length',
|
package/lib/picks.js
CHANGED
|
@@ -12,6 +12,7 @@ const {
|
|
|
12
12
|
number,
|
|
13
13
|
StringType,
|
|
14
14
|
string,
|
|
15
|
+
NumberType,
|
|
15
16
|
} = require('xcraft-core-stones');
|
|
16
17
|
|
|
17
18
|
const $o = require('./operators.js');
|
|
@@ -49,7 +50,7 @@ const $o = require('./operators.js');
|
|
|
49
50
|
|
|
50
51
|
/**
|
|
51
52
|
* @template {Type} T
|
|
52
|
-
* @typedef { [T] extends [ArrayType<infer V>] ? ArrayPick<V> : [T] extends [ObjectType<infer S>] ? ObjectPick<S> : [T] extends [ObjectMapType<infer V>] ? RecordPick<StringType,V> : [T] extends [RecordType<infer K,infer V>] ? RecordPick<K,V> : ValuePick<T>} PickOfType
|
|
53
|
+
* @typedef { [T] extends [ArrayType<infer V>] ? ArrayPick<V> : [T] extends [ObjectType<infer S>] ? ObjectPick<S> : [T] extends [ObjectMapType<infer V>] ? RecordPick<StringType,V> : [T] extends [RecordType<infer K,infer V>] ? RecordPick<K,V> : [T] extends [NumberType] ? NumberPick : ValuePick<T>} PickOfType
|
|
53
54
|
*/
|
|
54
55
|
/**
|
|
55
56
|
* @template {AnyTypeOrShape} T
|
|
@@ -155,6 +156,29 @@ class ValuePick {
|
|
|
155
156
|
}
|
|
156
157
|
}
|
|
157
158
|
|
|
159
|
+
/**
|
|
160
|
+
* @extends {ValuePick<NumberType>}
|
|
161
|
+
*/
|
|
162
|
+
class NumberPick extends ValuePick {
|
|
163
|
+
abs() {
|
|
164
|
+
return $o.abs(this.value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* @param {number | ValuePick<NumberType>} value
|
|
169
|
+
*/
|
|
170
|
+
plus(value) {
|
|
171
|
+
return $o.plus(this.value, value);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param {number | ValuePick<NumberType>} value
|
|
176
|
+
*/
|
|
177
|
+
minus(value) {
|
|
178
|
+
return $o.minus(this.value, value);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
158
182
|
/**
|
|
159
183
|
* @template {ObjectShape} T
|
|
160
184
|
*/
|
|
@@ -340,7 +364,7 @@ class ArrayPick {
|
|
|
340
364
|
}
|
|
341
365
|
|
|
342
366
|
get length() {
|
|
343
|
-
return new
|
|
367
|
+
return new NumberPick(number, {
|
|
344
368
|
field: $o.length(this.value),
|
|
345
369
|
path: [],
|
|
346
370
|
});
|
|
@@ -437,6 +461,9 @@ function makeTypePick(type, context) {
|
|
|
437
461
|
if (type instanceof RecordType) {
|
|
438
462
|
return /** @type {any} */ (new RecordPick(type, context));
|
|
439
463
|
}
|
|
464
|
+
if (type instanceof NumberType) {
|
|
465
|
+
return /** @type {any} */ (new NumberPick(type, context));
|
|
466
|
+
}
|
|
440
467
|
return /** @type {any} */ (new ValuePick(type, context));
|
|
441
468
|
}
|
|
442
469
|
|
package/lib/query-builder.js
CHANGED
|
@@ -31,9 +31,12 @@ const {queryToSql} = require('./query-to-sql.js');
|
|
|
31
31
|
/**
|
|
32
32
|
* @typedef {Operators["count"] | Operators["avg"] | Operators["max"] | Operators["min"] | Operators["sum"] | Operators["groupArray"]} Aggregator
|
|
33
33
|
*/
|
|
34
|
+
/**
|
|
35
|
+
* @typedef {Operators["abs"] | Operators["plus"] | Operators["minus"]} MathOperator
|
|
36
|
+
*/
|
|
34
37
|
|
|
35
38
|
/**
|
|
36
|
-
* @typedef {AnyPick | Aggregator} SelectValue
|
|
39
|
+
* @typedef {AnyPick | Aggregator | MathOperator} SelectValue
|
|
37
40
|
*/
|
|
38
41
|
/**
|
|
39
42
|
* @template T
|