@nest-extended/core 0.0.1-beta-1 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { Document, Query } from 'mongoose';
2
+ export declare function nestify<T extends Document, Q extends Query<any, T, any> = Query<any, T, any>>(q: Q, filters: NestifyFilters, options: NestifyOptions, isSingleOperation?: boolean, isPaginationDisabled?: boolean): void;
3
+ //# sourceMappingURL=nestify.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nestify.d.ts","sourceRoot":"","sources":["../../src/common/nestify.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAE3C,wBAAgB,OAAO,CACrB,CAAC,SAAS,QAAQ,EAClB,CAAC,SAAS,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,GAAG,CAAC,EAEjD,CAAC,EAAE,CAAC,EACJ,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,cAAc,EACvB,iBAAiB,CAAC,EAAE,OAAO,EAC3B,oBAAoB,CAAC,EAAE,OAAO,GAC7B,IAAI,CAmCN"}
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.nestify = nestify;
4
+ function nestify(q, filters, options, isSingleOperation, isPaginationDisabled) {
5
+ if (Array.isArray(filters.$select)) {
6
+ const selectFields = filters.$select.reduce((res, key) => {
7
+ res[key] = 1;
8
+ return res;
9
+ }, {});
10
+ q.select(selectFields);
11
+ }
12
+ else if (typeof filters.$select === 'string' || typeof filters.$select === 'object') {
13
+ q.select(filters.$select);
14
+ }
15
+ if (filters.$populate && options.defaultPagination) {
16
+ q.populate(filters.$populate);
17
+ }
18
+ if (filters.$sort) {
19
+ q.sort(filters.$sort);
20
+ }
21
+ if (!isPaginationDisabled && !isSingleOperation) {
22
+ const limit = Number(filters.$limit) || options.defaultLimit;
23
+ if (limit > 0) {
24
+ q.limit(limit);
25
+ }
26
+ const skip = Number(filters.$skip) || options.defaultSkip;
27
+ if (skip > 0) {
28
+ q.skip(skip);
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Warning: Modify this properly!
3
+ */
4
+ export declare const options: {
5
+ deleteKey: string;
6
+ defaultPagination: boolean;
7
+ defaultLimit: number;
8
+ defaultSkip: number;
9
+ multi: boolean;
10
+ };
11
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/common/options.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,OAAO;;;;;;CAMnB,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.options = void 0;
4
+ /**
5
+ * Warning: Modify this properly!
6
+ */
7
+ exports.options = {
8
+ deleteKey: 'deleted',
9
+ defaultPagination: true,
10
+ defaultLimit: 20,
11
+ defaultSkip: 0,
12
+ multi: false,
13
+ };
@@ -0,0 +1,14 @@
1
+ export declare const FILTERS: Record<string, (value: any, options?: any) => any>;
2
+ export declare function parse(number?: any): number | undefined;
3
+ export declare const rawQuery: (query: Record<string, any>) => Record<string, any>;
4
+ export declare const OPERATORS: string[];
5
+ export declare const filterQuery: (query: Record<string, any>, options?: {
6
+ filters?: Record<string, (value: any, options?: any) => any>;
7
+ operators?: string[];
8
+ }) => {
9
+ filters: Record<string, any>;
10
+ query: Record<string, any>;
11
+ };
12
+ export declare const assignFilters: (object: Record<string, any>, query: Record<string, any>, filters: Record<string, (value: any, options?: any) => any> | string[], options: any) => Record<string, any>;
13
+ export declare const cleanQuery: (query: any, operators: string[], filters: Record<string, any>) => any;
14
+ //# sourceMappingURL=query.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.utils.d.ts","sourceRoot":"","sources":["../../src/common/query.utils.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,GAAG,CAMtE,CAAC;AAEF,wBAAgB,KAAK,CAAC,MAAM,CAAC,EAAE,GAAG,GAAG,MAAM,GAAG,SAAS,CAKtD;AAWD,eAAO,MAAM,QAAQ,UAAW,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAmBvE,CAAC;AAYF,eAAO,MAAM,SAAS,UAA8D,CAAC;AAErF,eAAO,MAAM,WAAW,UACb,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,YACjB;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,GAAG,CAAC,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE;;WAOlF,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;CAOnC,CAAC;AAEF,eAAO,MAAM,aAAa,WACd,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,SACpB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,WACjB,MAAM,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,GAAG,KAAK,GAAG,CAAC,GAAG,MAAM,EAAE,WAC7D,GAAG,KACb,MAAM,CAAC,MAAM,EAAE,GAAG,CAgBpB,CAAC;AAEF,eAAO,MAAM,UAAU,UACZ,GAAG,aACC,MAAM,EAAE,WACV,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAC7B,GAcF,CAAC"}
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cleanQuery = exports.assignFilters = exports.filterQuery = exports.OPERATORS = exports.rawQuery = exports.FILTERS = void 0;
4
+ exports.parse = parse;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const mongoose_1 = require("mongoose");
8
+ const lodash_1 = tslib_1.__importDefault(require("lodash"));
9
+ exports.FILTERS = {
10
+ $sort: (value) => convertSort(value),
11
+ $limit: (value, options) => getLimit(parse(value), options.paginate),
12
+ $skip: (value) => parse(value),
13
+ $select: (value) => value,
14
+ $populate: (value) => value,
15
+ };
16
+ function parse(number) {
17
+ if (typeof number !== 'undefined') {
18
+ return Math.abs(parseInt(number, 10));
19
+ }
20
+ return undefined;
21
+ }
22
+ function getLimit(limit, paginate) {
23
+ if (paginate?.default) {
24
+ const lower = typeof limit === 'number' && !isNaN(limit) ? limit : paginate.default;
25
+ const upper = typeof paginate.max === 'number' ? paginate.max : Number.MAX_VALUE;
26
+ return Math.min(lower, upper);
27
+ }
28
+ return limit ?? 0;
29
+ }
30
+ const rawQuery = (query) => {
31
+ const rawQ = {};
32
+ for (const key in query) {
33
+ if (Object.prototype.hasOwnProperty.call(query, key)) {
34
+ if (key.startsWith('$')) {
35
+ const filterKey = key.slice(1);
36
+ if (filterKey === 'regex') {
37
+ const field = Object.keys(query[key])[0];
38
+ const regexPattern = query[key][field];
39
+ rawQ[field] = { $regex: new RegExp(regexPattern, 'i') };
40
+ }
41
+ else if (filterKey === 'or' && Array.isArray(query[key])) {
42
+ rawQ['$or'] = query[key].map((subQuery) => (0, exports.rawQuery)(subQuery));
43
+ }
44
+ }
45
+ else {
46
+ rawQ[key] = mongoose_1.Types.ObjectId.isValid(String(query[key])) ? new mongoose_1.Types.ObjectId(query[key]) : query[key];
47
+ }
48
+ }
49
+ }
50
+ return rawQ;
51
+ };
52
+ exports.rawQuery = rawQuery;
53
+ function convertSort(sort) {
54
+ if (typeof sort !== 'object' || Array.isArray(sort)) {
55
+ return sort;
56
+ }
57
+ return Object.keys(sort).reduce((result, key) => {
58
+ result[key] = typeof sort[key] === 'object' ? sort[key] : parseInt(sort[key], 10);
59
+ return result;
60
+ }, {});
61
+ }
62
+ exports.OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or'];
63
+ const filterQuery = (query, options = {}) => {
64
+ const additionalFilters = options.filters ?? {};
65
+ const additionalOperators = options.operators ?? [];
66
+ const result = {
67
+ filters: (0, exports.assignFilters)({}, query, exports.FILTERS, options),
68
+ query: {},
69
+ };
70
+ result.filters = (0, exports.assignFilters)(result.filters, query, additionalFilters, options);
71
+ result.query = (0, exports.cleanQuery)(query, [...exports.OPERATORS, ...additionalOperators], result.filters);
72
+ return result;
73
+ };
74
+ exports.filterQuery = filterQuery;
75
+ const assignFilters = (object, query, filters, options) => {
76
+ if (Array.isArray(filters)) {
77
+ filters.forEach((key) => {
78
+ if (query[key] !== undefined) {
79
+ object[key] = query[key];
80
+ }
81
+ });
82
+ }
83
+ else {
84
+ Object.entries(filters).forEach(([key, converter]) => {
85
+ const converted = converter(query[key], options);
86
+ if (converted !== undefined) {
87
+ object[key] = converted;
88
+ }
89
+ });
90
+ }
91
+ return object;
92
+ };
93
+ exports.assignFilters = assignFilters;
94
+ const cleanQuery = (query, operators, filters) => {
95
+ if (Array.isArray(query)) {
96
+ return query.map((value) => (0, exports.cleanQuery)(value, operators, filters));
97
+ }
98
+ else if (lodash_1.default.isPlainObject(query)) {
99
+ const result = {};
100
+ Object.entries(query).forEach(([key, value]) => {
101
+ if (key.startsWith('$') && filters[key] === undefined && !operators.includes(key)) {
102
+ throw new common_1.BadRequestException(`Invalid query parameter: ${key}`);
103
+ }
104
+ result[key] = (0, exports.cleanQuery)(value, operators, filters);
105
+ });
106
+ return result;
107
+ }
108
+ return query;
109
+ };
110
+ exports.cleanQuery = cleanQuery;
@@ -0,0 +1,7 @@
1
+ export * from './lib/core';
2
+ export * from './common/nestify';
3
+ export * from './common/options';
4
+ export * from './common/query.utils';
5
+ export * from './types/PaginatedResponse';
6
+ export * from './types/ServiceOptions';
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/core"), exports);
5
+ tslib_1.__exportStar(require("./common/nestify"), exports);
6
+ tslib_1.__exportStar(require("./common/options"), exports);
7
+ tslib_1.__exportStar(require("./common/query.utils"), exports);
8
+ tslib_1.__exportStar(require("./types/PaginatedResponse"), exports);
9
+ tslib_1.__exportStar(require("./types/ServiceOptions"), exports);
@@ -0,0 +1,2 @@
1
+ export declare function core(): string;
2
+ //# sourceMappingURL=core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/lib/core.ts"],"names":[],"mappings":"AAAA,wBAAgB,IAAI,IAAI,MAAM,CAE7B"}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.core = core;
4
+ function core() {
5
+ return 'core';
6
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nest-extended/core",
3
- "version": "0.0.1-beta-1",
3
+ "version": "0.0.1",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "publishConfig": {