@leyyo/query 1.0.1 → 1.0.3

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.
Files changed (45) hide show
  1. package/dist/error/index.d.ts +2 -0
  2. package/dist/error/index.js +18 -0
  3. package/dist/error/index.types.d.ts +1 -0
  4. package/dist/error/invalid-query-value.error.d.ts +6 -0
  5. package/dist/error/invalid-query-value.error.js +11 -0
  6. package/dist/field/index.d.ts +1 -0
  7. package/dist/field/index.js +17 -0
  8. package/dist/field/index.types.d.ts +9 -0
  9. package/dist/field/index.types.js +2 -0
  10. package/dist/group-by/index.d.ts +1 -0
  11. package/dist/group-by/index.js +17 -0
  12. package/dist/group-by/index.types.d.ts +7 -0
  13. package/dist/group-by/index.types.js +2 -0
  14. package/dist/index.d.ts +9 -1
  15. package/dist/index.js +9 -1
  16. package/dist/order-by/index.d.ts +1 -0
  17. package/dist/order-by/index.js +17 -0
  18. package/dist/order-by/index.types.d.ts +17 -0
  19. package/dist/order-by/index.types.js +2 -0
  20. package/dist/pagination/index.d.ts +1 -0
  21. package/dist/pagination/index.js +17 -0
  22. package/dist/pagination/index.types.d.ts +10 -0
  23. package/dist/pagination/index.types.js +2 -0
  24. package/dist/parser/index.d.ts +2 -0
  25. package/dist/parser/index.js +18 -0
  26. package/dist/parser/index.types.d.ts +6 -0
  27. package/dist/parser/index.types.js +2 -0
  28. package/dist/parser/query.parser.d.ts +2 -0
  29. package/dist/parser/query.parser.js +448 -0
  30. package/dist/query/index.d.ts +1 -0
  31. package/dist/query/index.js +17 -0
  32. package/dist/query/index.types.d.ts +21 -0
  33. package/dist/query/index.types.js +2 -0
  34. package/dist/select/index.d.ts +1 -0
  35. package/dist/select/index.js +17 -0
  36. package/dist/select/index.types.d.ts +7 -0
  37. package/dist/select/index.types.js +2 -0
  38. package/dist/where/index.d.ts +1 -0
  39. package/dist/where/index.js +17 -0
  40. package/dist/where/index.types.d.ts +20 -0
  41. package/dist/where/index.types.js +2 -0
  42. package/package.json +1 -1
  43. package/dist/assets/icon.svg +0 -7
  44. package/dist/index.types.d.ts +0 -37
  45. /package/dist/{index.types.js → error/index.types.js} +0 -0
