@ewb/mongoose-query 1.1.17 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Query.d.ts +0 -25
- package/lib/Query.js +3 -2
- package/lib/QueryError.d.ts +0 -25
- package/lib/QueryError.js +1 -2
- package/lib/QueryHistory.d.ts +3 -26
- package/lib/QueryHistory.js +5 -3
- package/lib/QueryModel.d.ts +25 -37
- package/lib/QueryModel.js +12 -8
- package/lib/QueryOptions.d.ts +3 -28
- package/lib/QueryOptions.js +2 -1
- package/lib/src/Query.d.ts +64 -0
- package/lib/src/Query.js +250 -0
- package/lib/src/QueryError.d.ts +16 -0
- package/lib/src/QueryError.js +52 -0
- package/lib/src/QueryHistory.d.ts +77 -0
- package/lib/src/QueryHistory.js +137 -0
- package/lib/src/QueryModel.d.ts +58 -0
- package/lib/src/QueryModel.js +250 -0
- package/lib/src/QueryOptions.d.ts +64 -0
- package/lib/src/QueryOptions.js +200 -0
- package/lib/src/index.d.ts +6 -0
- package/lib/src/index.js +6 -0
- package/lib/src/utils/Query.utils.d.ts +40 -0
- package/lib/src/utils/Query.utils.js +144 -0
- package/lib/src/utils/constants.d.ts +1 -0
- package/lib/src/utils/constants.js +1 -0
- package/lib/src/utils/index.d.ts +3 -0
- package/lib/src/utils/index.js +3 -0
- package/lib/src/utils/types.d.ts +23 -0
- package/lib/src/utils/types.js +1 -0
- package/lib/src/utils/utils.d.ts +8 -0
- package/lib/src/utils/utils.js +38 -0
- package/lib/utils/Query.utils.d.ts +4 -29
- package/lib/utils/types.d.ts +1 -26
- package/package.json +21 -29
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { toValidTextRegexp } from '../QueryOptions.js';
|
|
2
|
+
const VALUE_SPLIT = /,|\s/;
|
|
3
|
+
const POPULATE_SPLIT = ';';
|
|
4
|
+
const POPULATE_PATH_SPLIT = ':';
|
|
5
|
+
export const toValidNumber = (v, defaultV) => (typeof v === 'number' ? Number(v) : v ? parseInt(v, 10) : defaultV);
|
|
6
|
+
export function mongooseQueryOptions(query) {
|
|
7
|
+
const { skip, limit, page } = mongooseQuerySkipLimit(query);
|
|
8
|
+
const { select, sort } = mongooseQuerySortSelect(query);
|
|
9
|
+
const populate = mongooseQueryPopulate(query);
|
|
10
|
+
return {
|
|
11
|
+
limit,
|
|
12
|
+
skip,
|
|
13
|
+
sort,
|
|
14
|
+
select,
|
|
15
|
+
populate,
|
|
16
|
+
page,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export function mongooseQuerySkipLimit(query) {
|
|
20
|
+
try {
|
|
21
|
+
let skip = 0;
|
|
22
|
+
let limit = 20;
|
|
23
|
+
let page;
|
|
24
|
+
if ('skip' in query) {
|
|
25
|
+
skip = toValidNumber(query.skip, 0);
|
|
26
|
+
delete query.skip;
|
|
27
|
+
}
|
|
28
|
+
if ('limit' in query) {
|
|
29
|
+
limit = toValidNumber(query.limit, limit);
|
|
30
|
+
delete query.limit;
|
|
31
|
+
}
|
|
32
|
+
if ('page' in query) {
|
|
33
|
+
const _page = toValidNumber(query.page, 0);
|
|
34
|
+
if (_page === 0) {
|
|
35
|
+
skip = undefined;
|
|
36
|
+
limit = undefined;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
skip = limit * (_page - 1);
|
|
40
|
+
page = _page;
|
|
41
|
+
}
|
|
42
|
+
delete query.page;
|
|
43
|
+
}
|
|
44
|
+
return { skip, limit, page };
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
return { skip: 0, limit: 20 };
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function mongooseQuerySortSelect(query) {
|
|
51
|
+
try {
|
|
52
|
+
let sort = [];
|
|
53
|
+
let select = [];
|
|
54
|
+
if ('sort' in query && query.sort) {
|
|
55
|
+
sort = query.sort.split(VALUE_SPLIT).filter(Boolean);
|
|
56
|
+
delete query.sort;
|
|
57
|
+
}
|
|
58
|
+
if ('select' in query && query.select) {
|
|
59
|
+
select = query.select.split(VALUE_SPLIT).filter(Boolean);
|
|
60
|
+
delete query.select;
|
|
61
|
+
}
|
|
62
|
+
return { sort, select };
|
|
63
|
+
}
|
|
64
|
+
catch (e) {
|
|
65
|
+
return { sort: [], select: [] };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function mongooseQueryPopulate(query) {
|
|
69
|
+
try {
|
|
70
|
+
const populate = [];
|
|
71
|
+
if ('$populate' in query || 'populate' in query) {
|
|
72
|
+
const populateValue = String(query.populate || query.$populate || '');
|
|
73
|
+
if (populateValue) {
|
|
74
|
+
const pathsToPopulate = populateValue
|
|
75
|
+
.split(POPULATE_SPLIT)
|
|
76
|
+
.filter(Boolean);
|
|
77
|
+
for (const pathToPopulate of pathsToPopulate) {
|
|
78
|
+
const [path, selects] = pathToPopulate.split(POPULATE_PATH_SPLIT);
|
|
79
|
+
if (path) {
|
|
80
|
+
populate.push({
|
|
81
|
+
path,
|
|
82
|
+
...(selects ? { select: selects.split(VALUE_SPLIT) } : {}),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
delete query.populate;
|
|
88
|
+
delete query.$populate;
|
|
89
|
+
}
|
|
90
|
+
return populate;
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const splitKey = (key) => {
|
|
97
|
+
const [operation, ...field] = String(key).split('_');
|
|
98
|
+
return [operation, field.join('_')];
|
|
99
|
+
};
|
|
100
|
+
export function mongooseQueryWithOperation(query, queryKey, queryValue) {
|
|
101
|
+
try {
|
|
102
|
+
const _key = String(queryKey);
|
|
103
|
+
if (_key.match(/^\$gte?_/) || _key.match(/^\$lte?_/)) {
|
|
104
|
+
const [operation, field] = splitKey(_key);
|
|
105
|
+
return greaterLessThanValue(query, operation, field, queryValue);
|
|
106
|
+
}
|
|
107
|
+
else if (_key.match(/^\$n?in_/)) {
|
|
108
|
+
const [operation, field] = splitKey(_key);
|
|
109
|
+
return toMongooseOperation(query, operation, field, queryValue);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
const field = _key.slice(1);
|
|
113
|
+
return {
|
|
114
|
+
field,
|
|
115
|
+
value: {
|
|
116
|
+
$regex: new RegExp(toValidTextRegexp(queryValue), 'i'),
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
return { field: '', value: '' };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export function greaterLessThanValue(query, operation, field, queryValue) {
|
|
126
|
+
let value;
|
|
127
|
+
if (query[field]) {
|
|
128
|
+
value = {
|
|
129
|
+
...query[field],
|
|
130
|
+
[operation]: new Date(queryValue),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
value = { [operation]: new Date(queryValue) };
|
|
135
|
+
}
|
|
136
|
+
return { field, value };
|
|
137
|
+
}
|
|
138
|
+
const SORT_SPLIT = /,|\s/;
|
|
139
|
+
export function toMongooseOperation(query, operation, field, queryValue) {
|
|
140
|
+
const value = {
|
|
141
|
+
[operation]: queryValue.split(SORT_SPLIT).filter(Boolean),
|
|
142
|
+
};
|
|
143
|
+
return { field, value };
|
|
144
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DEFAULT_LIMIT = 20;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_LIMIT = 20;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
export type MongooseSortSelectValue<T extends object, K extends keyof T = keyof T> = K extends string ? `-${K}` | K : keyof T;
|
|
3
|
+
export type QueryType<T extends object> = mongoose.FilterQuery<T>;
|
|
4
|
+
export type IQueryAddToModelQuery<R extends object, T extends object> = {
|
|
5
|
+
[key in keyof R]?: QueryType<T>;
|
|
6
|
+
};
|
|
7
|
+
export interface IQueryOptions {
|
|
8
|
+
score?: {
|
|
9
|
+
$meta: 'textScore';
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export interface IMongooseQueryOptions {
|
|
13
|
+
skip?: number | string;
|
|
14
|
+
limit?: number | string;
|
|
15
|
+
page?: number | string;
|
|
16
|
+
text?: string;
|
|
17
|
+
$text?: string;
|
|
18
|
+
sort?: string;
|
|
19
|
+
select?: string;
|
|
20
|
+
populate?: string;
|
|
21
|
+
$populate?: string;
|
|
22
|
+
[key: string | number | symbol]: string | number | undefined;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const addInRange = (pages, currentPage) => (add) => {
|
|
2
|
+
const sum = currentPage + add;
|
|
3
|
+
if (sum < 0)
|
|
4
|
+
return '0';
|
|
5
|
+
if (sum > pages)
|
|
6
|
+
return String(pages);
|
|
7
|
+
return String(sum);
|
|
8
|
+
};
|
|
9
|
+
const constructUrl = (rootUrl) => {
|
|
10
|
+
const [url, urlQuery] = rootUrl.split('?');
|
|
11
|
+
const query = new URLSearchParams(urlQuery);
|
|
12
|
+
return (rel, page, limit = String(query.get('limit'))) => {
|
|
13
|
+
query.set('page', page);
|
|
14
|
+
query.set('limit', limit);
|
|
15
|
+
return `<${url}?${query.toString()}>; rel="${rel}"`;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export function getPageLinkHeader(rootUrl, query, data) {
|
|
19
|
+
if (!query.page)
|
|
20
|
+
return '';
|
|
21
|
+
if (data.count < data.limit)
|
|
22
|
+
return '';
|
|
23
|
+
const currentPage = query.page;
|
|
24
|
+
const pages = Math.floor(data.count / data.limit);
|
|
25
|
+
const getPage = addInRange(pages, currentPage);
|
|
26
|
+
const toUrl = constructUrl(rootUrl);
|
|
27
|
+
const next = toUrl('next', getPage(1));
|
|
28
|
+
const prevSkip = getPage(-1);
|
|
29
|
+
const prev = currentPage > 1 ? toUrl('prev', prevSkip) : '';
|
|
30
|
+
const first = currentPage > 1 ? toUrl('first', '1') : '';
|
|
31
|
+
const last = toUrl('last', String(pages), String(data.count - pages * data.limit));
|
|
32
|
+
let link = `${next},${last}`;
|
|
33
|
+
if (first)
|
|
34
|
+
link += `,${first}`;
|
|
35
|
+
if (prev)
|
|
36
|
+
link += `,${prev}`;
|
|
37
|
+
return link;
|
|
38
|
+
}
|
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate.js" />
|
|
2
|
-
/// <reference types="mongoose/types/callback.js" />
|
|
3
|
-
/// <reference types="mongoose/types/collection.js" />
|
|
4
|
-
/// <reference types="mongoose/types/connection.js" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor.js" />
|
|
6
|
-
/// <reference types="mongoose/types/document.js" />
|
|
7
|
-
/// <reference types="mongoose/types/error.js" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions.js" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers.js" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares.js" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes.js" />
|
|
12
|
-
/// <reference types="mongoose/types/models.js" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions.js" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage.js" />
|
|
15
|
-
/// <reference types="mongoose/types/populate.js" />
|
|
16
|
-
/// <reference types="mongoose/types/query.js" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions.js" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes.js" />
|
|
19
|
-
/// <reference types="mongoose/types/session.js" />
|
|
20
|
-
/// <reference types="mongoose/types/types.js" />
|
|
21
|
-
/// <reference types="mongoose/types/utility.js" />
|
|
22
|
-
/// <reference types="mongoose/types/validation.js" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals.js" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype.js" />
|
|
25
|
-
/// <reference types="mongoose/types/inferrawdoctype.js" />
|
|
26
1
|
import mongoose from 'mongoose';
|
|
27
2
|
import { IMongooseQueryOptions } from './types.js';
|
|
28
3
|
export declare const toValidNumber: (v: string | number | undefined, defaultV: number) => number;
|
|
@@ -39,27 +14,27 @@ export declare function mongooseQuerySkipLimit(query: IMongooseQueryOptions): {
|
|
|
39
14
|
limit: number;
|
|
40
15
|
page: number;
|
|
41
16
|
} | {
|
|
17
|
+
page?: undefined;
|
|
42
18
|
skip: number;
|
|
43
19
|
limit: number;
|
|
44
|
-
page?: undefined;
|
|
45
20
|
};
|
|
46
21
|
export declare function mongooseQuerySortSelect(query: IMongooseQueryOptions): {
|
|
47
22
|
sort: any[];
|
|
48
23
|
select: any[];
|
|
49
24
|
};
|
|
50
25
|
export declare function mongooseQueryPopulate(query: IMongooseQueryOptions): any[];
|
|
51
|
-
export declare function mongooseQueryWithOperation<T extends object, Q extends mongoose.
|
|
26
|
+
export declare function mongooseQueryWithOperation<T extends object, Q extends mongoose.QueryFilter<T>, K extends keyof Q>(query: Q, queryKey: K, queryValue: string): {
|
|
52
27
|
field: K;
|
|
53
28
|
value: Q[K];
|
|
54
29
|
} | {
|
|
55
30
|
field: string;
|
|
56
31
|
value: string;
|
|
57
32
|
};
|
|
58
|
-
export declare function greaterLessThanValue<T extends object, Q extends mongoose.
|
|
33
|
+
export declare function greaterLessThanValue<T extends object, Q extends mongoose.QueryFilter<T>, K extends keyof Q>(query: Q, operation: string, field: K, queryValue: string): {
|
|
59
34
|
field: K;
|
|
60
35
|
value: Q[K];
|
|
61
36
|
};
|
|
62
|
-
export declare function toMongooseOperation<T extends object, Q extends mongoose.
|
|
37
|
+
export declare function toMongooseOperation<T extends object, Q extends mongoose.QueryFilter<T>, K extends keyof Q>(query: Q, operation: string, field: K, queryValue: string): {
|
|
63
38
|
field: K;
|
|
64
39
|
value: Q[K];
|
|
65
40
|
};
|
package/lib/utils/types.d.ts
CHANGED
|
@@ -1,31 +1,6 @@
|
|
|
1
|
-
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
-
/// <reference types="mongoose/types/callback" />
|
|
3
|
-
/// <reference types="mongoose/types/collection" />
|
|
4
|
-
/// <reference types="mongoose/types/connection" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor" />
|
|
6
|
-
/// <reference types="mongoose/types/document" />
|
|
7
|
-
/// <reference types="mongoose/types/error" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes" />
|
|
12
|
-
/// <reference types="mongoose/types/models" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
-
/// <reference types="mongoose/types/populate" />
|
|
16
|
-
/// <reference types="mongoose/types/query" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes" />
|
|
19
|
-
/// <reference types="mongoose/types/session" />
|
|
20
|
-
/// <reference types="mongoose/types/types" />
|
|
21
|
-
/// <reference types="mongoose/types/utility" />
|
|
22
|
-
/// <reference types="mongoose/types/validation" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
-
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
1
|
import mongoose from 'mongoose';
|
|
27
2
|
export type MongooseSortSelectValue<T extends object, K extends keyof T = keyof T> = K extends string ? `-${K}` | K : keyof T;
|
|
28
|
-
export type QueryType<T extends object> = mongoose.
|
|
3
|
+
export type QueryType<T extends object> = mongoose.QueryFilter<T> & Record<string, any>;
|
|
29
4
|
export type IQueryAddToModelQuery<R extends object, T extends object> = {
|
|
30
5
|
[key in keyof R]?: QueryType<T>;
|
|
31
6
|
};
|
package/package.json
CHANGED
|
@@ -1,35 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ewb/mongoose-query",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Parse queries and params to mongoose search. Strips dangerous keys and set corrects options with help of query values.",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
8
|
-
"scripts": {
|
|
9
|
-
"dev": "yarn build:watch",
|
|
10
|
-
"build": "tsc",
|
|
11
|
-
"build:watch": "tsc --watch --incremental",
|
|
12
|
-
"lint": "tslint --project \"./tsconfig.json\"",
|
|
13
|
-
"test": "vitest run",
|
|
14
|
-
"test:ui": "vitest --ui",
|
|
15
|
-
"postversion": "git push && git push --tags",
|
|
16
|
-
"prepublish": "yarn test && yarn build",
|
|
17
|
-
"preversion": "yarn build"
|
|
18
|
-
},
|
|
19
8
|
"dependencies": {
|
|
20
9
|
"express-mongo-sanitize": "^2.2.0",
|
|
21
|
-
"http-status": "^1.
|
|
22
|
-
"jsondiffpatch": "^0.
|
|
10
|
+
"http-status": "^2.1.0",
|
|
11
|
+
"jsondiffpatch": "^0.7.7"
|
|
23
12
|
},
|
|
24
13
|
"devDependencies": {
|
|
25
|
-
"@
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"typescript": "^5.4.5",
|
|
32
|
-
"vitest": "^0.31.1"
|
|
14
|
+
"@types/node": "^24.19.2",
|
|
15
|
+
"@vitest/ui": "^5.1.0",
|
|
16
|
+
"mongoose": "^9.12.0",
|
|
17
|
+
"prettier": "^3.9.10",
|
|
18
|
+
"typescript": "^7.0.2",
|
|
19
|
+
"vitest": "^5.1.0"
|
|
33
20
|
},
|
|
34
21
|
"peerDependencies": {
|
|
35
22
|
"mongoose": "*"
|
|
@@ -52,11 +39,16 @@
|
|
|
52
39
|
"files": [
|
|
53
40
|
"lib"
|
|
54
41
|
],
|
|
55
|
-
"husky": {
|
|
56
|
-
"hooks": {
|
|
57
|
-
"pre-commit": "pretty-quick --staged"
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
42
|
"resolutions": {},
|
|
61
|
-
"
|
|
62
|
-
|
|
43
|
+
"scripts": {
|
|
44
|
+
"dev": "pnpm build:watch",
|
|
45
|
+
"build": "tsc",
|
|
46
|
+
"build:watch": "tsc --watch --incremental",
|
|
47
|
+
"lint": "tslint --project \"./tsconfig.json\"",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"test:ui": "vitest --ui",
|
|
50
|
+
"postversion": "git push && git push --tags",
|
|
51
|
+
"prepublish": "pnpm test && pnpm build",
|
|
52
|
+
"preversion": "pnpm build"
|
|
53
|
+
}
|
|
54
|
+
}
|