querier-ts 2.1.0 → 2.2.0
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/README.md +17 -2
- package/lib/core/errors/invalid-argument-error.d.ts +1 -12
- package/lib/core/errors/invalid-argument-error.d.ts.map +1 -1
- package/lib/core/errors/invalid-argument-error.js +2 -8
- package/lib/core/errors/invalid-argument-error.js.map +1 -1
- package/lib/core/types/nullable-condition.d.ts +2 -0
- package/lib/core/types/nullable-condition.d.ts.map +1 -0
- package/lib/core/types/nullable-condition.js +2 -0
- package/lib/core/types/nullable-condition.js.map +1 -0
- package/lib/core/types/query-conditions-group-nullable.d.ts +2 -1
- package/lib/core/types/query-conditions-group-nullable.d.ts.map +1 -1
- package/lib/core/types/validation-options.d.ts +4 -0
- package/lib/core/types/validation-options.d.ts.map +1 -0
- package/lib/core/types/validation-options.js +2 -0
- package/lib/core/types/validation-options.js.map +1 -0
- package/lib/core/validation/decorators/number-validaton.d.ts.map +1 -1
- package/lib/core/validation/decorators/number-validaton.js +6 -18
- package/lib/core/validation/decorators/number-validaton.js.map +1 -1
- package/lib/core/validation/query-row-validator.d.ts +6 -40
- package/lib/core/validation/query-row-validator.d.ts.map +1 -1
- package/lib/core/validation/query-row-validator.js +22 -44
- package/lib/core/validation/query-row-validator.js.map +1 -1
- package/lib/query.d.ts +29 -20
- package/lib/query.d.ts.map +1 -1
- package/lib/query.js +60 -68
- package/lib/query.js.map +1 -1
- package/lib/utils/functions/type-guards/is-function.d.ts +2 -1
- package/lib/utils/functions/type-guards/is-function.d.ts.map +1 -1
- package/lib/utils/functions/type-guards/is-function.js +2 -0
- package/lib/utils/functions/type-guards/is-function.js.map +1 -1
- package/package.json +15 -13
- package/lib/__tests__/query.spec.d.ts +0 -1
- package/lib/__tests__/query.spec.js +0 -165
- package/lib/core/errors/__tests__/invalid-argument-error.spec.d.ts +0 -1
- package/lib/core/errors/__tests__/invalid-argument-error.spec.js +0 -54
- package/lib/core/validation/__tests__/query-row-validator.spec.d.ts +0 -1
- package/lib/core/validation/__tests__/query-row-validator.spec.js +0 -195
- package/lib/core/validation/decorators/__tests__/number-validation.spec.d.ts +0 -1
- package/lib/core/validation/decorators/__tests__/number-validation.spec.js +0 -114
- package/lib/utils/functions/generic/__tests__/compare-arrays.spec.d.ts +0 -1
- package/lib/utils/functions/generic/__tests__/compare-arrays.spec.js +0 -21
- package/lib/utils/functions/generic/__tests__/deep-equal.spec.d.ts +0 -1
- package/lib/utils/functions/generic/__tests__/deep-equal.spec.js +0 -82
- package/lib/utils/functions/generic/__tests__/get-entries.spec.d.ts +0 -1
- package/lib/utils/functions/generic/__tests__/get-entries.spec.js +0 -55
- package/lib/utils/functions/sort/__tests__/sort-by-properties.spec.d.ts +0 -1
- package/lib/utils/functions/sort/__tests__/sort-by-properties.spec.js +0 -69
- package/lib/utils/functions/sort/__tests__/sort-by-property.spec.d.ts +0 -1
- package/lib/utils/functions/sort/__tests__/sort-by-property.spec.js +0 -63
- package/lib/utils/functions/type-guards/__tests__/is-function.spec.d.ts +0 -1
- package/lib/utils/functions/type-guards/__tests__/is-function.spec.js +0 -38
- package/lib/utils/functions/type-guards/__tests__/is-number.spec.d.ts +0 -1
- package/lib/utils/functions/type-guards/__tests__/is-number.spec.js +0 -35
- package/lib/utils/functions/type-guards/__tests__/is-object.spec.d.ts +0 -1
- package/lib/utils/functions/type-guards/__tests__/is-object.spec.js +0 -38
package/lib/query.js
CHANGED
|
@@ -18,7 +18,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
18
18
|
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");
|
|
19
19
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
20
20
|
};
|
|
21
|
-
var _Query_rows, _Query_startAt,
|
|
21
|
+
var _Query_rows, _Query_startAt, _Query_limit;
|
|
22
22
|
import { QueryRowValidator } from './core/validation';
|
|
23
23
|
import { integer, min, validateNumbers } from './core/validation/decorators';
|
|
24
24
|
import { getObjectPropertyNames } from './utils/functions/generic/get-object-property-names';
|
|
@@ -85,19 +85,10 @@ export class Query {
|
|
|
85
85
|
* Number of results to skip.
|
|
86
86
|
*/
|
|
87
87
|
_Query_startAt.set(this, 0);
|
|
88
|
-
/**
|
|
89
|
-
* Columns to order the results by.
|
|
90
|
-
*/
|
|
91
|
-
_Query_orderBy.set(this, []);
|
|
92
88
|
/**
|
|
93
89
|
* Limit of results.
|
|
94
90
|
*/
|
|
95
91
|
_Query_limit.set(this, null);
|
|
96
|
-
/**
|
|
97
|
-
* Indicates whether conditions with `null` and `undefined` values should be
|
|
98
|
-
* skipped.
|
|
99
|
-
*/
|
|
100
|
-
this.ignoreNullValues = false;
|
|
101
92
|
__classPrivateFieldSet(this, _Query_rows, [...rows], "f");
|
|
102
93
|
}
|
|
103
94
|
/**
|
|
@@ -129,27 +120,24 @@ export class Query {
|
|
|
129
120
|
}
|
|
130
121
|
// create new query
|
|
131
122
|
const query = new Query(rows);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
__classPrivateFieldSet(query, _Query_orderBy, orderBy, "f");
|
|
123
|
+
this.cloneStateInto(query);
|
|
124
|
+
return query;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Maps each row to a new object and returns a new query.
|
|
128
|
+
*
|
|
129
|
+
* @param callback Function to be called for each row.
|
|
130
|
+
* @returns New query with the mapped rows.
|
|
131
|
+
*/
|
|
132
|
+
map(callback) {
|
|
133
|
+
// map rows
|
|
134
|
+
const rows = [];
|
|
135
|
+
for (const row of __classPrivateFieldGet(this, _Query_rows, "f")) {
|
|
136
|
+
rows.push(callback(row));
|
|
149
137
|
}
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
138
|
+
// create new query
|
|
139
|
+
const query = new Query(rows);
|
|
140
|
+
this.cloneStateInto(query);
|
|
153
141
|
return query;
|
|
154
142
|
}
|
|
155
143
|
/**
|
|
@@ -181,9 +169,7 @@ export class Query {
|
|
|
181
169
|
* @returns Current query.
|
|
182
170
|
*/
|
|
183
171
|
filterWhere(condition) {
|
|
184
|
-
this.ignoreNullValues
|
|
185
|
-
this.filterRows(condition);
|
|
186
|
-
this.ignoreNullValues = false;
|
|
172
|
+
this.filterRows(condition, { ignoreNullValues: true });
|
|
187
173
|
return this;
|
|
188
174
|
}
|
|
189
175
|
/**
|
|
@@ -211,21 +197,15 @@ export class Query {
|
|
|
211
197
|
* @returns Current query.
|
|
212
198
|
*/
|
|
213
199
|
orderBy(...columns) {
|
|
214
|
-
__classPrivateFieldSet(this,
|
|
200
|
+
__classPrivateFieldSet(this, _Query_rows, __classPrivateFieldGet(this, _Query_rows, "f").sort(sortByProperties(...columns)), "f");
|
|
215
201
|
return this;
|
|
216
202
|
}
|
|
217
|
-
|
|
218
|
-
* Groups the results by a specific key.
|
|
219
|
-
*
|
|
220
|
-
* @param key The key to group the results by.
|
|
221
|
-
*
|
|
222
|
-
* @returns A map with the grouped results.
|
|
223
|
-
*/
|
|
224
|
-
groupBy(key) {
|
|
203
|
+
groupBy(arg) {
|
|
225
204
|
const rows = this.getLimitedRows();
|
|
226
205
|
const map = new Map();
|
|
206
|
+
const hasCallback = isFunction(arg);
|
|
227
207
|
for (const row of rows) {
|
|
228
|
-
const index = row[
|
|
208
|
+
const index = hasCallback ? arg(row) : row[arg];
|
|
229
209
|
if (map.has(index)) {
|
|
230
210
|
map.get(index).push(row);
|
|
231
211
|
}
|
|
@@ -304,7 +284,11 @@ export class Query {
|
|
|
304
284
|
* @returns Array with the values of all rows.
|
|
305
285
|
*/
|
|
306
286
|
values() {
|
|
307
|
-
|
|
287
|
+
const rows = [];
|
|
288
|
+
for (const row of this.getLimitedRows()) {
|
|
289
|
+
rows.push(Object.values(row));
|
|
290
|
+
}
|
|
291
|
+
return rows;
|
|
308
292
|
}
|
|
309
293
|
/**
|
|
310
294
|
* Defines the number of rows to skip.
|
|
@@ -334,16 +318,30 @@ export class Query {
|
|
|
334
318
|
return this;
|
|
335
319
|
}
|
|
336
320
|
/**
|
|
337
|
-
*
|
|
321
|
+
* Filters the rows according to the given conditions.
|
|
338
322
|
*
|
|
339
|
-
* @
|
|
323
|
+
* @param condition Object or callback function.
|
|
340
324
|
*/
|
|
341
|
-
|
|
342
|
-
const rows =
|
|
343
|
-
|
|
344
|
-
|
|
325
|
+
filterRows(condition, options) {
|
|
326
|
+
const rows = __classPrivateFieldGet(this, _Query_rows, "f");
|
|
327
|
+
const result = [];
|
|
328
|
+
if (isFunction(condition)) {
|
|
329
|
+
for (let i = 0; i < rows.length; i++) {
|
|
330
|
+
const row = rows[i];
|
|
331
|
+
if (condition(row)) {
|
|
332
|
+
result.push(row);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
345
335
|
}
|
|
346
|
-
|
|
336
|
+
else {
|
|
337
|
+
for (let i = 0; i < rows.length; i++) {
|
|
338
|
+
const row = rows[i];
|
|
339
|
+
if (QueryRowValidator.validate(row, condition, options)) {
|
|
340
|
+
result.push(row);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
__classPrivateFieldSet(this, _Query_rows, result, "f");
|
|
347
345
|
}
|
|
348
346
|
/**
|
|
349
347
|
* Returns the first selected column or the first key of some row.
|
|
@@ -362,31 +360,25 @@ export class Query {
|
|
|
362
360
|
return null;
|
|
363
361
|
}
|
|
364
362
|
/**
|
|
365
|
-
*
|
|
363
|
+
* Returns the rows that should be used in the final results.
|
|
366
364
|
*
|
|
367
|
-
* @
|
|
365
|
+
* @returns Rows within the specified limit.
|
|
368
366
|
*/
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
: this.validateRow(row, condition)), "f");
|
|
367
|
+
getLimitedRows() {
|
|
368
|
+
const rows = [...__classPrivateFieldGet(this, _Query_rows, "f")];
|
|
369
|
+
return rows.slice(__classPrivateFieldGet(this, _Query_startAt, "f"), __classPrivateFieldGet(this, _Query_startAt, "f") + (__classPrivateFieldGet(this, _Query_limit, "f") ?? rows.length));
|
|
373
370
|
}
|
|
374
371
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
* @param row Row to validate.
|
|
378
|
-
* @param condition Conditions object.
|
|
372
|
+
* Copies the state of the current query into the given query.
|
|
379
373
|
*
|
|
380
|
-
* @
|
|
374
|
+
* @param query Query to copy the state into.
|
|
381
375
|
*/
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
ignoreNullValues: this.ignoreNullValues,
|
|
386
|
-
});
|
|
376
|
+
cloneStateInto(query) {
|
|
377
|
+
__classPrivateFieldSet(query, _Query_startAt, __classPrivateFieldGet(this, _Query_startAt, "f"), "f");
|
|
378
|
+
__classPrivateFieldSet(query, _Query_limit, __classPrivateFieldGet(this, _Query_limit, "f"), "f");
|
|
387
379
|
}
|
|
388
380
|
}
|
|
389
|
-
_Query_rows = new WeakMap(), _Query_startAt = new WeakMap(),
|
|
381
|
+
_Query_rows = new WeakMap(), _Query_startAt = new WeakMap(), _Query_limit = new WeakMap();
|
|
390
382
|
__decorate([
|
|
391
383
|
validateNumbers,
|
|
392
384
|
__param(0, integer),
|
package/lib/query.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAMA,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,G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C;IAC7B,CAAC;CACF;;AAnGC;IADC,eAAe;IACV,WAAA,OAAO,CAAA;IAAE,WAAA,GAAG,CAAC,CAAC,CAAC,CAAA;iCAIpB;AAYD;IADC,eAAe;IACT,WAAA,OAAO,CAAA;IAAE,WAAA,GAAG,CAAC,CAAC,CAAC,CAAA;kCAIrB"}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* @param value The value to check.
|
|
5
5
|
*
|
|
6
6
|
* @returns Validation result.
|
|
7
|
+
*
|
|
7
8
|
*/
|
|
8
|
-
export declare const isFunction:
|
|
9
|
+
export declare const isFunction: (value: unknown) => value is (...args: any[]) => unknown;
|
|
9
10
|
//# sourceMappingURL=is-function.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-function.d.ts","sourceRoot":"","sources":["../../../../src/utils/functions/type-guards/is-function.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"is-function.d.ts","sourceRoot":"","sources":["../../../../src/utils/functions/type-guards/is-function.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,GACrB,OAAO,OAAO,KACb,KAAK,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAsC,CAAC"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1
2
|
/**
|
|
2
3
|
* Determines if the given value is a function.
|
|
3
4
|
*
|
|
4
5
|
* @param value The value to check.
|
|
5
6
|
*
|
|
6
7
|
* @returns Validation result.
|
|
8
|
+
*
|
|
7
9
|
*/
|
|
8
10
|
export const isFunction = (value) => typeof value === 'function';
|
|
9
11
|
//# sourceMappingURL=is-function.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-function.js","sourceRoot":"","sources":["../../../../src/utils/functions/type-guards/is-function.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"is-function.js","sourceRoot":"","sources":["../../../../src/utils/functions/type-guards/is-function.ts"],"names":[],"mappings":"AAAA,uDAAuD;AAEvD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,KAAc,EACwB,EAAE,CAAC,OAAO,KAAK,KAAK,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "querier-ts",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Query tool for analysing arrays of objects",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib/**/*"
|
|
9
9
|
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "vitest",
|
|
12
|
+
"test:cov": "vitest --coverage",
|
|
13
|
+
"build": "tsc --project tsconfig.build.json",
|
|
14
|
+
"type-check": "tsc --noEmit",
|
|
15
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
16
|
+
"lint": "eslint \"src/**/*.ts\" --fix",
|
|
17
|
+
"prepare": "husky",
|
|
18
|
+
"prepublishOnly": "pnpm test && pnpm lint",
|
|
19
|
+
"preversion": "pnpm lint",
|
|
20
|
+
"version": "pnpm format && git add -A src",
|
|
21
|
+
"postversion": "git push && git push --tags"
|
|
22
|
+
},
|
|
10
23
|
"lint-staged": {
|
|
11
24
|
"src/**/*.ts": [
|
|
12
25
|
"prettier --write",
|
|
@@ -49,16 +62,5 @@
|
|
|
49
62
|
"typescript": "~5.9.3",
|
|
50
63
|
"typescript-eslint": "^8.57.1",
|
|
51
64
|
"vitest": "^4.1.2"
|
|
52
|
-
},
|
|
53
|
-
"scripts": {
|
|
54
|
-
"test": "vitest",
|
|
55
|
-
"test:cov": "vitest --coverage",
|
|
56
|
-
"build": "tsc",
|
|
57
|
-
"type-check": "tsc --noEmit",
|
|
58
|
-
"format": "prettier --write \"src/**/*.ts\"",
|
|
59
|
-
"lint": "eslint \"src/**/*.ts\" --fix",
|
|
60
|
-
"preversion": "pnpm lint",
|
|
61
|
-
"version": "pnpm format && git add -A src",
|
|
62
|
-
"postversion": "git push && git push --tags"
|
|
63
65
|
}
|
|
64
|
-
}
|
|
66
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const errors_1 = require("../core/errors");
|
|
4
|
-
const query_1 = require("../query");
|
|
5
|
-
class User {
|
|
6
|
-
constructor(id, name, permissions, isActive, createdAt, updatedAt) {
|
|
7
|
-
this.id = id;
|
|
8
|
-
this.name = name;
|
|
9
|
-
this.permissions = permissions;
|
|
10
|
-
this.isActive = isActive;
|
|
11
|
-
this.createdAt = createdAt;
|
|
12
|
-
this.updatedAt = updatedAt;
|
|
13
|
-
}
|
|
14
|
-
isAdmin() {
|
|
15
|
-
return this.id === 1;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
const users = [
|
|
19
|
-
new User(1, 'John', { useCookies: true, sendNotifications: true }, true, new Date('2023-01-01'), new Date('2023-01-10')),
|
|
20
|
-
new User(2, 'Mary', { useCookies: false, sendNotifications: true }, false, new Date('2023-02-01'), new Date('2023-02-10')),
|
|
21
|
-
new User(3, 'Bob', { useCookies: true, sendNotifications: false }, true, new Date('2023-03-01'), new Date('2023-03-10')),
|
|
22
|
-
];
|
|
23
|
-
describe('Query', () => {
|
|
24
|
-
describe('creation', () => {
|
|
25
|
-
it('should create a query from array', () => {
|
|
26
|
-
const query = query_1.Query.from(users);
|
|
27
|
-
expect(query.all().length).toBe(3);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe('where()', () => {
|
|
31
|
-
it('should filter using object conditions', () => {
|
|
32
|
-
const result = query_1.Query.from(users).where({ isActive: true }).all();
|
|
33
|
-
expect(result).toHaveLength(2);
|
|
34
|
-
expect(result.every((u) => u.isActive)).toBe(true);
|
|
35
|
-
});
|
|
36
|
-
it('should filter using function condition', () => {
|
|
37
|
-
const result = query_1.Query.from(users)
|
|
38
|
-
.where((user) => user.isAdmin())
|
|
39
|
-
.all();
|
|
40
|
-
expect(result).toHaveLength(1);
|
|
41
|
-
expect(result[0].id).toBe(1);
|
|
42
|
-
});
|
|
43
|
-
it('should combine multiple where calls', () => {
|
|
44
|
-
const result = query_1.Query.from(users)
|
|
45
|
-
.where({ isActive: true })
|
|
46
|
-
.where((user) => user.permissions.useCookies)
|
|
47
|
-
.all();
|
|
48
|
-
expect(result).toHaveLength(2);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
describe('nested where()', () => {
|
|
52
|
-
it('should filter using inner object conditions', () => {
|
|
53
|
-
const result = query_1.Query.from(users)
|
|
54
|
-
.where({
|
|
55
|
-
permissions: {
|
|
56
|
-
sendNotifications: true,
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
.all();
|
|
60
|
-
expect(result).toHaveLength(2);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
describe('filterWhere()', () => {
|
|
64
|
-
it('should ignore null and undefined conditions', () => {
|
|
65
|
-
const isActive = undefined;
|
|
66
|
-
const result = query_1.Query.from(users)
|
|
67
|
-
.filterWhere({
|
|
68
|
-
isActive,
|
|
69
|
-
id: 1,
|
|
70
|
-
})
|
|
71
|
-
.all();
|
|
72
|
-
expect(result).toHaveLength(1);
|
|
73
|
-
expect(result[0].id).toBe(1);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
describe('select()', () => {
|
|
77
|
-
it('should select a single column', () => {
|
|
78
|
-
const result = query_1.Query.from(users).select('name').column();
|
|
79
|
-
expect(result).toEqual(['John', 'Mary', 'Bob']);
|
|
80
|
-
});
|
|
81
|
-
it('should select multiple columns', () => {
|
|
82
|
-
const result = query_1.Query.from(users).select(['id', 'name']).values();
|
|
83
|
-
expect(result).toEqual([
|
|
84
|
-
[1, 'John'],
|
|
85
|
-
[2, 'Mary'],
|
|
86
|
-
[3, 'Bob'],
|
|
87
|
-
]);
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
describe('ordering', () => {
|
|
91
|
-
it('should order by ascending property', () => {
|
|
92
|
-
const result = query_1.Query.from(users).select('name').orderBy('name').column();
|
|
93
|
-
expect(result).toEqual(['Bob', 'John', 'Mary']);
|
|
94
|
-
});
|
|
95
|
-
it('should order by descending property', () => {
|
|
96
|
-
const result = query_1.Query.from(users).select('id').orderBy('-id').column();
|
|
97
|
-
expect(result).toEqual([3, 2, 1]);
|
|
98
|
-
});
|
|
99
|
-
it('should order by multiple properties', () => {
|
|
100
|
-
const result = query_1.Query.from(users)
|
|
101
|
-
.orderBy('isActive', '-id')
|
|
102
|
-
.select('id')
|
|
103
|
-
.column();
|
|
104
|
-
expect(result).toEqual([2, 3, 1]);
|
|
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
|
-
});
|
|
122
|
-
});
|
|
123
|
-
describe('pagination', () => {
|
|
124
|
-
it('should skip rows', () => {
|
|
125
|
-
const result = query_1.Query.from(users).select('id').skip(1).column();
|
|
126
|
-
expect(result).toEqual([2, 3]);
|
|
127
|
-
});
|
|
128
|
-
it('should limit rows', () => {
|
|
129
|
-
const result = query_1.Query.from(users).select('id').limit(2).column();
|
|
130
|
-
expect(result).toEqual([1, 2]);
|
|
131
|
-
});
|
|
132
|
-
it('should combine skip and limit', () => {
|
|
133
|
-
const result = query_1.Query.from(users).select('id').skip(1).limit(1).column();
|
|
134
|
-
expect(result).toEqual([2]);
|
|
135
|
-
});
|
|
136
|
-
it('should throw if skip is negative', () => {
|
|
137
|
-
expect(() => query_1.Query.from(users).skip(-1)).toThrow(errors_1.InvalidArgumentError);
|
|
138
|
-
});
|
|
139
|
-
it('should throw if limit is not an integer', () => {
|
|
140
|
-
expect(() => query_1.Query.from(users).limit(1.5)).toThrow(errors_1.InvalidArgumentError);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
describe('result helpers', () => {
|
|
144
|
-
it('count()', () => {
|
|
145
|
-
expect(query_1.Query.from(users).count()).toBe(3);
|
|
146
|
-
});
|
|
147
|
-
it('exists()', () => {
|
|
148
|
-
expect(query_1.Query.from(users).where({ id: 99 }).exists()).toBe(false);
|
|
149
|
-
});
|
|
150
|
-
it('first()', () => {
|
|
151
|
-
expect(query_1.Query.from(users).first()?.id).toBe(1);
|
|
152
|
-
});
|
|
153
|
-
it('last()', () => {
|
|
154
|
-
expect(query_1.Query.from(users).last()?.id).toBe(3);
|
|
155
|
-
});
|
|
156
|
-
it('scalar()', () => {
|
|
157
|
-
const id = query_1.Query.from(users).select('id').scalar();
|
|
158
|
-
expect(id).toBe(1);
|
|
159
|
-
});
|
|
160
|
-
it('scalar() should return false if no results', () => {
|
|
161
|
-
const result = query_1.Query.from(users).where({ id: 99 }).scalar();
|
|
162
|
-
expect(result).toBe(false);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const invalid_argument_error_1 = require("../invalid-argument-error");
|
|
4
|
-
describe('InvalidArgumentError', () => {
|
|
5
|
-
it('should be an instance of Error', () => {
|
|
6
|
-
const error = new invalid_argument_error_1.InvalidArgumentError({
|
|
7
|
-
method: 'testMethod',
|
|
8
|
-
param: 'value',
|
|
9
|
-
argument: 123,
|
|
10
|
-
expected: 'a string',
|
|
11
|
-
});
|
|
12
|
-
expect(error).toBeInstanceOf(Error);
|
|
13
|
-
});
|
|
14
|
-
it('should have the correct name', () => {
|
|
15
|
-
const error = new invalid_argument_error_1.InvalidArgumentError({
|
|
16
|
-
method: 'testMethod',
|
|
17
|
-
param: 'value',
|
|
18
|
-
argument: 123,
|
|
19
|
-
expected: 'a string',
|
|
20
|
-
});
|
|
21
|
-
expect(error.name).toBe('InvalidArgumentError');
|
|
22
|
-
});
|
|
23
|
-
it('should generate the correct error message', () => {
|
|
24
|
-
const error = new invalid_argument_error_1.InvalidArgumentError({
|
|
25
|
-
method: 'doSomething',
|
|
26
|
-
param: 0,
|
|
27
|
-
argument: false,
|
|
28
|
-
expected: 'a boolean',
|
|
29
|
-
});
|
|
30
|
-
expect(error.message).toBe('false is not a valid argument to param 0 on doSomething(). It should be a boolean.');
|
|
31
|
-
});
|
|
32
|
-
it('should expose configuration properties', () => {
|
|
33
|
-
const config = {
|
|
34
|
-
method: 'save',
|
|
35
|
-
param: 'id',
|
|
36
|
-
argument: null,
|
|
37
|
-
expected: 'a non-null value',
|
|
38
|
-
};
|
|
39
|
-
const error = new invalid_argument_error_1.InvalidArgumentError(config);
|
|
40
|
-
expect(error.method).toBe(config.method);
|
|
41
|
-
expect(error.param).toBe(config.param);
|
|
42
|
-
expect(error.argument).toBe(config.argument);
|
|
43
|
-
expect(error.expected).toBe(config.expected);
|
|
44
|
-
});
|
|
45
|
-
it('should preserve prototype chain', () => {
|
|
46
|
-
const error = new invalid_argument_error_1.InvalidArgumentError({
|
|
47
|
-
method: 'run',
|
|
48
|
-
param: 'options',
|
|
49
|
-
argument: {},
|
|
50
|
-
expected: 'a valid options object',
|
|
51
|
-
});
|
|
52
|
-
expect(error instanceof invalid_argument_error_1.InvalidArgumentError).toBe(true);
|
|
53
|
-
});
|
|
54
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|