@@ -0,0 +1,2 @@
1
+ export * from './invalid-query-value.error';
2
+ export * from './index.types';
@@ -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("./invalid-query-value.error"), exports);
18
+ __exportStar(require("./index.types"), exports);
@@ -0,0 +1 @@
1
+ export type QueryErrorCode = 'xxx' | 'field:invalid' | 'field:empty' | 'field:raw-both' | 'field:raw-none' | 'as:invalid' | 'raw:invalid' | 'raw:empty' | 'eq:invalid-key' | 'eq:invalid-type' | 'asc:invalid-key' | 'asc:invalid-type' | 'value:invalid-type' | 'value:invalid-item' | 'integer:invalid' | 'integer:min' | 'select:item' | 'select:body' | 'where:item' | 'where:body' | 'having:item' | 'having:body' | 'groupBy:item' | 'groupBy:body' | 'orderBy:item' | 'orderBy:body' | 'page:conflict' | 'limit:conflict' | 'pagination:invalid-key' | 'pagination:invalid-type';
@@ -0,0 +1,6 @@
1
+ import { QueryErrorCode } from "./index.types";
2
+ export declare class InvalidQueryValueError extends Error {
3
+ readonly message: string;
4
+ readonly path: string;
5
+ constructor(code: QueryErrorCode, message: string, path: string);
6
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidQueryValueError = void 0;
4
+ class InvalidQueryValueError extends Error {
5
+ constructor(code, message, path) {
6
+ super(`[${code}] ${message} at ${path}`);
7
+ this.message = message;
8
+ this.path = path;
9
+ }
10
+ }
11
+ exports.InvalidQueryValueError = InvalidQueryValueError;
@@ -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,9 @@
1
+ export interface FieldRegular<K extends string> {
2
+ field: K;
3
+ }
4
+ export interface FieldRaw {
5
+ raw: string;
6
+ }
7
+ export interface FieldAs {
8
+ as?: string;
9
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,7 @@
1
+ import type { FieldRaw, FieldRegular } from "../field";
2
+ export type GroupByAny<K extends string> = Array<K | GroupByGivenRegular<K> | GroupByGivenRaw>;
3
+ export type GroupByGivenRegular<K extends string> = FieldRegular<K>;
4
+ export type GroupByGivenRaw = FieldRaw;
5
+ export type GroupBy<K extends string> = Array<GroupByItemRegular<K> | GroupByItemRaw>;
6
+ export type GroupByItemRegular<K extends string> = FieldRegular<K>;
7
+ export type GroupByItemRaw = FieldRaw;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/dist/index.d.ts CHANGED
@@ -1,2 +1,10 @@
1
- export * from './index.types';
2
1
  export * from './condition';
2
+ export * from './error';
3
+ export * from './field';
4
+ export * from './group-by';
5
+ export * from './order-by';
6
+ export * from './pagination';
7
+ export * from './parser';
8
+ export * from './query';
9
+ export * from './select';
10
+ export * from './where';
package/dist/index.js CHANGED
@@ -14,5 +14,13 @@ 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("./error"), exports);
19
+ __exportStar(require("./field"), exports);
20
+ __exportStar(require("./group-by"), exports);
21
+ __exportStar(require("./order-by"), exports);
22
+ __exportStar(require("./pagination"), exports);
23
+ __exportStar(require("./parser"), exports);
24
+ __exportStar(require("./query"), exports);
25
+ __exportStar(require("./select"), exports);
26
+ __exportStar(require("./where"), 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,17 @@
1
+ import type { FieldRaw, FieldRegular } from "../field";
2
+ export type OrderType = 'asc' | 'desc';
3
+ export type OrderByAny<K extends string> = K | Array<OrderByGiven<K> | K | OrderByGivenRaw> | OrderByValue<K>;
4
+ export type OrderByValue<K extends string> = {
5
+ [field in K]: boolean | OrderType;
6
+ };
7
+ export interface OrderByGivenAsc {
8
+ asc?: boolean | OrderType;
9
+ }
10
+ export interface OrderAscRegular {
11
+ asc: boolean;
12
+ }
13
+ export type OrderByGiven<K extends string> = FieldRegular<K> & OrderByGivenAsc;
14
+ export type OrderByGivenRaw = FieldRaw & OrderByGivenAsc;
15
+ export type OrderByItem<K extends string> = FieldRegular<K> & OrderAscRegular;
16
+ export type OrderByRaw = FieldRaw & OrderAscRegular;
17
+ export type OrderBy<K extends string> = Array<OrderByItem<K> | OrderByRaw>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from './index.types';
2
+ export * from './query.parser';
@@ -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,6 @@
1
+ import type { QueryAny, QueryRegular } from "../query";
2
+ import { BasicType } from "@leyyo/common";
3
+ export interface QueryParserLike {
4
+ exec<K extends string>(query: QueryAny<K>, availableFields: Array<K | string>, name?: string): QueryRegular<K>;
5
+ }
6
+ export type QueryValueType = BasicType | 'array' | 'null' | 'integer' | '*';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import type { QueryParserLike } from "./index.types";
2
+ export declare const queryParser: QueryParserLike;
@@ -0,0 +1,448 @@
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
+ const error_1 = require("../error");
7
+ class QueryParser {
8
+ // region private
9
+ _error(code, message, path) {
10
+ return new error_1.InvalidQueryValueError(code, message, path);
11
+ }
12
+ _invalid(value, path, expected, code) {
13
+ const ex = Array.isArray(expected) ? `[${expected.join(', ')}]` : expected;
14
+ return this._error(code, `It's expected as ${ex}, but it's type: ${typeof value}`, path);
15
+ }
16
+ _emptyOrInvalid(value, path, expected, empty, invalid) {
17
+ if (common_1.$is.empty(value)) {
18
+ return this._error(empty, `It's empty`, path);
19
+ }
20
+ if (typeof value === 'string' && value.trim() === '') {
21
+ return this._error(empty, `It's empty`, path);
22
+ }
23
+ return this._invalid(value, path, expected, invalid);
24
+ }
25
+ _asc(value, path) {
26
+ if (common_1.$is.empty(value)) {
27
+ return true;
28
+ }
29
+ if (common_1.$is.boolean(value)) {
30
+ return value;
31
+ }
32
+ else if (common_1.$is.text(value)) {
33
+ if (value.toLowerCase() === 'asc') {
34
+ return true;
35
+ }
36
+ else if (value.toLowerCase() === 'desc') {
37
+ return false;
38
+ }
39
+ throw this._error('asc:invalid-key', `It should be [asc, desc], but it's value: ${value}`, path);
40
+ }
41
+ throw this._invalid(value, path, ['boolean', 'string'], 'asc:invalid-type');
42
+ }
43
+ _field(value, path) {
44
+ if (common_1.$is.text(value)) {
45
+ return value;
46
+ }
47
+ throw this._emptyOrInvalid(value, path, 'string', 'field:empty', 'field:invalid');
48
+ }
49
+ _raw(value, path) {
50
+ if (common_1.$is.text(value)) {
51
+ return value;
52
+ }
53
+ throw this._emptyOrInvalid(value, path, 'string', 'raw:empty', 'raw:invalid');
54
+ }
55
+ _as(value, path) {
56
+ if (common_1.$is.empty(value)) {
57
+ return undefined;
58
+ }
59
+ else if (common_1.$is.text(value)) {
60
+ return value;
61
+ }
62
+ throw this._invalid(value, path, 'string', 'as:invalid');
63
+ }
64
+ _condition(value, path) {
65
+ if (common_1.$is.empty(value)) {
66
+ return '==';
67
+ }
68
+ else if (common_1.$is.text(value)) {
69
+ const key = value;
70
+ if (common_1.$is.literal(key, condition_1.ConditionTypeItems)) {
71
+ return key;
72
+ }
73
+ if (condition_1.ConditionTypeMap[key] !== undefined) {
74
+ return condition_1.ConditionTypeMap[key];
75
+ }
76
+ throw this._error('eq:invalid-key', `It should be [@see equalities], but it's value: ${value}`, path);
77
+ }
78
+ throw this._invalid(value, path, 'string', 'eq:invalid-type');
79
+ }
80
+ _value(value, path) {
81
+ if (value === undefined) {
82
+ return [];
83
+ }
84
+ switch (typeof value) {
85
+ case "string":
86
+ value = value.trim();
87
+ if (!value) {
88
+ throw this._error('value:invalid-type', `It should not be empty string`, path);
89
+ }
90
+ return [value];
91
+ case "number":
92
+ case "boolean":
93
+ return [value];
94
+ case "object":
95
+ if (value === null) {
96
+ return [null];
97
+ }
98
+ if (Array.isArray(value)) {
99
+ let index = 0;
100
+ for (const item of value) {
101
+ if (!common_1.$is.text(item) && !common_1.$is.number(item) && !common_1.$is.boolean(value)) {
102
+ throw this._invalid(item, `${path}[${index}]`, ['string', 'number', 'boolean', 'array', 'number'], 'value:invalid-item');
103
+ }
104
+ index++;
105
+ }
106
+ return value;
107
+ }
108
+ break;
109
+ }
110
+ throw this._invalid(value, path, ['string', 'number', 'boolean', 'array', 'number'], 'value:invalid-type');
111
+ }
112
+ _num(value, path, min) {
113
+ if (common_1.$is.empty(value)) {
114
+ return undefined;
115
+ }
116
+ else if (common_1.$is.integer(value)) {
117
+ if (value >= min) {
118
+ return value;
119
+ }
120
+ throw this._error('integer:min', `It should be gte ${min}`, path);
121
+ }
122
+ throw this._invalid(value, path, ['integer'], 'integer:invalid');
123
+ }
124
+ _fieldXorRaw(field, raw, path) {
125
+ if (!raw && !field) {
126
+ throw this._error('field:raw-none', `Field or raw are not provided, one of them should be`, path);
127
+ }
128
+ else if (raw && field) {
129
+ throw this._error('field:raw-both', `Field and raw are provided together, Field or raw are not provided, only one of them should be`, path);
130
+ }
131
+ }
132
+ // endregion private
133
+ // region parts
134
+ _select(given, _availableFields, _name) {
135
+ if (common_1.$is.empty(given)) {
136
+ return true;
137
+ }
138
+ // Cases:
139
+ // 1 - '*'
140
+ // 2 - Array<K | [K, string] | SelectGiven<K> | SelectGivenRaw>
141
+ // case 1: string as K
142
+ if (given === '*') {
143
+ return true;
144
+ }
145
+ const newSelect = [];
146
+ // case 2: Array<K | [K, string] | SelectGiven<K> | SelectGivenRaw>
147
+ if (Array.isArray(given)) {
148
+ if (given.length < 1) {
149
+ return true;
150
+ }
151
+ const arr = given;
152
+ arr.forEach((item, index) => {
153
+ // Case 2A: K
154
+ if (common_1.$is.text(item)) {
155
+ newSelect.push({
156
+ field: item,
157
+ });
158
+ }
159
+ // Case 2B: [K, string]
160
+ else if (Array.isArray(item)) {
161
+ let [field, as] = item;
162
+ field = this._field(field, `select[${index}][0]`);
163
+ as = this._as(as, `select[${index}][1]`);
164
+ newSelect.push({ field, as, });
165
+ }
166
+ // Case 2C: SelectGiven<K> | SelectGivenRaw
167
+ else if (common_1.$is.object(item)) {
168
+ let as;
169
+ let field;
170
+ let raw;
171
+ const obj = item;
172
+ if (!common_1.$is.empty(obj.raw)) {
173
+ raw = this._raw(obj.raw, `select[${index}].raw`);
174
+ }
175
+ if (!common_1.$is.empty(obj.field)) {
176
+ field = this._field(obj.field, `select[${index}].field`);
177
+ }
178
+ this._fieldXorRaw(field, raw, `select[${index}].field`);
179
+ as = this._as(obj.as, `select[${index}].as`);
180
+ if (field) {
181
+ newSelect.push({ field, as });
182
+ }
183
+ else {
184
+ newSelect.push({ raw, as });
185
+ }
186
+ }
187
+ // other
188
+ else {
189
+ throw this._invalid(item, `select[${index}]`, ['string', 'array', 'object'], 'select:item');
190
+ }
191
+ });
192
+ }
193
+ // case: other
194
+ else {
195
+ throw this._invalid(given, `select`, ['*', 'array'], 'select:body');
196
+ }
197
+ return newSelect;
198
+ }
199
+ _where(scope, given, _availableFields, _name) {
200
+ if (common_1.$is.empty(given)) {
201
+ return [];
202
+ }
203
+ // Cases:
204
+ // 1 - WhereValue<K>
205
+ // 2 - Array<WhereGiven<K>|WhereGivenRaw|[K, unknown]>
206
+ const newWhere = [];
207
+ // case 1: WhereValue<K>
208
+ if (common_1.$is.object(given)) {
209
+ let index = 0;
210
+ for (let [k, v] of Object.entries(given)) {
211
+ const field = this._field(k, `${scope}(key=${index})`);
212
+ const value = this._value(v, `${scope}.${field}`);
213
+ newWhere.push({ field, value, eq: '==' });
214
+ index++;
215
+ }
216
+ }
217
+ // case 2: array as Array<WhereGiven<K>|WhereGivenRaw|[K, unknown]>
218
+ else if (Array.isArray(given)) {
219
+ if (given.length < 1) {
220
+ return [];
221
+ }
222
+ const arr = given;
223
+ arr.forEach((item, index) => {
224
+ // Case 2A: WhereGiven<K>|WhereGivenRaw
225
+ if (common_1.$is.object(item)) {
226
+ let field;
227
+ let raw;
228
+ let eq;
229
+ let value;
230
+ let fullRaw;
231
+ const obj = item;
232
+ if (!common_1.$is.empty(obj.raw)) {
233
+ raw = this._raw(obj.raw, `${scope}[${index}].raw`);
234
+ }
235
+ if (!common_1.$is.empty(obj.field)) {
236
+ field = this._field(obj.field, `${scope}[${index}].field`);
237
+ }
238
+ this._fieldXorRaw(field, raw, `${scope}[${index}].field`);
239
+ const whereItem = obj;
240
+ if (raw && common_1.$is.empty(whereItem.eq) && common_1.$is.empty(whereItem.value)) {
241
+ fullRaw = true;
242
+ }
243
+ eq = this._condition(whereItem.eq, `${scope}[${index}].eq`);
244
+ value = this._value(whereItem.value, `${scope}[${index}].value`);
245
+ if (field) {
246
+ newWhere.push({ field, eq, value });
247
+ }
248
+ else {
249
+ newWhere.push({ raw, eq, value, fullRaw });
250
+ }
251
+ }
252
+ // Case 2B: |[K, unknown]
253
+ else if (Array.isArray(item) && item.length > 0) {
254
+ let field;
255
+ let value;
256
+ field = this._field(item[0], `${scope}[${index}][0]`);
257
+ value = this._value(item[1], `${scope}[${index}][1]`);
258
+ newWhere.push({ field, value, eq: '==' });
259
+ }
260
+ else {
261
+ throw this._invalid(item, `${scope}[${index}]`, ['object', 'array'], (scope === 'where') ? 'where:item' : 'having:item');
262
+ }
263
+ });
264
+ }
265
+ // case: other
266
+ else {
267
+ throw this._invalid(given, scope, ['object', 'array'], (scope === 'where') ? 'where:body' : 'having:body');
268
+ }
269
+ return newWhere;
270
+ }
271
+ _groupBy(given, _availableFields, _name) {
272
+ if (common_1.$is.empty(given)) {
273
+ return [];
274
+ }
275
+ // Cases:
276
+ // 1 - Array<K | GroupByGivenRegular<K> | GroupByGivenRaw>
277
+ const newGroup = [];
278
+ // case 1: Array<K | GroupByGivenRegular<K> | GroupByGivenRaw>
279
+ if (Array.isArray(given)) {
280
+ if (given.length < 1) {
281
+ return [];
282
+ }
283
+ const arr = given;
284
+ arr.forEach((item, index) => {
285
+ // Case 2A: GroupByGivenRegular<K> | GroupByGivenRaw
286
+ if (common_1.$is.object(item)) {
287
+ let field;
288
+ let raw;
289
+ const obj = item;
290
+ if (!common_1.$is.empty(obj.raw)) {
291
+ raw = this._raw(obj.raw, `groupBy[${index}].raw`);
292
+ }
293
+ if (!common_1.$is.empty(obj.field)) {
294
+ field = this._field(obj.field, `groupBy[${index}].field`);
295
+ }
296
+ this._fieldXorRaw(field, raw, `groupBy[${index}].field`);
297
+ if (field) {
298
+ newGroup.push({ field });
299
+ }
300
+ else {
301
+ newGroup.push({ raw });
302
+ }
303
+ }
304
+ // Case 2B: K
305
+ else if (common_1.$is.text(item)) {
306
+ newGroup.push({ field: item });
307
+ }
308
+ else {
309
+ throw this._invalid(item, `groupBy[${index}]`, ['string', 'object'], 'groupBy:item');
310
+ }
311
+ });
312
+ }
313
+ // case: other
314
+ else {
315
+ throw this._invalid(given, `groupBy`, ['array'], 'groupBy:body');
316
+ }
317
+ return newGroup;
318
+ }
319
+ _orderBy(given, _availableFields, _name) {
320
+ if (common_1.$is.empty(given)) {
321
+ return [];
322
+ }
323
+ // Cases:
324
+ // 1 - K
325
+ // 2 - Array<OrderByGiven<K>|K|OrderByGivenRaw>
326
+ // 3 - OrderByValue<K>
327
+ const newOrder = [];
328
+ // case 1: string as K
329
+ if (common_1.$is.text(given)) {
330
+ newOrder.push({ field: given, asc: true });
331
+ }
332
+ // case 2: array as Array<OrderByGiven<K>|K|OrderByGivenRaw>
333
+ else if (Array.isArray(given)) {
334
+ if (given.length < 1) {
335
+ return [];
336
+ }
337
+ const arr = given;
338
+ arr.forEach((item, index) => {
339
+ // Case 2A: OrderByGiven<K>|OrderByGivenRaw
340
+ if (common_1.$is.object(item)) {
341
+ let asc;
342
+ let field;
343
+ let raw;
344
+ const obj = item;
345
+ if (!common_1.$is.empty(obj.raw)) {
346
+ raw = this._raw(obj.raw, `orderBy[${index}].raw`);
347
+ }
348
+ if (!common_1.$is.empty(obj.field)) {
349
+ field = this._field(obj.field, `orderBy[${index}].field`);
350
+ }
351
+ this._fieldXorRaw(field, raw, `orderBy[${index}].field`);
352
+ asc = this._asc(obj.asc, `orderBy[${index}].asc`);
353
+ if (field) {
354
+ newOrder.push({ field, asc });
355
+ }
356
+ else {
357
+ newOrder.push({ raw, asc });
358
+ }
359
+ }
360
+ // Case 2B: K
361
+ else if (common_1.$is.text(item)) {
362
+ newOrder.push({ field: this._field(item, ''), asc: true });
363
+ }
364
+ // other
365
+ else {
366
+ throw this._invalid(item, `orderBy[${index}]`, ['string', 'object'], 'orderBy:item');
367
+ }
368
+ });
369
+ }
370
+ // case 3: {'id': true, name: true, ...} as OrderByValue<K>
371
+ else if (common_1.$is.object(given)) {
372
+ let index = 0;
373
+ for (let [k, v] of Object.entries(given)) {
374
+ const field = this._field(k, `orderBy(key=${index})`);
375
+ const asc = this._asc(v, `orderBy.${field}`);
376
+ newOrder.push({ field, asc });
377
+ index++;
378
+ }
379
+ }
380
+ // case: other
381
+ else {
382
+ throw this._invalid(given, `orderBy`, ['string', 'array', 'object'], 'orderBy:body');
383
+ }
384
+ return newOrder;
385
+ }
386
+ _pagination(given, _name) {
387
+ var _a;
388
+ if (common_1.$is.empty(given)) {
389
+ return {};
390
+ }
391
+ // Case 1: PaginationLiteral
392
+ if (Array.isArray(given)) {
393
+ if (given.length < 1) {
394
+ return {};
395
+ }
396
+ return {
397
+ limit: this._num(given[0], `pagination[0]`, 1),
398
+ offset: this._num(given[1], `pagination[1]`, 0)
399
+ };
400
+ }
401
+ // Case 2: PaginationPage | PaginationLimit
402
+ else if (common_1.$is.object(given)) {
403
+ if (Object.keys(given).length < 1) {
404
+ return {};
405
+ }
406
+ const obj = given;
407
+ if (!common_1.$is.empty(obj.page)) {
408
+ const page = this._num(obj.page, `pagination.page`, 1);
409
+ const size = (_a = this._num(obj.size, `pagination.size`, 1)) !== null && _a !== void 0 ? _a : 50;
410
+ ['limit', 'offset'].forEach(f => {
411
+ if (!common_1.$is.empty(given[f])) {
412
+ throw this._error('page:conflict', 'If you give page; limit and offset can not be used anymore', `pagination.page`);
413
+ }
414
+ });
415
+ return {
416
+ limit: size,
417
+ offset: (page - 1) * size
418
+ };
419
+ }
420
+ if (!common_1.$is.empty(obj.limit)) {
421
+ ['page', 'size'].forEach(f => {
422
+ if (!common_1.$is.empty(obj[f])) {
423
+ throw this._error('limit:conflict', 'If you give limit; page and size can not be used anymore', `pagination.limit`);
424
+ }
425
+ });
426
+ return {
427
+ limit: this._num(obj.limit, `pagination.limit`, 1),
428
+ offset: this._num(obj.offset, `pagination.offset`, 0)
429
+ };
430
+ }
431
+ throw this._error('pagination:invalid-key', 'Pagination should have limit/offset or page/size keys', 'pagination');
432
+ }
433
+ throw this._invalid(given, 'pagination', ['array', 'object'], 'pagination:invalid-type');
434
+ }
435
+ // endregion parts
436
+ exec(query, availableFields, name) {
437
+ return {
438
+ select: this._select(query === null || query === void 0 ? void 0 : query.select, availableFields, name),
439
+ where: this._where('where', query === null || query === void 0 ? void 0 : query.where, availableFields, name),
440
+ having: this._where('having', query === null || query === void 0 ? void 0 : query.having, availableFields, name),
441
+ groupBy: this._groupBy(query === null || query === void 0 ? void 0 : query.groupBy, availableFields, name),
442
+ orderBy: this._orderBy(query === null || query === void 0 ? void 0 : query.orderBy, availableFields, name),
443
+ pagination: this._pagination(query === null || query === void 0 ? void 0 : query.pagination, name),
444
+ };
445
+ }
446
+ }
447
+ // noinspection JSUnusedGlobalSymbols
448
+ 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<K extends string> {
7
+ select?: SelectAny<K>;
8
+ where?: WhereAny<K>;
9
+ having?: WhereAny<K>;
10
+ groupBy?: GroupByAny<K>;
11
+ orderBy?: OrderByAny<K>;
12
+ pagination?: PaginationAny;
13
+ }
14
+ export interface QueryRegular<K extends string> {
15
+ select: Select<K>;
16
+ where: Where<K>;
17
+ having: Where<K>;
18
+ groupBy: GroupBy<K>;
19
+ orderBy: OrderBy<K>;
20
+ pagination: PaginationLimit;
21
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,7 @@
1
+ import type { FieldAs, FieldRaw, FieldRegular } from "../field";
2
+ export type SelectAny<K extends string> = '*' | Array<K | [K, string] | SelectGiven<K> | SelectGivenRaw>;
3
+ export type SelectGiven<K extends string> = FieldRegular<K> & FieldAs;
4
+ export type SelectGivenRaw = FieldRaw & FieldAs;
5
+ export type Select<K extends string> = true | Array<SelectItemRegular<K> | SelectItemRaw>;
6
+ export type SelectItemRegular<K extends string> = FieldRegular<K> & FieldAs;
7
+ export type SelectItemRaw = FieldRaw & FieldAs;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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,20 @@
1
+ import type { ConditionType } from "../condition";
2
+ import type { FieldRaw, FieldRegular } from "../field";
3
+ export type WhereValue<K extends string> = {
4
+ [P in K]: unknown;
5
+ };
6
+ export type WhereAny<K extends string> = WhereValue<K> | Array<WhereGiven<K> | WhereGivenRaw | [K, unknown]>;
7
+ export interface WhereGivenCondition {
8
+ eq: string | ConditionType;
9
+ value?: unknown;
10
+ }
11
+ export type WhereGiven<K extends string> = FieldRegular<K> & WhereGivenCondition;
12
+ export type WhereGivenRaw = FieldRaw & WhereGivenCondition;
13
+ export type Where<K extends string> = Array<WhereItemRegular<K> | WhereItemRaw>;
14
+ export type WhereItemRegular<K extends string> = FieldRegular<K> & WhereCondition;
15
+ export type WhereItemRaw = FieldRaw & WhereCondition;
16
+ export interface WhereCondition {
17
+ fullRaw?: true;
18
+ eq?: ConditionType;
19
+ value?: unknown;
20
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leyyo/query",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Query common component",
5
5
  "keywords": [
6
6
  "Query"
@@ -1,7 +0,0 @@
1
- <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
2
- <svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
3
- <rect width="24" height="24" rx="4" fill="#03a56f" />
4
- <g transform="scale(0.9)" transform-origin="12 12" stroke-width="1">
5
- <path 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" stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
6
- </g>
7
- </svg>
@@ -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
- }
File without changes