@ewb/mongoose-query-express 1.0.2 → 1.0.4
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/QueryExpress.d.ts +1 -0
- package/lib/QueryExpress.js +16 -8
- package/lib/QueryExpressSub.d.ts +2 -2
- package/lib/QueryExpressSub.js +17 -17
- package/package.json +2 -2
package/lib/QueryExpress.d.ts
CHANGED
package/lib/QueryExpress.js
CHANGED
|
@@ -27,7 +27,7 @@ export class QueryExpress {
|
|
|
27
27
|
}
|
|
28
28
|
async post(req, res, next) {
|
|
29
29
|
try {
|
|
30
|
-
if (
|
|
30
|
+
if (this.skipHandlerAction(req, 'post')) {
|
|
31
31
|
return next();
|
|
32
32
|
}
|
|
33
33
|
this.init(req);
|
|
@@ -47,7 +47,8 @@ export class QueryExpress {
|
|
|
47
47
|
}
|
|
48
48
|
async findOne(req, _res, next) {
|
|
49
49
|
try {
|
|
50
|
-
if (
|
|
50
|
+
if (this.skipHandlerAction(req, 'findOne')) {
|
|
51
|
+
req.ewb.queryOptions.skipHandlerAction = undefined;
|
|
51
52
|
return next();
|
|
52
53
|
}
|
|
53
54
|
this.init(req);
|
|
@@ -65,7 +66,7 @@ export class QueryExpress {
|
|
|
65
66
|
}
|
|
66
67
|
async search(req, res, next) {
|
|
67
68
|
try {
|
|
68
|
-
if (
|
|
69
|
+
if (this.skipHandlerAction(req, 'search')) {
|
|
69
70
|
return next();
|
|
70
71
|
}
|
|
71
72
|
const data = await this._search(req, res);
|
|
@@ -80,7 +81,7 @@ export class QueryExpress {
|
|
|
80
81
|
}
|
|
81
82
|
async searchNext(req, res, next) {
|
|
82
83
|
try {
|
|
83
|
-
if (
|
|
84
|
+
if (this.skipHandlerAction(req, 'search')) {
|
|
84
85
|
return next();
|
|
85
86
|
}
|
|
86
87
|
req.ewb.querySearchJson = await this._search(req, res);
|
|
@@ -100,7 +101,7 @@ export class QueryExpress {
|
|
|
100
101
|
}
|
|
101
102
|
async export(req, res, next) {
|
|
102
103
|
try {
|
|
103
|
-
if (
|
|
104
|
+
if (this.skipHandlerAction(req, 'export')) {
|
|
104
105
|
return next();
|
|
105
106
|
}
|
|
106
107
|
this.init(req);
|
|
@@ -117,7 +118,7 @@ export class QueryExpress {
|
|
|
117
118
|
}
|
|
118
119
|
async patch(req, _res, next) {
|
|
119
120
|
try {
|
|
120
|
-
if (
|
|
121
|
+
if (this.skipHandlerAction(req, 'patch')) {
|
|
121
122
|
return next();
|
|
122
123
|
}
|
|
123
124
|
this.validateReq(req);
|
|
@@ -134,7 +135,7 @@ export class QueryExpress {
|
|
|
134
135
|
}
|
|
135
136
|
async archive(req, _res, next) {
|
|
136
137
|
try {
|
|
137
|
-
if (
|
|
138
|
+
if (this.skipHandlerAction(req, 'archive')) {
|
|
138
139
|
return next();
|
|
139
140
|
}
|
|
140
141
|
this.validateReq(req);
|
|
@@ -149,7 +150,7 @@ export class QueryExpress {
|
|
|
149
150
|
}
|
|
150
151
|
async delete(req, _res, next) {
|
|
151
152
|
try {
|
|
152
|
-
if (
|
|
153
|
+
if (this.skipHandlerAction(req, 'delete')) {
|
|
153
154
|
return next();
|
|
154
155
|
}
|
|
155
156
|
this.validateReq(req);
|
|
@@ -377,4 +378,11 @@ export class QueryExpress {
|
|
|
377
378
|
}
|
|
378
379
|
return req.body.ids.map((x) => new mongoose.Types.ObjectId(x));
|
|
379
380
|
}
|
|
381
|
+
skipHandlerAction(req, action) {
|
|
382
|
+
if (req.ewb.queryOptions.skipHandlerAction === action) {
|
|
383
|
+
req.ewb.queryOptions.skipHandlerAction = undefined;
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
380
388
|
}
|
package/lib/QueryExpressSub.d.ts
CHANGED
|
@@ -5,9 +5,9 @@ import { QueryExpress } from './QueryExpress.js';
|
|
|
5
5
|
export declare class QueryExpressSub<R extends Request, T extends mongoose.Document, K extends keyof T> {
|
|
6
6
|
private handler;
|
|
7
7
|
private express;
|
|
8
|
-
private reqKey;
|
|
9
8
|
private param;
|
|
10
|
-
|
|
9
|
+
private subKey;
|
|
10
|
+
constructor(handler: QueryModel<T>, express: QueryExpress<R, T>, param: keyof R, subKey: K);
|
|
11
11
|
post(req: R, _res: Response, next: NextFunction): Promise<void>;
|
|
12
12
|
search(req: R, res: Response, next: NextFunction): Promise<Response<any, Record<string, any>>>;
|
|
13
13
|
findOne(req: R, _res: Response, next: NextFunction): Promise<void>;
|
package/lib/QueryExpressSub.js
CHANGED
|
@@ -2,11 +2,11 @@ import { QueryError } from '@ewb/mongoose-query';
|
|
|
2
2
|
import httpStatus from 'http-status';
|
|
3
3
|
import mongoose from 'mongoose';
|
|
4
4
|
export class QueryExpressSub {
|
|
5
|
-
constructor(handler, express,
|
|
5
|
+
constructor(handler, express, param, subKey) {
|
|
6
6
|
this.handler = handler;
|
|
7
7
|
this.express = express;
|
|
8
|
-
this.reqKey = reqKey;
|
|
9
8
|
this.param = param;
|
|
9
|
+
this.subKey = subKey;
|
|
10
10
|
this.post = this.post.bind(this);
|
|
11
11
|
this.search = this.search.bind(this);
|
|
12
12
|
this.findOne = this.findOne.bind(this);
|
|
@@ -17,11 +17,11 @@ export class QueryExpressSub {
|
|
|
17
17
|
async post(req, _res, next) {
|
|
18
18
|
try {
|
|
19
19
|
this.express.addRootRequest(req);
|
|
20
|
-
req[this.
|
|
20
|
+
req[this.param] = (await this.handler.subCreate(this.subKey, this.express.query(req), req.body));
|
|
21
21
|
this.validateReq(req);
|
|
22
22
|
this.express.postHistory(req, true).then();
|
|
23
23
|
this.express.removeRootRequest(req, this.express.options.slugKey);
|
|
24
|
-
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.
|
|
24
|
+
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.subKey, 'onPost');
|
|
25
25
|
next();
|
|
26
26
|
}
|
|
27
27
|
catch (e) {
|
|
@@ -31,7 +31,7 @@ export class QueryExpressSub {
|
|
|
31
31
|
async search(req, res, next) {
|
|
32
32
|
try {
|
|
33
33
|
this.express.query(req).removeQuery('as');
|
|
34
|
-
const results = (await this.handler.subSearch(this.
|
|
34
|
+
const results = (await this.handler.subSearch(this.subKey, this.express.query(req), this.express.getRootQuery(req)));
|
|
35
35
|
res.setHeader('Link', this.express.pageLinkHeader(req, results));
|
|
36
36
|
res.setHeader('X-Total-Count', results.count);
|
|
37
37
|
if (req.query.as === 'array' || this.express.query(req).page > 0) {
|
|
@@ -46,10 +46,10 @@ export class QueryExpressSub {
|
|
|
46
46
|
async findOne(req, _res, next) {
|
|
47
47
|
try {
|
|
48
48
|
this.express.addRootRequest(req);
|
|
49
|
-
req[this.
|
|
49
|
+
req[this.param] = (await this.handler.subFindOne(this.subKey, this.express.query(req), this.getQuery(req)));
|
|
50
50
|
this.validateReq(req);
|
|
51
51
|
this.express.removeRootRequest(req, this.express.options.slugKey);
|
|
52
|
-
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.
|
|
52
|
+
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.subKey, 'onFindOne');
|
|
53
53
|
next();
|
|
54
54
|
}
|
|
55
55
|
catch (e) {
|
|
@@ -59,11 +59,11 @@ export class QueryExpressSub {
|
|
|
59
59
|
async patch(req, _res, next) {
|
|
60
60
|
try {
|
|
61
61
|
this.express.addRootRequest(req);
|
|
62
|
-
req[this.
|
|
62
|
+
req[this.param] = (await this.handler.subFindOneAndUpdate(this.subKey, this.express.query(req), this.getQuery(req), req.body));
|
|
63
63
|
this.validateReq(req);
|
|
64
64
|
this.express.postHistory(req, true).then();
|
|
65
65
|
this.express.removeRootRequest(req, this.express.options.slugKey);
|
|
66
|
-
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.
|
|
66
|
+
await this.handler.subPopulate(this.express.query(req), this.getSubDoc(req), this.subKey, 'onPatch');
|
|
67
67
|
next();
|
|
68
68
|
}
|
|
69
69
|
catch (e) {
|
|
@@ -73,7 +73,7 @@ export class QueryExpressSub {
|
|
|
73
73
|
async delete(req, _res, next) {
|
|
74
74
|
try {
|
|
75
75
|
this.express.addRootRequest(req);
|
|
76
|
-
req[this.
|
|
76
|
+
req[this.param] = (await this.handler.subDelete(this.subKey, this.express.query(req), this.getQuery(req)));
|
|
77
77
|
this.express.postHistory(req, true).then();
|
|
78
78
|
this.validateReq(req);
|
|
79
79
|
this.express.removeRootRequest(req, this.express.options.slugKey);
|
|
@@ -85,25 +85,25 @@ export class QueryExpressSub {
|
|
|
85
85
|
}
|
|
86
86
|
async json(req, res, next) {
|
|
87
87
|
try {
|
|
88
|
-
this.validateReq(req);
|
|
89
|
-
res.json(req[this.reqKey]);
|
|
88
|
+
res.json(this.validateReq(req));
|
|
90
89
|
}
|
|
91
90
|
catch (e) {
|
|
92
91
|
next(QueryError.catch(e));
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
getQuery(req) {
|
|
96
|
-
const param = this.express.getParam(req, this.
|
|
95
|
+
const param = this.express.getParam(req, this.param);
|
|
97
96
|
return { _id: new mongoose.Types.ObjectId(param) };
|
|
98
97
|
}
|
|
99
98
|
validateReq(req) {
|
|
100
|
-
if (!req[this.
|
|
101
|
-
throw new QueryError(httpStatus.NOT_FOUND,
|
|
102
|
-
detail:
|
|
99
|
+
if (!req[this.param]) {
|
|
100
|
+
throw new QueryError(httpStatus.NOT_FOUND, 'Document not found', {
|
|
101
|
+
detail: `Param ${String(this.param)} not found`,
|
|
103
102
|
});
|
|
104
103
|
}
|
|
104
|
+
return req[this.param];
|
|
105
105
|
}
|
|
106
106
|
getSubDoc(req) {
|
|
107
|
-
return req[this.
|
|
107
|
+
return req[this.param];
|
|
108
108
|
}
|
|
109
109
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ewb/mongoose-query-express",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Routes and controllers for express to handle mongoose queries",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"preversion": "yarn build"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@ewb/mongoose-query": "
|
|
20
|
+
"@ewb/mongoose-query": "*",
|
|
21
21
|
"http-status": "^1.6.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|