@ewb/mongoose-query-express 1.0.3 → 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.
@@ -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
- constructor(handler: QueryModel<T>, express: QueryExpress<R, T>, reqKey: keyof R, param: K);
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>;
@@ -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, reqKey, param) {
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.reqKey] = (await this.handler.subCreate(this.param, this.express.query(req), req.body));
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.param, 'onPost');
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.param, this.express.query(req), this.express.getRootQuery(req)));
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.reqKey] = (await this.handler.subFindOne(this.param, this.express.query(req), this.getQuery(req)));
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.param, 'onFindOne');
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.reqKey] = (await this.handler.subFindOneAndUpdate(this.param, this.express.query(req), this.getQuery(req), req.body));
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.param, 'onPatch');
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.reqKey] = (await this.handler.subDelete(this.param, this.express.query(req), this.getQuery(req)));
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.reqKey);
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.reqKey]) {
101
- throw new QueryError(httpStatus.NOT_FOUND, String(this.param), {
102
- detail: 'Param not found',
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.reqKey];
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",
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",