querier-ts 1.0.0 → 1.0.1
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/__tests__/query.spec.js +16 -0
- package/lib/query.js +17 -4
- package/lib/utils/functions/sort/sort-by-properties.d.ts +2 -2
- package/lib/utils/functions/sort/sort-by-property.d.ts +2 -2
- package/lib/utils/types/index.d.ts +1 -0
- package/lib/utils/types/index.js +1 -0
- package/lib/utils/types/sort-function.d.ts +1 -0
- package/lib/utils/types/sort-function.js +2 -0
- package/package.json +2 -2
|
@@ -103,6 +103,22 @@ describe('Query', () => {
|
|
|
103
103
|
.column();
|
|
104
104
|
expect(result).toEqual([2, 3, 1]);
|
|
105
105
|
});
|
|
106
|
+
it('should override order defined previously', () => {
|
|
107
|
+
const result = query_1.Query.from(users)
|
|
108
|
+
.orderBy('id')
|
|
109
|
+
.orderBy('-id')
|
|
110
|
+
.select('id')
|
|
111
|
+
.column();
|
|
112
|
+
expect(result).toEqual([3, 2, 1]);
|
|
113
|
+
});
|
|
114
|
+
it('should clear order defined previously', () => {
|
|
115
|
+
const result = query_1.Query.from(users)
|
|
116
|
+
.orderBy('-id')
|
|
117
|
+
.orderBy()
|
|
118
|
+
.select('id')
|
|
119
|
+
.column();
|
|
120
|
+
expect(result).toEqual([1, 2, 3]);
|
|
121
|
+
});
|
|
106
122
|
});
|
|
107
123
|
describe('pagination', () => {
|
|
108
124
|
it('should skip rows', () => {
|
package/lib/query.js
CHANGED
|
@@ -19,7 +19,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
19
19
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
20
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
21
21
|
};
|
|
22
|
-
var _Query_rows, _Query_columns, _Query_startAt, _Query_limit;
|
|
22
|
+
var _Query_rows, _Query_columns, _Query_startAt, _Query_limit, _Query_sortFunction;
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
24
|
exports.Query = void 0;
|
|
25
25
|
const validation_1 = require("./core/validation");
|
|
@@ -95,6 +95,10 @@ class Query {
|
|
|
95
95
|
* Limit of results.
|
|
96
96
|
*/
|
|
97
97
|
_Query_limit.set(this, void 0);
|
|
98
|
+
/**
|
|
99
|
+
* Function to order the results.
|
|
100
|
+
*/
|
|
101
|
+
_Query_sortFunction.set(this, void 0);
|
|
98
102
|
/**
|
|
99
103
|
* Indicates whether conditions with `null` and `undefined` values should be
|
|
100
104
|
* skipped.
|
|
@@ -166,7 +170,12 @@ class Query {
|
|
|
166
170
|
* @returns Current query.
|
|
167
171
|
*/
|
|
168
172
|
orderBy(...columns) {
|
|
169
|
-
|
|
173
|
+
if (columns.length > 0) {
|
|
174
|
+
__classPrivateFieldSet(this, _Query_sortFunction, (0, sort_1.sortByProperties)(...columns), "f");
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
__classPrivateFieldSet(this, _Query_sortFunction, undefined, "f");
|
|
178
|
+
}
|
|
170
179
|
return this;
|
|
171
180
|
}
|
|
172
181
|
/**
|
|
@@ -279,7 +288,11 @@ class Query {
|
|
|
279
288
|
* @returns Rows within the specified limit.
|
|
280
289
|
*/
|
|
281
290
|
getLimitedRows() {
|
|
282
|
-
|
|
291
|
+
const rows = [...__classPrivateFieldGet(this, _Query_rows, "f")];
|
|
292
|
+
if (__classPrivateFieldGet(this, _Query_sortFunction, "f")) {
|
|
293
|
+
rows.sort(__classPrivateFieldGet(this, _Query_sortFunction, "f"));
|
|
294
|
+
}
|
|
295
|
+
return rows.slice(__classPrivateFieldGet(this, _Query_startAt, "f")).slice(0, __classPrivateFieldGet(this, _Query_limit, "f"));
|
|
283
296
|
}
|
|
284
297
|
/**
|
|
285
298
|
* Returns the first selected column or the first key of some row.
|
|
@@ -319,7 +332,7 @@ class Query {
|
|
|
319
332
|
}
|
|
320
333
|
}
|
|
321
334
|
exports.Query = Query;
|
|
322
|
-
_Query_rows = new WeakMap(), _Query_columns = new WeakMap(), _Query_startAt = new WeakMap(), _Query_limit = new WeakMap();
|
|
335
|
+
_Query_rows = new WeakMap(), _Query_columns = new WeakMap(), _Query_startAt = new WeakMap(), _Query_limit = new WeakMap(), _Query_sortFunction = new WeakMap();
|
|
323
336
|
__decorate([
|
|
324
337
|
decorators_1.validateNumbers,
|
|
325
338
|
__param(0, decorators_1.integer),
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { addPrefixToObject, PropertyOnly, PropOf } from '../../types';
|
|
2
|
-
export declare function sortByProperties<T extends object>(...props: (PropOf<T> | keyof addPrefixToObject<PropertyOnly<T>, '-'>)[]):
|
|
1
|
+
import { addPrefixToObject, PropertyOnly, PropOf, SortFunction } from '../../types';
|
|
2
|
+
export declare function sortByProperties<T extends object>(...props: (PropOf<T> | keyof addPrefixToObject<PropertyOnly<T>, '-'>)[]): SortFunction<T>;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { addPrefixToObject, PropertyOnly, PropOf } from '../../types';
|
|
2
|
-
export declare function sortByProperty<T extends object>(property: PropOf<T> | keyof addPrefixToObject<PropertyOnly<T>, '-'>):
|
|
1
|
+
import { addPrefixToObject, PropertyOnly, PropOf, SortFunction } from '../../types';
|
|
2
|
+
export declare function sortByProperty<T extends object>(property: PropOf<T> | keyof addPrefixToObject<PropertyOnly<T>, '-'>): SortFunction<T>;
|
package/lib/utils/types/index.js
CHANGED
|
@@ -23,3 +23,4 @@ __exportStar(require("./partial-of-properties"), exports);
|
|
|
23
23
|
__exportStar(require("./prop-of"), exports);
|
|
24
24
|
__exportStar(require("./property-only"), exports);
|
|
25
25
|
__exportStar(require("./recursive-partial"), exports);
|
|
26
|
+
__exportStar(require("./sort-function"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type SortFunction<T> = (a: T, b: T) => number;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "querier-ts",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Query tool for analysing arrays of objects",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"typescript": "~5.9.3"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
|
-
"test": "jest --config
|
|
43
|
+
"test": "jest --config jest.config.ts",
|
|
44
44
|
"build": "tsc",
|
|
45
45
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
46
46
|
"lint": "tslint -p tsconfig.json",
|