@leyyo/query 1.0.1 → 1.0.2
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/assets/icon.svg +3 -2
- package/dist/field/index.d.ts +1 -0
- package/dist/field/index.js +17 -0
- package/dist/field/index.types.d.ts +10 -0
- package/dist/field/index.types.js +2 -0
- package/dist/group-by/index.d.ts +1 -0
- package/dist/group-by/index.js +17 -0
- package/dist/group-by/index.types.d.ts +8 -0
- package/dist/group-by/index.types.js +2 -0
- package/dist/index.d.ts +8 -1
- package/dist/index.js +8 -1
- package/dist/index.types.d.ts +0 -37
- package/dist/index.types.js +0 -2
- package/dist/order-by/index.d.ts +1 -0
- package/dist/order-by/index.js +17 -0
- package/dist/order-by/index.types.d.ts +18 -0
- package/dist/order-by/index.types.js +2 -0
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +17 -0
- package/dist/pagination/index.types.d.ts +10 -0
- package/dist/pagination/index.types.js +2 -0
- package/dist/parser/index.d.ts +2 -0
- package/dist/parser/index.js +18 -0
- package/dist/parser/index.types.d.ts +5 -0
- package/dist/parser/index.types.js +2 -0
- package/dist/parser/query.parser.d.ts +2 -0
- package/dist/parser/query.parser.js +421 -0
- package/dist/query/index.d.ts +1 -0
- package/dist/query/index.js +17 -0
- package/dist/query/index.types.d.ts +21 -0
- package/dist/query/index.types.js +2 -0
- package/dist/select/index.d.ts +1 -0
- package/dist/select/index.js +17 -0
- package/dist/select/index.types.d.ts +8 -0
- package/dist/select/index.types.js +2 -0
- package/dist/where/index.d.ts +1 -0
- package/dist/where/index.js +17 -0
- package/dist/where/index.types.d.ts +22 -0
- package/dist/where/index.types.js +2 -0
- package/package.json +1 -1
package/dist/assets/icon.svg
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
|
2
2
|
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
3
3
|
<rect width="24" height="24" rx="4" fill="#03a56f" />
|
|
4
|
-
<g transform="scale(0.9)" transform-origin="12 12" stroke-width="
|
|
5
|
-
<path
|
|
4
|
+
<g transform="scale(0.9)" transform-origin="12 12" stroke-width="2">
|
|
5
|
+
<path stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"
|
|
6
|
+
d="M21 6H19M21 12H16M21 18H16M7 20V13.5612C7 13.3532 7 13.2492 6.97958 13.1497C6.96147 13.0615 6.93151 12.9761 6.89052 12.8958C6.84431 12.8054 6.77934 12.7242 6.64939 12.5617L3.35061 8.43826C3.22066 8.27583 3.15569 8.19461 3.10948 8.10417C3.06849 8.02393 3.03853 7.93852 3.02042 7.85026C3 7.75078 3 7.64677 3 7.43875V5.6C3 5.03995 3 4.75992 3.10899 4.54601C3.20487 4.35785 3.35785 4.20487 3.54601 4.10899C3.75992 4 4.03995 4 4.6 4H13.4C13.9601 4 14.2401 4 14.454 4.10899C14.6422 4.20487 14.7951 4.35785 14.891 4.54601C15 4.75992 15 5.03995 15 5.6V7.43875C15 7.64677 15 7.75078 14.9796 7.85026C14.9615 7.93852 14.9315 8.02393 14.8905 8.10417C14.8443 8.19461 14.7793 8.27583 14.6494 8.43826L11.3506 12.5617C11.2207 12.7242 11.1557 12.8054 11.1095 12.8958C11.0685 12.9761 11.0385 13.0615 11.0204 13.1497C11 13.2492 11 13.3532 11 13.5612V17L7 20Z"/>
|
|
6
7
|
</g>
|
|
7
8
|
</svg>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { KeyOf } from "@leyyo/common";
|
|
2
|
+
import type { FieldRaw, FieldRegular } from "../field";
|
|
3
|
+
export type GroupByAny<T> = Array<KeyOf<T> | GroupByGivenRegular<T> | GroupByGivenRaw>;
|
|
4
|
+
export type GroupByGivenRegular<T> = FieldRegular<T>;
|
|
5
|
+
export type GroupByGivenRaw = FieldRaw;
|
|
6
|
+
export type GroupBy<T> = Array<GroupByItemRegular<T> | GroupByItemRaw>;
|
|
7
|
+
export type GroupByItemRegular<T> = FieldRegular<T>;
|
|
8
|
+
export type GroupByItemRaw = FieldRaw;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
export * from './index.types';
|
|
2
1
|
export * from './condition';
|
|
2
|
+
export * from './field';
|
|
3
|
+
export * from './group-by';
|
|
4
|
+
export * from './order-by';
|
|
5
|
+
export * from './pagination';
|
|
6
|
+
export * from './parser';
|
|
7
|
+
export * from './query';
|
|
8
|
+
export * from './select';
|
|
9
|
+
export * from './where';
|
package/dist/index.js
CHANGED
|
@@ -14,5 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./index.types"), exports);
|
|
18
17
|
__exportStar(require("./condition"), exports);
|
|
18
|
+
__exportStar(require("./field"), exports);
|
|
19
|
+
__exportStar(require("./group-by"), exports);
|
|
20
|
+
__exportStar(require("./order-by"), exports);
|
|
21
|
+
__exportStar(require("./pagination"), exports);
|
|
22
|
+
__exportStar(require("./parser"), exports);
|
|
23
|
+
__exportStar(require("./query"), exports);
|
|
24
|
+
__exportStar(require("./select"), exports);
|
|
25
|
+
__exportStar(require("./where"), exports);
|
package/dist/index.types.d.ts
CHANGED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ConditionType } from "./condition";
|
|
2
|
-
import { KeyOf, Keys } from "@leyyo/common";
|
|
3
|
-
export type OrderType = 'asc' | 'desc';
|
|
4
|
-
export type OrderByAny<T> = KeyOf<T> | Keys<T> | Array<OrderBy<T>>;
|
|
5
|
-
export interface OrderBy<T> {
|
|
6
|
-
field: KeyOf<T>;
|
|
7
|
-
asc: boolean | OrderType;
|
|
8
|
-
}
|
|
9
|
-
export type PaginationAny = PaginationPage | PaginationLimit | PaginationLiteral;
|
|
10
|
-
export interface PaginationPage {
|
|
11
|
-
page?: number;
|
|
12
|
-
size?: number;
|
|
13
|
-
}
|
|
14
|
-
export interface PaginationLimit {
|
|
15
|
-
limit?: number;
|
|
16
|
-
offset?: number;
|
|
17
|
-
}
|
|
18
|
-
export type PaginationLiteral = [number?, number?];
|
|
19
|
-
export type WhereValue<T, K extends keyof T = keyof T> = {
|
|
20
|
-
[P in K]: T[P];
|
|
21
|
-
};
|
|
22
|
-
export type WhereAny<T> = WhereValue<T> | Array<Where<T>>;
|
|
23
|
-
export interface Where<T> {
|
|
24
|
-
field: KeyOf<T>;
|
|
25
|
-
condition?: string | ConditionType;
|
|
26
|
-
value: unknown;
|
|
27
|
-
lambda?: string;
|
|
28
|
-
}
|
|
29
|
-
export type SelectAny<T> = '*' | string | Keys<T>;
|
|
30
|
-
export type GroupByAny<T> = Keys<T>;
|
|
31
|
-
export interface QueryAny<T = Record<string, unknown>> {
|
|
32
|
-
select: SelectAny<T>;
|
|
33
|
-
where?: WhereAny<T>;
|
|
34
|
-
groupBy?: GroupByAny<T>;
|
|
35
|
-
orderBy?: OrderByAny<T>;
|
|
36
|
-
pagination?: PaginationAny;
|
|
37
|
-
}
|
package/dist/index.types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { KeyOf } from "@leyyo/common";
|
|
2
|
+
import type { FieldRaw, FieldRegular } from "../field";
|
|
3
|
+
export type OrderType = 'asc' | 'desc';
|
|
4
|
+
export type OrderByAny<T> = KeyOf<T> | Array<OrderByGiven<T> | KeyOf<T> | OrderByGivenRaw> | OrderByValue<T>;
|
|
5
|
+
export type OrderByValue<T, K extends keyof T = keyof T> = {
|
|
6
|
+
[P in K]: boolean;
|
|
7
|
+
};
|
|
8
|
+
export interface OrderByGivenAsc {
|
|
9
|
+
asc?: boolean | OrderType;
|
|
10
|
+
}
|
|
11
|
+
export interface OrderAscRegular {
|
|
12
|
+
asc: boolean;
|
|
13
|
+
}
|
|
14
|
+
export type OrderByGiven<T> = FieldRegular<T> & OrderByGivenAsc;
|
|
15
|
+
export type OrderByGivenRaw = FieldRaw & OrderByGivenAsc;
|
|
16
|
+
export type OrderByItem<T> = FieldRegular<T> & OrderAscRegular;
|
|
17
|
+
export type OrderByRaw = FieldRaw & OrderAscRegular;
|
|
18
|
+
export type OrderBy<T> = Array<OrderByItem<T> | OrderByRaw>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type PaginationAny = PaginationPage | PaginationLimit | PaginationLiteral;
|
|
2
|
+
export interface PaginationPage {
|
|
3
|
+
page?: number;
|
|
4
|
+
size?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface PaginationLimit {
|
|
7
|
+
limit?: number;
|
|
8
|
+
offset?: number;
|
|
9
|
+
}
|
|
10
|
+
export type PaginationLiteral = [number?, number?];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
18
|
+
__exportStar(require("./query.parser"), exports);
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.queryParser = void 0;
|
|
4
|
+
const common_1 = require("@leyyo/common");
|
|
5
|
+
const condition_1 = require("../condition");
|
|
6
|
+
class QueryParser {
|
|
7
|
+
// region private
|
|
8
|
+
_error(message, path) {
|
|
9
|
+
return new Error(message + ', ' + path);
|
|
10
|
+
}
|
|
11
|
+
_asc(value, path) {
|
|
12
|
+
if (common_1.$is.empty(value)) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
if (common_1.$is.boolean(value)) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
else if (common_1.$is.text(value)) {
|
|
19
|
+
if (value.toLowerCase() === 'asc') {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
else if (value.toLowerCase() === 'desc') {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
throw this._error('Order asc error: invalid string', path);
|
|
26
|
+
}
|
|
27
|
+
throw this._error('Order asc error: invalid string', path);
|
|
28
|
+
}
|
|
29
|
+
_field(value, path) {
|
|
30
|
+
if (common_1.$is.text(value)) {
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
throw this._error('Order asc error: invalid string', path);
|
|
34
|
+
}
|
|
35
|
+
_raw(value, path) {
|
|
36
|
+
if (common_1.$is.text(value)) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
throw this._error('Order asc error: invalid string', path);
|
|
40
|
+
}
|
|
41
|
+
_as(value, path) {
|
|
42
|
+
if (common_1.$is.empty(value)) {
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
else if (common_1.$is.text(value)) {
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
throw this._error('Order asc error: invalid string', path);
|
|
49
|
+
}
|
|
50
|
+
_condition(value, path) {
|
|
51
|
+
if (common_1.$is.empty(value)) {
|
|
52
|
+
return '==';
|
|
53
|
+
}
|
|
54
|
+
else if (common_1.$is.text(value)) {
|
|
55
|
+
const key = value;
|
|
56
|
+
if (common_1.$is.literal(key, condition_1.ConditionTypeItems)) {
|
|
57
|
+
return key;
|
|
58
|
+
}
|
|
59
|
+
if (condition_1.ConditionTypeMap[key] !== undefined) {
|
|
60
|
+
return condition_1.ConditionTypeMap[key];
|
|
61
|
+
}
|
|
62
|
+
throw this._error('Order asc error: invalid string', path);
|
|
63
|
+
}
|
|
64
|
+
throw this._error('Order asc error: invalid string', path);
|
|
65
|
+
}
|
|
66
|
+
_value(value, path) {
|
|
67
|
+
if (value === undefined) {
|
|
68
|
+
return [undefined];
|
|
69
|
+
}
|
|
70
|
+
switch (typeof value) {
|
|
71
|
+
case "string":
|
|
72
|
+
return [];
|
|
73
|
+
case "number":
|
|
74
|
+
case "boolean":
|
|
75
|
+
return [value];
|
|
76
|
+
case "object":
|
|
77
|
+
if (value === null) {
|
|
78
|
+
return [null];
|
|
79
|
+
}
|
|
80
|
+
if (Array.isArray(value) && value.length > 0) {
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
throw this._error('Order asc error: invalid string', path);
|
|
86
|
+
}
|
|
87
|
+
_num(value, path, min) {
|
|
88
|
+
if (common_1.$is.empty(value)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
else if (common_1.$is.integer(value) && value >= min) {
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
throw this._error('Order asc error: invalid string', path);
|
|
95
|
+
}
|
|
96
|
+
_fieldXorRaw(field, raw, path) {
|
|
97
|
+
if (!raw && !field) {
|
|
98
|
+
throw this._error('Order asc error: invalid string', path);
|
|
99
|
+
}
|
|
100
|
+
else if (raw && field) {
|
|
101
|
+
throw this._error('Order asc error: invalid string', path);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// endregion private
|
|
105
|
+
// region parts
|
|
106
|
+
_select(given, _availableFields, name) {
|
|
107
|
+
if (common_1.$is.empty(given)) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
// Cases:
|
|
111
|
+
// 1 - '*'
|
|
112
|
+
// 2 - Array<KeyOf<T> | [KeyOf<T>, string] | SelectGiven<T> | SelectGivenRaw>
|
|
113
|
+
// case 1: string as KeyOf<T>
|
|
114
|
+
if (given === '*') {
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
const newSelect = [];
|
|
118
|
+
// case 2: Array<KeyOf<T> | [KeyOf<T>, string] | SelectGiven<T> | SelectGivenRaw>
|
|
119
|
+
if (Array.isArray(given)) {
|
|
120
|
+
if (given.length < 1) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
const arr = given;
|
|
124
|
+
arr.forEach((item, index) => {
|
|
125
|
+
// Case 2A: KeyOf<T>
|
|
126
|
+
if (common_1.$is.text(item)) {
|
|
127
|
+
newSelect.push({
|
|
128
|
+
field: item,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
// Case 2B: [KeyOf<T>, string]
|
|
132
|
+
else if (Array.isArray(item)) {
|
|
133
|
+
let [field, as] = item;
|
|
134
|
+
field = this._field(field, `select[${index}][0]`);
|
|
135
|
+
as = this._as(as, `select[${index}][1]`);
|
|
136
|
+
newSelect.push({ field, as, });
|
|
137
|
+
}
|
|
138
|
+
// Case 2C: SelectGiven<T> | SelectGivenRaw
|
|
139
|
+
else if (common_1.$is.object(item)) {
|
|
140
|
+
let as;
|
|
141
|
+
let field;
|
|
142
|
+
let raw;
|
|
143
|
+
const obj = item;
|
|
144
|
+
if (!common_1.$is.empty(obj.raw)) {
|
|
145
|
+
raw = this._raw(obj.raw, `select[${index}].raw`);
|
|
146
|
+
}
|
|
147
|
+
if (!common_1.$is.empty(obj.field)) {
|
|
148
|
+
field = this._field(obj.field, `select[${index}].field`);
|
|
149
|
+
}
|
|
150
|
+
this._fieldXorRaw(field, raw, `select[${index}].field`);
|
|
151
|
+
as = this._as(obj.as, `select[${index}].as`);
|
|
152
|
+
if (field) {
|
|
153
|
+
newSelect.push({ field, as });
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
newSelect.push({ raw, as });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// other
|
|
160
|
+
else {
|
|
161
|
+
throw this._error('Invalid select item', name);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
// case: other
|
|
166
|
+
else {
|
|
167
|
+
throw this._error('Invalid select item', name);
|
|
168
|
+
}
|
|
169
|
+
return newSelect;
|
|
170
|
+
}
|
|
171
|
+
_where(scope, given, _availableFields, name) {
|
|
172
|
+
if (common_1.$is.empty(given)) {
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
// Cases:
|
|
176
|
+
// 1 - WhereValue<T>
|
|
177
|
+
// 2 - Array<WhereGiven<T>|WhereGivenRaw|[KeyOf<T>, unknown]>
|
|
178
|
+
const newWhere = [];
|
|
179
|
+
// case 1: WhereValue<T>
|
|
180
|
+
if (common_1.$is.object(given)) {
|
|
181
|
+
let index = 0;
|
|
182
|
+
for (let [k, v] of Object.entries(given)) {
|
|
183
|
+
const field = this._field(k, `where(key=${index})`);
|
|
184
|
+
const value = this._value(v, `where.${field}`);
|
|
185
|
+
newWhere.push({ field, value, condition: '==' });
|
|
186
|
+
index++;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// case 2: array as Array<WhereGiven<T>|WhereGivenRaw|[KeyOf<T>, unknown]>
|
|
190
|
+
else if (Array.isArray(given)) {
|
|
191
|
+
if (given.length < 1) {
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
const arr = given;
|
|
195
|
+
arr.forEach((item, index) => {
|
|
196
|
+
var _a, _b;
|
|
197
|
+
// Case 2A: WhereGiven<T>|WhereGivenRaw
|
|
198
|
+
if (common_1.$is.object(item)) {
|
|
199
|
+
let field;
|
|
200
|
+
let raw;
|
|
201
|
+
let condition;
|
|
202
|
+
let value;
|
|
203
|
+
let fullRaw;
|
|
204
|
+
const obj = item;
|
|
205
|
+
if (!common_1.$is.empty(obj.raw)) {
|
|
206
|
+
raw = this._raw(obj.raw, `select[${index}].raw`);
|
|
207
|
+
}
|
|
208
|
+
if (!common_1.$is.empty(obj.field)) {
|
|
209
|
+
field = this._field(obj.field, `select[${index}].field`);
|
|
210
|
+
}
|
|
211
|
+
this._fieldXorRaw(field, raw, `where[${index}].field`);
|
|
212
|
+
const whereItem = obj;
|
|
213
|
+
if (raw && common_1.$is.empty((_a = whereItem.eq) !== null && _a !== void 0 ? _a : whereItem.condition) && common_1.$is.empty(whereItem.value)) {
|
|
214
|
+
fullRaw = true;
|
|
215
|
+
}
|
|
216
|
+
condition = this._condition((_b = whereItem.eq) !== null && _b !== void 0 ? _b : whereItem.condition, `select[${index}].condition`);
|
|
217
|
+
value = this._value(whereItem.value, `select[${index}].value`);
|
|
218
|
+
if (field) {
|
|
219
|
+
newWhere.push({ field, condition, value });
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
newWhere.push({ raw, condition, value, fullRaw });
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// Case 2B: |[KeyOf<T>, unknown]
|
|
226
|
+
else if (Array.isArray(item) && item.length > 0) {
|
|
227
|
+
let field;
|
|
228
|
+
let value;
|
|
229
|
+
field = this._field(item[0], `select[${index}][0]`);
|
|
230
|
+
value = this._value(item[1], `select[${index}][1]`);
|
|
231
|
+
newWhere.push({ field, value, condition: '==' });
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
throw this._error('Invalid select item', name);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
// case: other
|
|
239
|
+
else {
|
|
240
|
+
throw this._error('Invalid select item', name);
|
|
241
|
+
}
|
|
242
|
+
return newWhere;
|
|
243
|
+
}
|
|
244
|
+
_groupBy(given, _availableFields, name) {
|
|
245
|
+
if (common_1.$is.empty(given)) {
|
|
246
|
+
return [];
|
|
247
|
+
}
|
|
248
|
+
// Cases:
|
|
249
|
+
// 1 - Array<KeyOf<T> | GroupByGivenRegular<T> | GroupByGivenRaw>
|
|
250
|
+
const newGroup = [];
|
|
251
|
+
// case 1: Array<KeyOf<T> | GroupByGivenRegular<T> | GroupByGivenRaw>
|
|
252
|
+
if (Array.isArray(given)) {
|
|
253
|
+
if (given.length < 1) {
|
|
254
|
+
return [];
|
|
255
|
+
}
|
|
256
|
+
const arr = given;
|
|
257
|
+
arr.forEach((item, index) => {
|
|
258
|
+
// Case 2A: GroupByGivenRegular<T> | GroupByGivenRaw
|
|
259
|
+
if (common_1.$is.object(item)) {
|
|
260
|
+
let field;
|
|
261
|
+
let raw;
|
|
262
|
+
const obj = item;
|
|
263
|
+
if (!common_1.$is.empty(obj.raw)) {
|
|
264
|
+
raw = this._raw(obj.raw, `groupBy[${index}].raw`);
|
|
265
|
+
}
|
|
266
|
+
if (!common_1.$is.empty(obj.field)) {
|
|
267
|
+
field = this._field(obj.field, `groupBy[${index}].field`);
|
|
268
|
+
}
|
|
269
|
+
this._fieldXorRaw(field, raw, `groupBy[${index}].field`);
|
|
270
|
+
if (field) {
|
|
271
|
+
newGroup.push({ field });
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
newGroup.push({ raw });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
// Case 2B: KeyOf<T>
|
|
278
|
+
else if (common_1.$is.text(item)) {
|
|
279
|
+
newGroup.push({ field: item });
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
throw this._error('Invalid select item', name);
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
// case: other
|
|
287
|
+
else {
|
|
288
|
+
throw this._error('Invalid select item', name);
|
|
289
|
+
}
|
|
290
|
+
return newGroup;
|
|
291
|
+
}
|
|
292
|
+
_orderBy(given, _availableFields, name) {
|
|
293
|
+
if (common_1.$is.empty(given)) {
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
// Cases:
|
|
297
|
+
// 1 - KeyOf<T>
|
|
298
|
+
// 2 - Array<OrderByGiven<T>|KeyOf<T>|OrderByGivenRaw>
|
|
299
|
+
// 3 - OrderByValue<T>
|
|
300
|
+
const newOrder = [];
|
|
301
|
+
// case 1: string as KeyOf<T>
|
|
302
|
+
if (common_1.$is.text(given)) {
|
|
303
|
+
newOrder.push({ field: given, asc: true });
|
|
304
|
+
}
|
|
305
|
+
// case 2: array as Array<OrderByGiven<T>|KeyOf<T>|OrderByGivenRaw>
|
|
306
|
+
else if (Array.isArray(given)) {
|
|
307
|
+
if (given.length < 1) {
|
|
308
|
+
return [];
|
|
309
|
+
}
|
|
310
|
+
const arr = given;
|
|
311
|
+
arr.forEach((item, index) => {
|
|
312
|
+
// Case 2A: OrderByGiven<T>|OrderByGivenRaw
|
|
313
|
+
if (common_1.$is.object(item)) {
|
|
314
|
+
let asc;
|
|
315
|
+
let field;
|
|
316
|
+
let raw;
|
|
317
|
+
const obj = item;
|
|
318
|
+
if (!common_1.$is.empty(obj.raw)) {
|
|
319
|
+
raw = this._raw(obj.raw, `orderBy[${index}].raw`);
|
|
320
|
+
}
|
|
321
|
+
if (!common_1.$is.empty(obj.field)) {
|
|
322
|
+
field = this._field(obj.field, `orderBy[${index}].field`);
|
|
323
|
+
}
|
|
324
|
+
this._fieldXorRaw(field, raw, `orderBy[${index}].field`);
|
|
325
|
+
asc = this._asc(obj.asc, `orderBy[${index}].asc`);
|
|
326
|
+
if (field) {
|
|
327
|
+
newOrder.push({ field, asc });
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
newOrder.push({ raw, asc });
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// Case 2B: KeyOf<T>
|
|
334
|
+
else if (common_1.$is.text(item)) {
|
|
335
|
+
newOrder.push({ field: this._field(item, ''), asc: true });
|
|
336
|
+
}
|
|
337
|
+
// other
|
|
338
|
+
else {
|
|
339
|
+
throw this._error('Invalid select item', name);
|
|
340
|
+
}
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
// case 3: {'id': true, name: true, ...} as OrderByValue<T>
|
|
344
|
+
else if (common_1.$is.object(given)) {
|
|
345
|
+
let index = 0;
|
|
346
|
+
for (let [k, v] of Object.entries(given)) {
|
|
347
|
+
const field = this._field(k, `orderBy(key=${index})`);
|
|
348
|
+
const asc = this._asc(v, `orderBy.${field}`);
|
|
349
|
+
newOrder.push({ field, asc });
|
|
350
|
+
index++;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// case: other
|
|
354
|
+
else {
|
|
355
|
+
throw this._error('Invalid select item', name);
|
|
356
|
+
}
|
|
357
|
+
return newOrder;
|
|
358
|
+
}
|
|
359
|
+
_pagination(given, name) {
|
|
360
|
+
var _a;
|
|
361
|
+
if (common_1.$is.empty(given)) {
|
|
362
|
+
return {};
|
|
363
|
+
}
|
|
364
|
+
// Case 1: PaginationLiteral
|
|
365
|
+
if (Array.isArray(given)) {
|
|
366
|
+
if (given.length < 1) {
|
|
367
|
+
return {};
|
|
368
|
+
}
|
|
369
|
+
return {
|
|
370
|
+
limit: this._num(given[0], `pagination[0]`, 1),
|
|
371
|
+
offset: this._num(given[1], `pagination[1]`, 0)
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
// Case 2: PaginationPage | PaginationLimit
|
|
375
|
+
else if (common_1.$is.object(given)) {
|
|
376
|
+
if (Object.keys(given).length < 1) {
|
|
377
|
+
return {};
|
|
378
|
+
}
|
|
379
|
+
const obj = given;
|
|
380
|
+
if (!common_1.$is.empty(obj.page)) {
|
|
381
|
+
['limit', 'offset'].forEach(f => {
|
|
382
|
+
if (!common_1.$is.empty(given[f])) {
|
|
383
|
+
throw this._error('Invalid select item', name);
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
const page = this._num(obj.page, `pagination.page`, 1);
|
|
387
|
+
const size = (_a = this._num(obj.size, `pagination.size`, 1)) !== null && _a !== void 0 ? _a : 50;
|
|
388
|
+
return {
|
|
389
|
+
limit: size,
|
|
390
|
+
offset: (page - 1) * size
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
if (!common_1.$is.empty(obj.limit)) {
|
|
394
|
+
['page', 'size'].forEach(f => {
|
|
395
|
+
if (!common_1.$is.empty(obj[f])) {
|
|
396
|
+
throw this._error('Invalid select item', name);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
return {
|
|
400
|
+
limit: this._num(obj.limit, `pagination.limit`, 1),
|
|
401
|
+
offset: this._num(obj.offset, `pagination.offset`, 0)
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
throw this._error('Invalid select item', name);
|
|
405
|
+
}
|
|
406
|
+
throw this._error('Invalid select item', name);
|
|
407
|
+
}
|
|
408
|
+
// endregion parts
|
|
409
|
+
exec(query, availableFields, name) {
|
|
410
|
+
return {
|
|
411
|
+
select: this._select(query === null || query === void 0 ? void 0 : query.select, availableFields, name),
|
|
412
|
+
where: this._where('where', query === null || query === void 0 ? void 0 : query.where, availableFields, name),
|
|
413
|
+
having: this._where('having', query === null || query === void 0 ? void 0 : query.having, availableFields, name),
|
|
414
|
+
groupBy: this._groupBy(query === null || query === void 0 ? void 0 : query.groupBy, availableFields, name),
|
|
415
|
+
orderBy: this._orderBy(query === null || query === void 0 ? void 0 : query.orderBy, availableFields, name),
|
|
416
|
+
pagination: this._pagination(query === null || query === void 0 ? void 0 : query.pagination, name),
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// noinspection JSUnusedGlobalSymbols
|
|
421
|
+
exports.queryParser = new QueryParser();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Select, SelectAny } from "../select";
|
|
2
|
+
import type { Where, WhereAny } from "../where";
|
|
3
|
+
import type { GroupBy, GroupByAny } from "../group-by";
|
|
4
|
+
import type { OrderBy, OrderByAny } from "../order-by";
|
|
5
|
+
import type { PaginationAny, PaginationLimit } from "../pagination";
|
|
6
|
+
export interface QueryAny<T = Record<string, unknown>> {
|
|
7
|
+
select?: SelectAny<T>;
|
|
8
|
+
where?: WhereAny<T>;
|
|
9
|
+
having?: WhereAny<T>;
|
|
10
|
+
groupBy?: GroupByAny<T>;
|
|
11
|
+
orderBy?: OrderByAny<T>;
|
|
12
|
+
pagination?: PaginationAny;
|
|
13
|
+
}
|
|
14
|
+
export interface QueryRegular<T = Record<string, unknown>> {
|
|
15
|
+
select: Select<T>;
|
|
16
|
+
where: Where<T>;
|
|
17
|
+
having: Where<T>;
|
|
18
|
+
groupBy: GroupBy<T>;
|
|
19
|
+
orderBy: OrderBy<T>;
|
|
20
|
+
pagination: PaginationLimit;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { KeyOf } from "@leyyo/common";
|
|
2
|
+
import type { FieldAs, FieldRaw, FieldRegular } from "../field";
|
|
3
|
+
export type SelectAny<T> = '*' | Array<KeyOf<T> | [KeyOf<T>, string] | SelectGiven<T> | SelectGivenRaw>;
|
|
4
|
+
export type SelectGiven<T> = FieldRegular<T> & FieldAs;
|
|
5
|
+
export type SelectGivenRaw = FieldRaw & FieldAs;
|
|
6
|
+
export type Select<T> = true | Array<SelectItemRegular<T> | SelectItemRaw>;
|
|
7
|
+
export type SelectItemRegular<T> = FieldRegular<T> & FieldAs;
|
|
8
|
+
export type SelectItemRaw = FieldRaw & FieldAs;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './index.types';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.types"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { KeyOf } from "@leyyo/common";
|
|
2
|
+
import type { ConditionType } from "../condition";
|
|
3
|
+
import type { FieldRaw, FieldRegular } from "../field";
|
|
4
|
+
export type WhereValue<T, K extends keyof T = keyof T> = {
|
|
5
|
+
[P in K]: T[P];
|
|
6
|
+
};
|
|
7
|
+
export type WhereAny<T> = WhereValue<T> | Array<WhereGiven<T> | WhereGivenRaw | [KeyOf<T>, unknown]>;
|
|
8
|
+
export interface WhereGivenCondition {
|
|
9
|
+
eq?: string | ConditionType;
|
|
10
|
+
condition?: string | ConditionType;
|
|
11
|
+
value: unknown;
|
|
12
|
+
}
|
|
13
|
+
export type WhereGiven<T> = FieldRegular<T> & WhereGivenCondition;
|
|
14
|
+
export type WhereGivenRaw = FieldRaw & WhereGivenCondition;
|
|
15
|
+
export type Where<T> = Array<WhereItemRegular<T> | WhereItemRaw>;
|
|
16
|
+
export type WhereItemRegular<T> = FieldRegular<T> & WhereCondition;
|
|
17
|
+
export type WhereItemRaw = FieldRaw & WhereCondition;
|
|
18
|
+
export interface WhereCondition {
|
|
19
|
+
fullRaw?: true;
|
|
20
|
+
condition?: ConditionType;
|
|
21
|
+
value?: unknown;
|
|
22
|
+
}
|