express-restify-mongoose 8.0.0 β†’ 9.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.
Files changed (36) hide show
  1. package/README.md +8 -6
  2. package/dist/buildQuery.js +18 -1
  3. package/dist/detective.js +11 -1
  4. package/dist/errorHandler.js +13 -3
  5. package/dist/express-restify-mongoose.js +32 -22
  6. package/dist/getQuerySchema.js +35 -25
  7. package/dist/middleware/access.js +13 -3
  8. package/dist/middleware/ensureContentType.js +13 -3
  9. package/dist/middleware/filterAndFindById.js +15 -5
  10. package/dist/middleware/onError.js +13 -3
  11. package/dist/middleware/outputFn.js +11 -1
  12. package/dist/middleware/prepareOutput.js +13 -3
  13. package/dist/middleware/prepareQuery.js +15 -5
  14. package/dist/moredots.js +18 -3
  15. package/dist/operations.js +38 -19
  16. package/dist/resource_filter.js +36 -7
  17. package/dist/types.js +4 -1
  18. package/dist/weedout.js +11 -1
  19. package/package.json +14 -20
  20. package/dist/cjs/buildQuery.js +0 -52
  21. package/dist/cjs/detective.js +0 -39
  22. package/dist/cjs/errorHandler.js +0 -20
  23. package/dist/cjs/express-restify-mongoose.js +0 -104
  24. package/dist/cjs/getQuerySchema.js +0 -161
  25. package/dist/cjs/middleware/access.js +0 -32
  26. package/dist/cjs/middleware/ensureContentType.js +0 -23
  27. package/dist/cjs/middleware/filterAndFindById.js +0 -32
  28. package/dist/cjs/middleware/onError.js +0 -29
  29. package/dist/cjs/middleware/outputFn.js +0 -26
  30. package/dist/cjs/middleware/prepareOutput.js +0 -89
  31. package/dist/cjs/middleware/prepareQuery.js +0 -25
  32. package/dist/cjs/moredots.js +0 -24
  33. package/dist/cjs/operations.js +0 -259
  34. package/dist/cjs/resource_filter.js +0 -113
  35. package/dist/cjs/types.js +0 -4
  36. package/dist/cjs/weedout.js +0 -27
package/README.md CHANGED
@@ -17,12 +17,14 @@ npm install express-restify-mongoose --save
17
17
 
18
18
  ## Compatibility
19
19
 
20
- | This library | Mongoose |
21
- | ------------ | -------- |
22
- | >= 7.0.0 | >= 6.x |
23
- | >= 6.0.0 | >= 5.8.0 |
24
- | >= 1.0.0 | 4.x |
25
- | 0.7.5 | 3.x |
20
+ | This library | Mongoose | MongoDB | NodeJS
21
+ | ------------ | ----------- | ----------- | --------
22
+ | >= 9.0.0 | 6.x - 8.x | 3.6.x - 7.x | >=16
23
+ | >= 8.0.0 | 6.x | 3.6.x - 6.x | >=18
24
+ | >= 7.0.0 | 6.x | 3.6.x - 6.x | >=14
25
+ | >= 6.0.0 | 5.8.0 - 6.x | 3.6.x - 6.x | >=6
26
+ | >= 1.0.0 | 4.x | 2.4 - 3.4 | >=0.10
27
+ | 0.7.5 | 3.x | 2.4 - 3.0 | *
26
28
 
27
29
  ## Contributing
28
30
 
