@ewb/mongoose-query 1.0.0 → 1.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.
- package/README.md +24 -35
- package/lib/Query.d.ts +60 -77
- package/lib/Query.js +227 -307
- package/lib/QueryError.d.ts +16 -50
- package/lib/QueryError.js +52 -62
- package/lib/QueryHandler.d.ts +47 -59
- package/lib/QueryHandler.js +250 -297
- package/lib/QueryHistory.d.ts +75 -76
- package/lib/QueryHistory.js +137 -154
- package/lib/QueryModel.d.ts +65 -80
- package/lib/QueryModel.js +214 -201
- package/lib/index.d.ts +9 -10
- package/lib/index.js +6 -32
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/utils/Query.utils.d.ts +49 -0
- package/lib/utils/Query.utils.js +143 -0
- package/lib/{constants.d.ts → utils/constants.d.ts} +2 -2
- package/lib/utils/constants.js +2 -0
- package/lib/utils/types.d.ts +11 -0
- package/lib/utils/types.js +1 -0
- package/lib/utils/utils.d.ts +8 -0
- package/lib/{utils.js → utils/utils.js} +38 -44
- package/package.json +4 -4
- package/lib/ExpressHandler.d.ts +0 -30
- package/lib/ExpressHandler.js +0 -215
- package/lib/ExpressHandler.js.map +0 -1
- package/lib/ModelQuery.d.ts +0 -24
- package/lib/ModelQuery.js +0 -80
- package/lib/ModelQuery.js.map +0 -1
- package/lib/Query.js.map +0 -1
- package/lib/QueryError.js.map +0 -1
- package/lib/QueryExpressHandler.d.ts +0 -50
- package/lib/QueryExpressHandler.js +0 -383
- package/lib/QueryExpressHandler.js.map +0 -1
- package/lib/QueryExpressSubHandler.d.ts +0 -20
- package/lib/QueryExpressSubHandler.js +0 -137
- package/lib/QueryExpressSubHandler.js.map +0 -1
- package/lib/QueryHandler.js.map +0 -1
- package/lib/QueryHistory.js.map +0 -1
- package/lib/QueryModel.js.map +0 -1
- package/lib/constants.js +0 -6
- package/lib/constants.js.map +0 -1
- package/lib/declarations.d.ts +0 -16
- package/lib/declarations.js +0 -3
- package/lib/declarations.js.map +0 -1
- package/lib/error/ExpressError.d.ts +0 -44
- package/lib/error/ExpressError.js +0 -39
- package/lib/error/ExpressError.js.map +0 -1
- package/lib/error/QueryError.d.ts +0 -47
- package/lib/error/QueryError.js +0 -50
- package/lib/error/QueryError.js.map +0 -1
- package/lib/error/QueryExpressError.d.ts +0 -46
- package/lib/error/QueryExpressError.js +0 -39
- package/lib/error/QueryExpressError.js.map +0 -1
- package/lib/error/V1Error.d.ts +0 -12
- package/lib/error/V1Error.js +0 -27
- package/lib/error/V1Error.js.map +0 -1
- package/lib/error/error.handler.d.ts +0 -3
- package/lib/error/error.handler.js +0 -30
- package/lib/error/error.handler.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/statics.d.ts +0 -17
- package/lib/statics.js +0 -35
- package/lib/statics.js.map +0 -1
- package/lib/utils.d.ts +0 -3
- package/lib/utils.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
### Info
|
|
4
4
|
Library to create middlewares for server to parse queries from users to valid mongoose queries.
|
|
5
|
+
This is just a base library to create your own query middleware. It does not provide any routes or controllers.\
|
|
6
|
+
Have another [mongoose-query-express](https://github.com/EmilsWebbod/mongoose-query-express) for that.
|
|
5
7
|
|
|
6
8
|
```
|
|
9
|
+
mongooseQuery in req is not set. You can use your own key.
|
|
7
10
|
function queryMiddleware(req, next) {
|
|
8
|
-
req.mongooseQuery = new Query(req.
|
|
11
|
+
req.mongooseQuery = new Query(req.query);
|
|
9
12
|
next();
|
|
10
13
|
}
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
// This is used to set some logic for query to that model. Model for query is set in the controllers.
|
|
16
|
+
const modelQuery = new QueryModel<Doc>({
|
|
13
17
|
limit: 10,
|
|
14
18
|
query: ['name', 'slug'], // Valid queries for model. Filters all other fields
|
|
15
19
|
select: ['_id', 'name', 'slug'], // Valid select fields for search,
|
|
@@ -20,51 +24,36 @@ const modelQuery = new ModelQuery<Doc>({
|
|
|
20
24
|
}]
|
|
21
25
|
});
|
|
22
26
|
|
|
23
|
-
function modelStaticSearch<T>(this: Model<T>, query: Query<T>) {
|
|
24
|
-
return modelQuerySearchPaginate(modelQuery, query).bind(this);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const model = mongoose.model('Name', Schema);
|
|
28
|
-
model.statics.search(modelStaticSearch);
|
|
29
|
-
|
|
30
27
|
function someController(req, res) {
|
|
31
28
|
// add organization ID to root query so query will always search inside organization
|
|
32
|
-
req.mongooseQuery.
|
|
33
|
-
res.json(await Model.search(req.mongooseQuery));
|
|
29
|
+
req.mongooseQuery.addToRoot({ organization: req.organization._id });
|
|
34
30
|
}
|
|
35
31
|
|
|
36
32
|
```
|
|
37
33
|
|
|
38
34
|
### Queries
|
|
39
35
|
|
|
40
|
-
#### Text
|
|
41
|
-
``?text=some%20text``
|
|
42
|
-
|
|
43
|
-
#### Sort
|
|
44
|
-
``?sort=name,-_order``
|
|
45
|
-
|
|
46
|
-
#### Populate
|
|
47
|
-
``?populate=organization:_id,name;user:_id,name``
|
|
48
|
-
|
|
49
|
-
#### Select
|
|
50
|
-
``?select=_id,name``
|
|
51
|
-
|
|
52
|
-
#### Mongoose Ids
|
|
53
|
-
``?_id=60ec0aa707bda11b64ecbf6b,60ec0aa707bda11b64ecbf6c``
|
|
54
|
-
|
|
55
36
|
#### Skip
|
|
56
37
|
``?skip=10``
|
|
57
38
|
|
|
58
39
|
#### Limit
|
|
59
40
|
``?limit=20``
|
|
60
41
|
|
|
61
|
-
####
|
|
42
|
+
#### Select (Accepts space and comma)
|
|
43
|
+
``?select=_id,name email``
|
|
62
44
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
45
|
+
#### Sort (Accepts space and comma)
|
|
46
|
+
``?sort=name,-_order``
|
|
47
|
+
|
|
48
|
+
#### Populate
|
|
49
|
+
``?$populate=organization:_id,name;user:_id,name``
|
|
50
|
+
Populates organization with _id and name and user with _id and name
|
|
51
|
+
|
|
52
|
+
#### Text
|
|
53
|
+
``?$text=some%20text``
|
|
54
|
+
|
|
55
|
+
#### Mongoose $in
|
|
56
|
+
``?$in__id=60ec0aa707bda11b64ecbf6b,60ec0aa707bda11b64ecbf6c``
|
|
57
|
+
|
|
58
|
+
#### Mongoose $gte $gt $lte $lt
|
|
59
|
+
``?$gte_createdAt=01.02.2023&$lt_createdAt=01.02.2023``
|
package/lib/Query.d.ts
CHANGED
|
@@ -1,77 +1,60 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
addOr(query: FilterQuery<T>): void;
|
|
62
|
-
defaultSelect(select: (keyof LeanDocument<T>)[]): void;
|
|
63
|
-
addPopulate(populate: PopulateOptions | PopulateOptions[]): void;
|
|
64
|
-
addParamToRoot(key: keyof P): void;
|
|
65
|
-
addRoot(query: FilterQuery<T>): FilterQuery<T>;
|
|
66
|
-
addRootNext(query: FilterQuery<T>): FilterQuery<T>;
|
|
67
|
-
removeQuery(key: keyof FilterQuery<T>): void;
|
|
68
|
-
deleteKeysFromRoot(keys: (keyof FilterQuery<T>)[]): void;
|
|
69
|
-
addToModelQuery<R extends any, K extends keyof R = keyof R>(key: K | K[], query: FilterQuery<R[K]>): void;
|
|
70
|
-
createQuery<R extends Request = Request>({ param, query, noRoot, }?: ICreateQueryOptions<R, T>): FilterQuery<T>;
|
|
71
|
-
}
|
|
72
|
-
interface ICreateQueryOptions<R extends Request, T extends Document> {
|
|
73
|
-
param?: keyof R;
|
|
74
|
-
query?: (keyof LeanDocument<T>)[];
|
|
75
|
-
noRoot?: boolean;
|
|
76
|
-
}
|
|
77
|
-
export {};
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { IQueryPopulate } from './QueryModel';
|
|
3
|
+
import { IMongooseQueryOptions } from './utils/Query.utils';
|
|
4
|
+
import { IQueryOptions, MongooseSortSelectValue, QueryType } from './utils/types';
|
|
5
|
+
export declare class Query<R extends object, T extends object = object, Q extends IMongooseQueryOptions = IMongooseQueryOptions> {
|
|
6
|
+
private _root;
|
|
7
|
+
private _rootNext;
|
|
8
|
+
private _rootPopulate;
|
|
9
|
+
private _or;
|
|
10
|
+
private _model;
|
|
11
|
+
private _addToModelQuery;
|
|
12
|
+
private _export;
|
|
13
|
+
private _projection;
|
|
14
|
+
readonly _query: QueryType<T>;
|
|
15
|
+
readonly page: number | undefined;
|
|
16
|
+
private _skip;
|
|
17
|
+
private _limit;
|
|
18
|
+
readonly sort: MongooseSortSelectValue<T>[];
|
|
19
|
+
private _select;
|
|
20
|
+
readonly hasTextSearch: boolean;
|
|
21
|
+
readonly options: IQueryOptions;
|
|
22
|
+
readonly populate: IQueryPopulate[];
|
|
23
|
+
constructor(queries: Q);
|
|
24
|
+
get skip(): number;
|
|
25
|
+
get limit(): number;
|
|
26
|
+
get root(): QueryType<T> | null;
|
|
27
|
+
get query(): QueryType<T>;
|
|
28
|
+
get model(): keyof R;
|
|
29
|
+
set model(value: keyof R);
|
|
30
|
+
get rootPopulate(): mongoose.PopulateOptions[];
|
|
31
|
+
get select(): MongooseSortSelectValue<T>[];
|
|
32
|
+
get or(): QueryType<T>[];
|
|
33
|
+
get export(): boolean;
|
|
34
|
+
set export(value: boolean);
|
|
35
|
+
get projection(): object | undefined;
|
|
36
|
+
set projection(value: object | undefined);
|
|
37
|
+
setSkipAndLimit(skip: number | undefined, limit: number | undefined): void;
|
|
38
|
+
setQuery<K extends keyof QueryType<T>>(key: K, value: QueryType<T>[K]): void;
|
|
39
|
+
addOr(query: QueryType<T>): void;
|
|
40
|
+
defaultSelect(select: MongooseSortSelectValue<T>[]): void;
|
|
41
|
+
addPopulate(populate: mongoose.PopulateOptions | mongoose.PopulateOptions[]): void;
|
|
42
|
+
addRoot(query: typeof this._query): QueryType<T>;
|
|
43
|
+
addRootNext(query: typeof this._query): QueryType<T>;
|
|
44
|
+
removeQuery(key: keyof typeof this._query): void;
|
|
45
|
+
deleteKeysFromQuery(key: keyof typeof this._query): void;
|
|
46
|
+
deleteKeysFromRoot(keys: (keyof QueryType<T>)[]): void;
|
|
47
|
+
addToModelQuery<TT extends {
|
|
48
|
+
[key: string]: object;
|
|
49
|
+
}, K extends keyof TT = keyof TT>(key: K | K[], query: QueryType<TT[K]>): void;
|
|
50
|
+
createQuery({ param, query, noRoot, }?: ICreateQueryOptions<R, T>): QueryType<T>;
|
|
51
|
+
private mongooseTextSearch;
|
|
52
|
+
private mongooseQueryWithOperations;
|
|
53
|
+
private sanitizedQuery;
|
|
54
|
+
}
|
|
55
|
+
interface ICreateQueryOptions<R, T extends object> {
|
|
56
|
+
param?: keyof R;
|
|
57
|
+
query?: (keyof T)[];
|
|
58
|
+
noRoot?: boolean;
|
|
59
|
+
}
|
|
60
|
+
export {};
|