@leyyo/query 1.2.1 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { QueryErrorCode } from "./index.types";
2
- export declare class InvalidQueryValueError extends Error {
2
+ import { LeyyoError } from "@leyyo/common";
3
+ export declare class InvalidQueryValueError extends LeyyoError {
3
4
  readonly message: string;
4
5
  readonly path: string;
5
6
  constructor(code: QueryErrorCode, message: string, path: string);
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InvalidQueryValueError = void 0;
4
- class InvalidQueryValueError extends Error {
4
+ const common_1 = require("@leyyo/common");
5
+ class InvalidQueryValueError extends common_1.LeyyoError {
5
6
  constructor(code, message, path) {
6
7
  super(`[${code}] ${message} at ${path}`);
7
8
  this.message = message;
@@ -1,4 +1,4 @@
1
- import type { FieldRaw, FieldRegular } from "../field";
1
+ import { FieldRaw, FieldRegular } from "../field";
2
2
  export type GroupByAny<K extends string> = Array<K | GroupByGivenRegular<K> | GroupByGivenRaw>;
3
3
  export type GroupByGivenRegular<K extends string> = FieldRegular<K>;
4
4
  export type GroupByGivenRaw = FieldRaw;
@@ -1,3 +1,8 @@
1
- export declare const ConditionTypeItems: readonly ["eq", "ne", "null", "!null", "missing", "!missing", "gt", "gte", "lt", "lte", "between", "!between", "in", "!in", "starts", "!starts", "ends", "!ends", "matches", "!matches", "contains", "!contains", "contained", "!contained", "true", "false", "includes", "!includes", "intersects", "!intersects", "exists", "!exists"];
2
- export type OperationType = typeof ConditionTypeItems[number];
1
+ declare const literals: readonly ["eq", "ne", "null", "!null", "missing", "!missing", "gt", "gte", "lt", "lte", "between", "!between", "in", "!in", "starts", "!starts", "ends", "!ends", "matches", "!matches", "contains", "!contains", "contained", "!contained", "true", "false", "includes", "!includes", "intersects", "!intersects", "exists", "!exists"];
2
+ /**
3
+ * Operation Type
4
+ * */
5
+ export type OperationType = typeof literals[number];
6
+ export declare const OperationTypeItems: ReadonlyArray<OperationType>;
3
7
  export declare const OperationTypeMap: Record<string, OperationType>;
8
+ export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OperationTypeMap = exports.ConditionTypeItems = void 0;
4
- exports.ConditionTypeItems = [
3
+ exports.OperationTypeMap = exports.OperationTypeItems = void 0;
4
+ const literals = [
5
5
  // all
6
6
  'eq',
7
7
  'ne',
@@ -41,6 +41,7 @@ exports.ConditionTypeItems = [
41
41
  'exists',
42
42
  '!exists',
43
43
  ];
44
+ exports.OperationTypeItems = literals;
44
45
  // noinspection JSUnusedGlobalSymbols
45
46
  exports.OperationTypeMap = {
46
47
  // all
@@ -1,4 +1,4 @@
1
- import type { FieldRaw, FieldRegular } from "../field";
1
+ import { FieldRaw, FieldRegular } from "../field";
2
2
  export type OrderType = 'asc' | 'desc';
3
3
  export type OrderByAny<K extends string> = K | Array<OrderByGiven<K> | K | OrderByGivenRaw> | OrderByValue<K>;
4
4
  export type OrderByValue<K extends string> = {
@@ -1,4 +1,4 @@
1
- import type { QueryAny, QueryRegular } from "../query";
1
+ import { QueryAny, QueryRegular } from "../query";
2
2
  import { BasicType } from "@leyyo/common";
3
3
  export interface QueryParserLike {
4
4
  exec<K extends string>(query: QueryAny<K>, availableFields: Array<K | string>, name?: string): QueryRegular<K>;
@@ -1,2 +1,2 @@
1
- import type { QueryParserLike } from "./index.types";
1
+ import { QueryParserLike } from "./index.types";
2
2
  export declare const queryParser: QueryParserLike;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.queryParser = void 0;
4
+ const common_1 = require("@leyyo/common");
4
5
  const operation_1 = require("../operation");
5
6
  const error_1 = require("../error");
6
7
  const type_1 = require("@leyyo/type");
@@ -14,7 +15,7 @@ class QueryParser {
14
15
  return this._error(code, `It's expected as ${ex}, but it's type: ${typeof value}`, path);
15
16
  }
16
17
  _emptyOrInvalid(value, path, expected, empty, invalid) {
17
- if ((0, type_1.isEmpty)(value)) {
18
+ if ((0, common_1.isEmpty)(value)) {
18
19
  return this._error(empty, `It's empty`, path);
19
20
  }
20
21
  if (typeof value === 'string' && value.trim() === '') {
@@ -23,13 +24,13 @@ class QueryParser {
23
24
  return this._invalid(value, path, expected, invalid);
24
25
  }
25
26
  _asc(value, path) {
26
- if ((0, type_1.isEmpty)(value)) {
27
+ if ((0, common_1.isEmpty)(value)) {
27
28
  return true;
28
29
  }
29
30
  if (typeof value === 'boolean') {
30
31
  return value;
31
32
  }
32
- else if ((0, type_1.isText)(value)) {
33
+ else if ((0, common_1.isText)(value)) {
33
34
  if (value.toLowerCase() === 'asc') {
34
35
  return true;
35
36
  }
@@ -41,33 +42,33 @@ class QueryParser {
41
42
  throw this._invalid(value, path, ['boolean', 'string'], 'asc:invalid-type');
42
43
  }
43
44
  _field(value, path) {
44
- if ((0, type_1.isText)(value)) {
45
+ if ((0, common_1.isText)(value)) {
45
46
  return value;
46
47
  }
47
48
  throw this._emptyOrInvalid(value, path, 'string', 'field:empty', 'field:invalid');
48
49
  }
49
50
  _raw(value, path) {
50
- if ((0, type_1.isText)(value)) {
51
+ if ((0, common_1.isText)(value)) {
51
52
  return value;
52
53
  }
53
54
  throw this._emptyOrInvalid(value, path, 'string', 'raw:empty', 'raw:invalid');
54
55
  }
55
56
  _as(value, path) {
56
- if ((0, type_1.isEmpty)(value)) {
57
+ if ((0, common_1.isEmpty)(value)) {
57
58
  return undefined;
58
59
  }
59
- else if ((0, type_1.isText)(value)) {
60
+ else if ((0, common_1.isText)(value)) {
60
61
  return value;
61
62
  }
62
63
  throw this._invalid(value, path, 'string', 'as:invalid');
63
64
  }
64
65
  _operation(value, path) {
65
- if ((0, type_1.isEmpty)(value)) {
66
+ if ((0, common_1.isEmpty)(value)) {
66
67
  return 'eq';
67
68
  }
68
- else if ((0, type_1.isText)(value)) {
69
+ else if ((0, common_1.isText)(value)) {
69
70
  const key = value;
70
- if ((0, type_1.isLiteral)(key, operation_1.ConditionTypeItems)) {
71
+ if ((0, common_1.isText)(key) && operation_1.OperationTypeItems.includes(key)) {
71
72
  return key;
72
73
  }
73
74
  if (operation_1.OperationTypeMap[key] !== undefined) {
@@ -98,7 +99,7 @@ class QueryParser {
98
99
  if (Array.isArray(value)) {
99
100
  let index = 0;
100
101
  for (const item of value) {
101
- if (!(0, type_1.isText)(item) && !(0, type_1.isNumberValid)(item) && typeof value !== 'boolean') {
102
+ if (!(0, common_1.isText)(item) && typeof value !== 'number' && typeof value !== 'boolean') {
102
103
  throw this._invalid(item, `${path}[${index}]`, ['string', 'number', 'boolean', 'array', 'number'], 'value:invalid-item');
103
104
  }
104
105
  index++;
@@ -110,10 +111,10 @@ class QueryParser {
110
111
  throw this._invalid(value, path, ['string', 'number', 'boolean', 'array', 'number'], 'value:invalid-type');
111
112
  }
112
113
  _num(value, path, min) {
113
- if ((0, type_1.isEmpty)(value)) {
114
+ if ((0, common_1.isEmpty)(value)) {
114
115
  return undefined;
115
116
  }
116
- else if ((0, type_1.isIntegerValid)(value)) {
117
+ else if (Number.isSafeInteger(value)) {
117
118
  if (value >= min) {
118
119
  return value;
119
120
  }
@@ -132,7 +133,7 @@ class QueryParser {
132
133
  // endregion private
133
134
  // region parts
134
135
  _select(given, _availableFields, _name) {
135
- if ((0, type_1.isEmpty)(given)) {
136
+ if ((0, common_1.isEmpty)(given)) {
136
137
  return { all: true };
137
138
  }
138
139
  // Cases:
@@ -151,7 +152,7 @@ class QueryParser {
151
152
  const arr = given;
152
153
  arr.forEach((item, index) => {
153
154
  // Case 2A: K
154
- if ((0, type_1.isText)(item)) {
155
+ if ((0, common_1.isText)(item)) {
155
156
  newSelect.fields.push({
156
157
  field: item,
157
158
  });
@@ -164,15 +165,15 @@ class QueryParser {
164
165
  newSelect.fields.push({ field, as, });
165
166
  }
166
167
  // Case 2C: SelectGiven<K> | SelectGivenRaw
167
- else if ((0, type_1.isObjectBare)(item)) {
168
+ else if ((0, type_1.isBareObject)(item)) {
168
169
  let as;
169
170
  let field;
170
171
  let raw;
171
172
  const obj = item;
172
- if (!(0, type_1.isEmpty)(obj.raw)) {
173
+ if (!(0, common_1.isEmpty)(obj.raw)) {
173
174
  raw = this._raw(obj.raw, `select[${index}].raw`);
174
175
  }
175
- if (!(0, type_1.isEmpty)(obj.field)) {
176
+ if (!(0, common_1.isEmpty)(obj.field)) {
176
177
  field = this._field(obj.field, `select[${index}].field`);
177
178
  }
178
179
  this._fieldXorRaw(field, raw, `select[${index}].field`);
@@ -197,7 +198,7 @@ class QueryParser {
197
198
  return newSelect;
198
199
  }
199
200
  _where(scope, given, _availableFields, _name) {
200
- if ((0, type_1.isEmpty)(given)) {
201
+ if ((0, common_1.isEmpty)(given)) {
201
202
  return [];
202
203
  }
203
204
  // Cases:
@@ -205,7 +206,7 @@ class QueryParser {
205
206
  // 2 - Array<WhereGiven<K>|WhereGivenRaw|[K, unknown]>
206
207
  const newWhere = [];
207
208
  // case 1: WhereValue<K>
208
- if ((0, type_1.isObjectBare)(given)) {
209
+ if ((0, type_1.isBareObject)(given)) {
209
210
  let index = 0;
210
211
  for (let [k, v] of Object.entries(given)) {
211
212
  const field = this._field(k, `${scope}(key=${index})`);
@@ -222,22 +223,22 @@ class QueryParser {
222
223
  const arr = given;
223
224
  arr.forEach((item, index) => {
224
225
  // Case 2A: WhereGiven<K>|WhereGivenRaw
225
- if ((0, type_1.isObjectBare)(item)) {
226
+ if ((0, type_1.isBareObject)(item)) {
226
227
  let field;
227
228
  let raw;
228
229
  let op;
229
230
  let value;
230
231
  let fullRaw;
231
232
  const obj = item;
232
- if (!(0, type_1.isEmpty)(obj.raw)) {
233
+ if (!(0, common_1.isEmpty)(obj.raw)) {
233
234
  raw = this._raw(obj.raw, `${scope}[${index}].raw`);
234
235
  }
235
- if (!(0, type_1.isEmpty)(obj.field)) {
236
+ if (!(0, common_1.isEmpty)(obj.field)) {
236
237
  field = this._field(obj.field, `${scope}[${index}].field`);
237
238
  }
238
239
  this._fieldXorRaw(field, raw, `${scope}[${index}].field`);
239
240
  const whereItem = obj;
240
- if (raw && (0, type_1.isEmpty)(whereItem.op) && (0, type_1.isEmpty)(whereItem.value)) {
241
+ if (raw && (0, common_1.isEmpty)(whereItem.op) && (0, common_1.isEmpty)(whereItem.value)) {
241
242
  fullRaw = true;
242
243
  }
243
244
  op = this._operation(whereItem.op, `${scope}[${index}].op`);
@@ -269,7 +270,7 @@ class QueryParser {
269
270
  return newWhere;
270
271
  }
271
272
  _groupBy(given, _availableFields, _name) {
272
- if ((0, type_1.isEmpty)(given)) {
273
+ if ((0, common_1.isEmpty)(given)) {
273
274
  return [];
274
275
  }
275
276
  // Cases:
@@ -283,14 +284,14 @@ class QueryParser {
283
284
  const arr = given;
284
285
  arr.forEach((item, index) => {
285
286
  // Case 2A: GroupByGivenRegular<K> | GroupByGivenRaw
286
- if ((0, type_1.isObjectBare)(item)) {
287
+ if ((0, type_1.isBareObject)(item)) {
287
288
  let field;
288
289
  let raw;
289
290
  const obj = item;
290
- if (!(0, type_1.isEmpty)(obj.raw)) {
291
+ if (!(0, common_1.isEmpty)(obj.raw)) {
291
292
  raw = this._raw(obj.raw, `groupBy[${index}].raw`);
292
293
  }
293
- if (!(0, type_1.isEmpty)(obj.field)) {
294
+ if (!(0, common_1.isEmpty)(obj.field)) {
294
295
  field = this._field(obj.field, `groupBy[${index}].field`);
295
296
  }
296
297
  this._fieldXorRaw(field, raw, `groupBy[${index}].field`);
@@ -302,7 +303,7 @@ class QueryParser {
302
303
  }
303
304
  }
304
305
  // Case 2B: K
305
- else if ((0, type_1.isText)(item)) {
306
+ else if ((0, common_1.isText)(item)) {
306
307
  newGroup.push({ field: item });
307
308
  }
308
309
  else {
@@ -317,7 +318,7 @@ class QueryParser {
317
318
  return newGroup;
318
319
  }
319
320
  _orderBy(given, _availableFields, _name) {
320
- if ((0, type_1.isEmpty)(given)) {
321
+ if ((0, common_1.isEmpty)(given)) {
321
322
  return [];
322
323
  }
323
324
  // Cases:
@@ -326,7 +327,7 @@ class QueryParser {
326
327
  // 3 - OrderByValue<K>
327
328
  const newOrder = [];
328
329
  // case 1: string as K
329
- if ((0, type_1.isText)(given)) {
330
+ if ((0, common_1.isText)(given)) {
330
331
  newOrder.push({ field: given, asc: true });
331
332
  }
332
333
  // case 2: array as Array<OrderByGiven<K>|K|OrderByGivenRaw>
@@ -337,15 +338,15 @@ class QueryParser {
337
338
  const arr = given;
338
339
  arr.forEach((item, index) => {
339
340
  // Case 2A: OrderByGiven<K>|OrderByGivenRaw
340
- if ((0, type_1.isObjectBare)(item)) {
341
+ if ((0, type_1.isBareObject)(item)) {
341
342
  let asc;
342
343
  let field;
343
344
  let raw;
344
345
  const obj = item;
345
- if (!(0, type_1.isEmpty)(obj.raw)) {
346
+ if (!(0, common_1.isEmpty)(obj.raw)) {
346
347
  raw = this._raw(obj.raw, `orderBy[${index}].raw`);
347
348
  }
348
- if (!(0, type_1.isEmpty)(obj.field)) {
349
+ if (!(0, common_1.isEmpty)(obj.field)) {
349
350
  field = this._field(obj.field, `orderBy[${index}].field`);
350
351
  }
351
352
  this._fieldXorRaw(field, raw, `orderBy[${index}].field`);
@@ -358,7 +359,7 @@ class QueryParser {
358
359
  }
359
360
  }
360
361
  // Case 2B: K
361
- else if ((0, type_1.isText)(item)) {
362
+ else if ((0, common_1.isText)(item)) {
362
363
  newOrder.push({ field: this._field(item, ''), asc: true });
363
364
  }
364
365
  // other
@@ -368,7 +369,7 @@ class QueryParser {
368
369
  });
369
370
  }
370
371
  // case 3: {'id': true, name: true, ...} as OrderByValue<K>
371
- else if ((0, type_1.isObjectBare)(given)) {
372
+ else if ((0, type_1.isBareObject)(given)) {
372
373
  let index = 0;
373
374
  for (let [k, v] of Object.entries(given)) {
374
375
  const field = this._field(k, `orderBy(key=${index})`);
@@ -385,7 +386,7 @@ class QueryParser {
385
386
  }
386
387
  _pagination(given, _name) {
387
388
  var _a;
388
- if ((0, type_1.isEmpty)(given)) {
389
+ if ((0, common_1.isEmpty)(given)) {
389
390
  return {};
390
391
  }
391
392
  // Case 1: PaginationLiteral
@@ -399,16 +400,16 @@ class QueryParser {
399
400
  };
400
401
  }
401
402
  // Case 2: PaginationPage | PaginationLimit
402
- else if ((0, type_1.isObjectBare)(given)) {
403
+ else if ((0, type_1.isBareObject)(given)) {
403
404
  if (Object.keys(given).length < 1) {
404
405
  return {};
405
406
  }
406
407
  const obj = given;
407
- if (!(0, type_1.isEmpty)(obj.page)) {
408
+ if (!(0, common_1.isEmpty)(obj.page)) {
408
409
  const page = this._num(obj.page, `pagination.page`, 1);
409
410
  const size = (_a = this._num(obj.size, `pagination.size`, 1)) !== null && _a !== void 0 ? _a : 50;
410
411
  ['limit', 'offset'].forEach(f => {
411
- if (!(0, type_1.isEmpty)(given[f])) {
412
+ if (!(0, common_1.isEmpty)(given[f])) {
412
413
  throw this._error('page:conflict', 'If you give page; limit and offset can not be used anymore', `pagination.page`);
413
414
  }
414
415
  });
@@ -417,9 +418,9 @@ class QueryParser {
417
418
  offset: (page - 1) * size
418
419
  };
419
420
  }
420
- if (!(0, type_1.isEmpty)(obj.limit)) {
421
+ if (!(0, common_1.isEmpty)(obj.limit)) {
421
422
  ['page', 'size'].forEach(f => {
422
- if (!(0, type_1.isEmpty)(obj[f])) {
423
+ if (!(0, common_1.isEmpty)(obj[f])) {
423
424
  throw this._error('limit:conflict', 'If you give limit; page and size can not be used anymore', `pagination.limit`);
424
425
  }
425
426
  });
@@ -1,8 +1,8 @@
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";
1
+ import { Select, SelectAny } from "../select";
2
+ import { Where, WhereAny } from "../where";
3
+ import { GroupBy, GroupByAny } from "../group-by";
4
+ import { OrderBy, OrderByAny } from "../order-by";
5
+ import { PaginationAny, PaginationLimit } from "../pagination";
6
6
  export interface QueryAny<K extends string> {
7
7
  select?: SelectAny<K>;
8
8
  where?: WhereAny<K>;
@@ -1,4 +1,4 @@
1
- import type { FieldAs, FieldRaw, FieldRegular } from "../field";
1
+ import { FieldAs, FieldRaw, FieldRegular } from "../field";
2
2
  export type SelectAny<K extends string> = '*' | Array<K | [K, string] | SelectGiven<K> | SelectGivenRaw>;
3
3
  export type SelectGiven<K extends string> = FieldRegular<K> & FieldAs;
4
4
  export type SelectGivenRaw = FieldRaw & FieldAs;
@@ -1,5 +1,5 @@
1
- import type { OperationType } from "../operation";
2
- import type { FieldRaw, FieldRegular } from "../field";
1
+ import { OperationType } from "../operation";
2
+ import { FieldRaw, FieldRegular } from "../field";
3
3
  export type WhereValue<K extends string> = {
4
4
  [P in K]: unknown;
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leyyo/query",
3
- "version": "1.2.1",
3
+ "version": "1.3.1",
4
4
  "description": "Query common component",
5
5
  "keywords": [
6
6
  "Query"
@@ -66,7 +66,7 @@
66
66
  }
67
67
  },
68
68
  "dependencies": {
69
- "@leyyo/common": "^1.2.1",
70
- "@leyyo/type": "^1.1.1"
69
+ "@leyyo/common": "^1.3.7",
70
+ "@leyyo/type": "^1.3.3"
71
71
  }
72
72
  }