@@ -1,4 +1,14 @@
1
- export function getBuildQuery(options) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getBuildQuery", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getBuildQuery;
9
+ }
10
+ });
11
+ function getBuildQuery(options) {
2
12
  return function buildQuery(query, queryOptions) {
3
13
  const promise = new Promise((resolve)=>{
4
14
  if (!queryOptions) {
@@ -18,6 +28,13 @@ export function getBuildQuery(options) {
18
28
  query.limit(queryOptions.limit);
19
29
  }
20
30
  if (queryOptions.sort) {
31
+ // Necessary to support Mongoose 8
32
+ if (typeof queryOptions.sort === 'object') {
33
+ Object.keys(queryOptions.sort).forEach((key)=>{
34
+ // @ts-expect-error this is fine 🐢πŸ”₯
35
+ queryOptions.sort[key] = Number(queryOptions.sort[key]);
36
+ });
37
+ }
21
38
  // @ts-expect-error this is fine 🐢πŸ”₯
22
39
  query.sort(queryOptions.sort);
23
40
  }
package/dist/detective.js CHANGED
@@ -1,4 +1,14 @@
1
- export function detective(model, path) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "detective", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return detective;
9
+ }
10
+ });
11
+ function detective(model, path) {
2
12
  const keys = path.split(".");
3
13
  let schema = model.schema;
4
14
  let schemaPath = "";
@@ -1,7 +1,17 @@
1
- import { STATUS_CODES } from "http";
2
- export function getErrorHandler(options) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getErrorHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getErrorHandler;
9
+ }
10
+ });
11
+ const _http = require("http");
12
+ function getErrorHandler(options) {
3
13
  const fn = function errorHandler(err, req, res, next) {
4
- if (err.message === STATUS_CODES[404] || req.params?.id && err.path === options.idProperty && err.name === "CastError") {
14
+ if (err.message === _http.STATUS_CODES[404] || req.params?.id && err.path === options.idProperty && err.name === "CastError") {
5
15
  req.erm.statusCode = 404;
6
16
  } else {
7
17
  req.erm.statusCode = req.erm.statusCode && req.erm.statusCode >= 400 ? req.erm.statusCode : 400;
@@ -1,13 +1,23 @@
1
- import { deprecate } from "util";
2
- import { getAccessHandler } from "./middleware/access.js";
3
- import { getEnsureContentTypeHandler } from "./middleware/ensureContentType.js";
4
- import { getFilterAndFindByIdHandler } from "./middleware/filterAndFindById.js";
5
- import { getOnErrorHandler } from "./middleware/onError.js";
6
- import { getOutputFnHandler } from "./middleware/outputFn.js";
7
- import { getPrepareOutputHandler } from "./middleware/prepareOutput.js";
8
- import { getPrepareQueryHandler } from "./middleware/prepareQuery.js";
9
- import { operations } from "./operations.js";
10
- import { Filter } from "./resource_filter.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "serve", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return serve;
9
+ }
10
+ });
11
+ const _util = require("util");
12
+ const _access = require("./middleware/access.js");
13
+ const _ensureContentType = require("./middleware/ensureContentType.js");
14
+ const _filterAndFindById = require("./middleware/filterAndFindById.js");
15
+ const _onError = require("./middleware/onError.js");
16
+ const _outputFn = require("./middleware/outputFn.js");
17
+ const _prepareOutput = require("./middleware/prepareOutput.js");
18
+ const _prepareQuery = require("./middleware/prepareQuery.js");
19
+ const _operations = require("./operations.js");
20
+ const _resource_filter = require("./resource_filter.js");
11
21
  const defaultOptions = {
12
22
  prefix: "/api",
13
23
  version: "/v1",
@@ -29,14 +39,14 @@ const defaultOptions = {
29
39
  preUpdate: [],
30
40
  preDelete: []
31
41
  };
32
- const filter = new Filter();
33
- export function serve(app, model, options = {}) {
42
+ const filter = new _resource_filter.Filter();
43
+ function serve(app, model, options = {}) {
34
44
  const serveOptions = {
35
45
  ...defaultOptions,
36
46
  name: typeof options.name === "string" ? options.name : model.modelName,
37
47
  contextFilter: (model, req, done)=>done(model),
38
- outputFn: getOutputFnHandler(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
39
- onError: getOnErrorHandler(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
48
+ outputFn: (0, _outputFn.getOutputFnHandler)(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
49
+ onError: (0, _onError.getOnErrorHandler)(typeof options.restify === "boolean" ? !options.restify : !defaultOptions.restify),
40
50
  ...options
41
51
  };
42
52
  model.schema.eachPath((name, path)=>{
@@ -57,7 +67,7 @@ export function serve(app, model, options = {}) {
57
67
  protected: serveOptions.protected
58
68
  }
59
69
  });
60
- const ops = operations(model, serveOptions, filter);
70
+ const ops = (0, _operations.operations)(model, serveOptions, filter);
61
71
  let uriItem = `${serveOptions.prefix}${serveOptions.version}/${serveOptions.name}`;
62
72
  if (uriItem.indexOf("/:id") === -1) {
63
73
  uriItem += "/:id";
@@ -73,22 +83,22 @@ export function serve(app, model, options = {}) {
73
83
  req.erm = {};
74
84
  next();
75
85
  });
76
- const accessMiddleware = serveOptions.access ? getAccessHandler({
86
+ const accessMiddleware = serveOptions.access ? (0, _access.getAccessHandler)({
77
87
  access: serveOptions.access,
78
88
  idProperty: serveOptions.idProperty,
79
89
  onError: serveOptions.onError
80
90
  }) : [];
81
- const ensureContentType = getEnsureContentTypeHandler(serveOptions);
82
- const filterAndFindById = getFilterAndFindByIdHandler(serveOptions, model);
83
- const prepareQuery = getPrepareQueryHandler(serveOptions);
84
- const prepareOutput = getPrepareOutputHandler(serveOptions, model.modelName, filter);
91
+ const ensureContentType = (0, _ensureContentType.getEnsureContentTypeHandler)(serveOptions);
92
+ const filterAndFindById = (0, _filterAndFindById.getFilterAndFindByIdHandler)(serveOptions, model);
93
+ const prepareQuery = (0, _prepareQuery.getPrepareQueryHandler)(serveOptions);
94
+ const prepareOutput = (0, _prepareOutput.getPrepareOutputHandler)(serveOptions, model.modelName, filter);
85
95
  app.get(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
86
96
  app.get(uriCount, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
87
97
  app.get(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
88
98
  app.get(uriShallow, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
89
99
  app.post(uriItems, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
90
- app.post(uriItem, deprecate(prepareQuery, "express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
91
- app.put(uriItem, deprecate(prepareQuery, "express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
100
+ app.post(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
101
+ app.put(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
92
102
  app.patch(uriItem, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
93
103
  app.delete(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
94
104
  app.delete(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
@@ -1,14 +1,24 @@
1
- import { z } from "zod";
2
- const PopulateOptionsSchema = z.object({
3
- path: z.string(),
4
- match: z.record(z.unknown()).optional(),
5
- options: z.record(z.unknown()).optional(),
6
- select: z.string().optional(),
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getQueryOptionsSchema", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getQueryOptionsSchema;
9
+ }
10
+ });
11
+ const _zod = require("zod");
12
+ const PopulateOptionsSchema = _zod.z.object({
13
+ path: _zod.z.string(),
14
+ match: _zod.z.record(_zod.z.unknown()).optional(),
15
+ options: _zod.z.record(_zod.z.unknown()).optional(),
16
+ select: _zod.z.string().optional(),
7
17
  // Configure populate query to not use strict populate to maintain
8
18
  // behavior from Mongoose previous to v6 (unless already configured)
9
- strictPopulate: z.boolean().optional().default(false)
19
+ strictPopulate: _zod.z.boolean().optional().default(false)
10
20
  });
11
- const PopulateSchema = z.preprocess((value)=>{
21
+ const PopulateSchema = _zod.z.preprocess((value)=>{
12
22
  if (typeof value === "string") {
13
23
  if (value.startsWith("{")) {
14
24
  return JSON.parse(`[${value}]`);
@@ -21,11 +31,11 @@ const PopulateSchema = z.preprocess((value)=>{
21
31
  return Array.isArray(value) ? value : [
22
32
  value
23
33
  ];
24
- }, z.union([
25
- z.string(),
26
- z.array(PopulateOptionsSchema)
34
+ }, _zod.z.union([
35
+ _zod.z.string(),
36
+ _zod.z.array(PopulateOptionsSchema)
27
37
  ]));
28
- const SelectSchema = z.preprocess((value)=>{
38
+ const SelectSchema = _zod.z.preprocess((value)=>{
29
39
  const fieldToRecord = (field)=>{
30
40
  if (field.startsWith("-")) {
31
41
  return [
@@ -48,15 +58,15 @@ const SelectSchema = z.preprocess((value)=>{
48
58
  return Object.fromEntries(value.map(fieldToRecord));
49
59
  }
50
60
  return value;
51
- }, z.record(z.number().min(0).max(1)));
52
- const SortSchema = z.preprocess((value)=>{
61
+ }, _zod.z.record(_zod.z.number().min(0).max(1)));
62
+ const SortSchema = _zod.z.preprocess((value)=>{
53
63
  if (typeof value === "string" && value.startsWith("{")) {
54
64
  return JSON.parse(value);
55
65
  }
56
66
  return value;
57
- }, z.union([
58
- z.string(),
59
- z.record(z.enum([
67
+ }, _zod.z.union([
68
+ _zod.z.string(),
69
+ _zod.z.record(_zod.z.enum([
60
70
  "asc",
61
71
  "desc",
62
72
  "ascending",
@@ -64,16 +74,16 @@ const SortSchema = z.preprocess((value)=>{
64
74
  "-1",
65
75
  "1"
66
76
  ])),
67
- z.record(z.number().min(-1).max(1))
77
+ _zod.z.record(_zod.z.number().min(-1).max(1))
68
78
  ]));
69
- const LimitSkipSchema = z.preprocess((value)=>{
79
+ const LimitSkipSchema = _zod.z.preprocess((value)=>{
70
80
  if (typeof value !== "string") {
71
81
  return value;
72
82
  }
73
83
  return Number(value);
74
- }, z.number());
75
- export function getQueryOptionsSchema({ allowRegex }) {
76
- const QuerySchema = z.preprocess((value)=>{
84
+ }, _zod.z.number());
85
+ function getQueryOptionsSchema({ allowRegex }) {
86
+ const QuerySchema = _zod.z.preprocess((value)=>{
77
87
  if (!allowRegex && `${value}`.toLowerCase().includes("$regex")) {
78
88
  throw new Error("regex_not_allowed");
79
89
  }
@@ -81,7 +91,7 @@ export function getQueryOptionsSchema({ allowRegex }) {
81
91
  return value;
82
92
  }
83
93
  return JSON.parse(value);
84
- }, z.record(z.unknown())).transform((value)=>{
94
+ }, _zod.z.record(_zod.z.unknown())).transform((value)=>{
85
95
  return Object.fromEntries(Object.entries(value).map(([key, value])=>{
86
96
  if (Array.isArray(value) && !key.startsWith("$")) {
87
97
  return [
@@ -97,14 +107,14 @@ export function getQueryOptionsSchema({ allowRegex }) {
97
107
  ];
98
108
  }));
99
109
  });
100
- return z.object({
110
+ return _zod.z.object({
101
111
  query: QuerySchema.optional(),
102
112
  populate: PopulateSchema.optional(),
103
113
  select: SelectSchema.optional(),
104
114
  sort: SortSchema.optional(),
105
115
  limit: LimitSkipSchema.optional(),
106
116
  skip: LimitSkipSchema.optional(),
107
- distinct: z.string().optional()
117
+ distinct: _zod.z.string().optional()
108
118
  }).transform((value)=>{
109
119
  if (typeof value.populate === "undefined") {
110
120
  return value;
@@ -1,6 +1,16 @@
1
- import { getErrorHandler } from "../errorHandler.js";
2
- export function getAccessHandler(options) {
3
- const errorHandler = getErrorHandler(options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getAccessHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getAccessHandler;
9
+ }
10
+ });
11
+ const _errorHandler = require("../errorHandler.js");
12
+ function getAccessHandler(options) {
13
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
4
14
  const fn = function access(req, res, next) {
5
15
  const handler = function(access) {
6
16
  if (![
@@ -1,6 +1,16 @@
1
- import { getErrorHandler } from "../errorHandler.js";
2
- export function getEnsureContentTypeHandler(options) {
3
- const errorHandler = getErrorHandler(options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getEnsureContentTypeHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getEnsureContentTypeHandler;
9
+ }
10
+ });
11
+ const _errorHandler = require("../errorHandler.js");
12
+ function getEnsureContentTypeHandler(options) {
13
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
4
14
  const fn = function ensureContentType(req, res, next) {
5
15
  const contentType = req.headers["content-type"];
6
16
  if (!contentType) {
@@ -1,7 +1,17 @@
1
- import { STATUS_CODES } from "http";
2
- import { getErrorHandler } from "../errorHandler.js";
3
- export function getFilterAndFindByIdHandler(options, model) {
4
- const errorHandler = getErrorHandler(options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getFilterAndFindByIdHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getFilterAndFindByIdHandler;
9
+ }
10
+ });
11
+ const _http = require("http");
12
+ const _errorHandler = require("../errorHandler.js");
13
+ function getFilterAndFindByIdHandler(options, model) {
14
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
5
15
  const fn = function filterAndFindById(req, res, next) {
6
16
  const contextModel = model;
7
17
  if (!req.params.id) {
@@ -13,7 +23,7 @@ export function getFilterAndFindByIdHandler(options, model) {
13
23
  [options.idProperty]: req.params.id
14
24
  }).lean(false).read(options.readPreference || "p").exec().then((doc)=>{
15
25
  if (!doc) {
16
- return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
26
+ return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
17
27
  }
18
28
  req.erm.document = doc;
19
29
  next();
@@ -1,7 +1,17 @@
1
- import { serializeError } from "serialize-error";
2
- export function getOnErrorHandler(isExpress) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getOnErrorHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getOnErrorHandler;
9
+ }
10
+ });
11
+ const _serializeerror = require("serialize-error");
12
+ function getOnErrorHandler(isExpress) {
3
13
  const fn = function onError(err, req, res) {
4
- const serializedErr = serializeError(err);
14
+ const serializedErr = (0, _serializeerror.serializeError)(err);
5
15
  delete serializedErr.stack;
6
16
  if (serializedErr.errors) {
7
17
  for(const key in serializedErr.errors){
@@ -1,4 +1,14 @@
1
- export function getOutputFnHandler(isExpress) {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getOutputFnHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getOutputFnHandler;
9
+ }
10
+ });
11
+ function getOutputFnHandler(isExpress) {
2
12
  const fn = function outputFn(req, res) {
3
13
  if (!req.erm.statusCode) {
4
14
  throw new Error("statusCode not set");
@@ -1,9 +1,19 @@
1
- import { getErrorHandler } from "../errorHandler.js";
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getPrepareOutputHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getPrepareOutputHandler;
9
+ }
10
+ });
11
+ const _errorHandler = require("../errorHandler.js");
2
12
  function isDefined(arg) {
3
13
  return typeof arg !== "undefined";
4
14
  }
5
- export function getPrepareOutputHandler(options, modelName, filter) {
6
- const errorHandler = getErrorHandler(options);
15
+ function getPrepareOutputHandler(options, modelName, filter) {
16
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
7
17
  const fn = function prepareOutput(req, res, next) {
8
18
  const postMiddleware = (()=>{
9
19
  switch(req.method.toLowerCase()){
@@ -1,11 +1,21 @@
1
- import { getErrorHandler } from "../errorHandler.js";
2
- import { getQueryOptionsSchema } from "../getQuerySchema.js";
3
- export function getPrepareQueryHandler(options) {
4
- const errorHandler = getErrorHandler(options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "getPrepareQueryHandler", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return getPrepareQueryHandler;
9
+ }
10
+ });
11
+ const _errorHandler = require("../errorHandler.js");
12
+ const _getQuerySchema = require("../getQuerySchema.js");
13
+ function getPrepareQueryHandler(options) {
14
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
5
15
  const fn = function prepareQuery(req, res, next) {
6
16
  req.erm = req.erm || {};
7
17
  try {
8
- req.erm.query = getQueryOptionsSchema({
18
+ req.erm.query = (0, _getQuerySchema.getQueryOptionsSchema)({
9
19
  allowRegex: options.allowRegex
10
20
  }).parse(req.query || {});
11
21
  next();
package/dist/moredots.js CHANGED
@@ -1,7 +1,22 @@
1
- import isPlainObject from "lodash.isplainobject";
2
- export function moredots(src, dst = {}, prefix = "") {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "moredots", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return moredots;
9
+ }
10
+ });
11
+ const _lodashisplainobject = /*#__PURE__*/ _interop_require_default(require("lodash.isplainobject"));
12
+ function _interop_require_default(obj) {
13
+ return obj && obj.__esModule ? obj : {
14
+ default: obj
15
+ };
16
+ }
17
+ function moredots(src, dst = {}, prefix = "") {
3
18
  for (const [key, value] of Object.entries(src)){
4
- if (isPlainObject(value)) {
19
+ if ((0, _lodashisplainobject.default)(value)) {
5
20
  moredots(value, dst, `${prefix}${key}.`);
6
21
  } else {
7
22
  dst[`${prefix}${key}`] = value;
@@ -1,11 +1,26 @@
1
- import { STATUS_CODES } from "http";
2
- import isPlainObject from "lodash.isplainobject";
3
- import { getBuildQuery } from "./buildQuery.js";
4
- import { getErrorHandler } from "./errorHandler.js";
5
- import { moredots } from "./moredots.js";
6
- export function operations(model, options, filter) {
7
- const buildQuery = getBuildQuery(options);
8
- const errorHandler = getErrorHandler(options);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "operations", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return operations;
9
+ }
10
+ });
11
+ const _http = require("http");
12
+ const _lodashisplainobject = /*#__PURE__*/ _interop_require_default(require("lodash.isplainobject"));
13
+ const _buildQuery = require("./buildQuery.js");
14
+ const _errorHandler = require("./errorHandler.js");
15
+ const _moredots = require("./moredots.js");
16
+ function _interop_require_default(obj) {
17
+ return obj && obj.__esModule ? obj : {
18
+ default: obj
19
+ };
20
+ }
21
+ function operations(model, options, filter) {
22
+ const buildQuery = (0, _buildQuery.getBuildQuery)(options);
23
+ const errorHandler = (0, _errorHandler.getErrorHandler)(options);
9
24
  function findById(filteredContext, id) {
10
25
  return filteredContext.findOne().and([
11
26
  {
@@ -69,7 +84,7 @@ export function operations(model, options, filter) {
69
84
  buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
70
85
  findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
71
86
  if (!item) {
72
- return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
87
+ return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
73
88
  }
74
89
  for(const prop in item){
75
90
  item[prop] = typeof item[prop] === "object" && prop !== "_id" ? true : item[prop];
@@ -100,7 +115,7 @@ export function operations(model, options, filter) {
100
115
  buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
101
116
  findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
102
117
  if (!item) {
103
- return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
118
+ return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
104
119
  }
105
120
  req.erm.result = item;
106
121
  req.erm.statusCode = 200;
@@ -113,16 +128,18 @@ export function operations(model, options, filter) {
113
128
  if (options.findOneAndRemove) {
114
129
  options.contextFilter(contextModel, req, (filteredContext)=>{
115
130
  // @ts-expect-error this is fine 🐢πŸ”₯
116
- findById(filteredContext, req.params.id).findOneAndRemove().then((item)=>{
131
+ findById(filteredContext, req.params.id).findOneAndDelete() // switched to findOneAndDelete to add support for Mongoose 7 and 8
132
+ .then((item)=>{
117
133
  if (!item) {
118
- return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
134
+ return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
119
135
  }
120
136
  req.erm.statusCode = 204;
121
137
  next();
122
138
  }).catch((err)=>errorHandler(err, req, res, next));
123
139
  });
124
140
  } else {
125
- req.erm.document?.remove().then(()=>{
141
+ req.erm.document?.deleteOne() // switched to deleteOne to add support for Mongoose 7 and 8
142
+ .then(()=>{
126
143
  req.erm.statusCode = 204;
127
144
  next();
128
145
  }).catch((err)=>errorHandler(err, req, res, next));
@@ -172,7 +189,8 @@ export function operations(model, options, filter) {
172
189
  for (const [key, value] of Object.entries(src)){
173
190
  const path = contextModel.schema.path(key);
174
191
  // @ts-expect-error this is fine 🐢πŸ”₯
175
- if (path && path.caster && path.caster.instance === "ObjectID") {
192
+ // Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
193
+ if (path && path.caster && (path.caster.instance === "ObjectID" || path.caster.instance === "ObjectId")) {
176
194
  if (Array.isArray(value)) {
177
195
  for(let j = 0; j < value.length; ++j){
178
196
  if (typeof value[j] === "object") {
@@ -181,11 +199,12 @@ export function operations(model, options, filter) {
181
199
  dst[key].push(value[j]._id);
182
200
  }
183
201
  }
184
- } else if (isPlainObject(value)) {
202
+ } else if ((0, _lodashisplainobject.default)(value)) {
185
203
  dst[key] = value._id;
186
204
  }
187
- } else if (isPlainObject(value)) {
188
- if (path && path.instance === "ObjectID") {
205
+ } else if ((0, _lodashisplainobject.default)(value)) {
206
+ // Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
207
+ if (path && (path.instance === "ObjectID" || path.instance === "ObjectId")) {
189
208
  dst[key] = value._id;
190
209
  } else {
191
210
  dst[key] = depopulate(value);
@@ -197,7 +216,7 @@ export function operations(model, options, filter) {
197
216
  }
198
217
  return dst;
199
218
  }
200
- const cleanBody = moredots(depopulate(req.body));
219
+ const cleanBody = (0, _moredots.moredots)(depopulate(req.body));
201
220
  if (options.findOneAndUpdate) {
202
221
  options.contextFilter(contextModel, req, (filteredContext)=>{
203
222
  // @ts-expect-error this is fine 🐢πŸ”₯
@@ -212,7 +231,7 @@ export function operations(model, options, filter) {
212
231
  return contextModel.populate(item, req.erm.query?.populate || []);
213
232
  }).then((item)=>{
214
233
  if (!item) {
215
- return errorHandler(new Error(STATUS_CODES[404]), req, res, next);
234
+ return errorHandler(new Error(_http.STATUS_CODES[404]), req, res, next);
216
235
  }
217
236
  req.erm.result = item;
218
237
  req.erm.statusCode = 200;