@kikiutils/mongoose 3.0.3 → 3.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.
@@ -3,6 +3,7 @@
3
3
  const lodashGet = require('lodash/get.js');
4
4
  const lodashSet = require('lodash/set.js');
5
5
  const lodashUnset = require('lodash/unset.js');
6
+ const mongoose = require('mongoose');
6
7
 
7
8
  const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
8
9
 
@@ -10,6 +11,7 @@ const lodashGet__default = /*#__PURE__*/_interopDefaultCompat(lodashGet);
10
11
  const lodashSet__default = /*#__PURE__*/_interopDefaultCompat(lodashSet);
11
12
  const lodashUnset__default = /*#__PURE__*/_interopDefaultCompat(lodashUnset);
12
13
 
14
+ // @ts-expect-error Ignore this error.
13
15
  /**
14
16
  * Mongoose plugin to normalize the JSON output of documents.
15
17
  *
@@ -30,9 +32,9 @@ function mongooseNormalizePlugin(schema) {
30
32
  schema.set('toJSON', {
31
33
  ...toJson,
32
34
  transform(doc, ret, options) {
33
- const copiedRet = { ...ret };
34
- copiedRet.id = copiedRet._id?.toHexString();
35
- delete copiedRet._id;
35
+ // eslint-disable-next-line style/object-curly-newline
36
+ const { __v, _id, ...copiedRet } = ret;
37
+ copiedRet.id = _id instanceof mongoose.Types.ObjectId ? _id.toHexString() : _id;
36
38
  for (const path in schema.paths) {
37
39
  if (schema.paths[path]?.options?.private)
38
40
  lodashUnset__default(copiedRet, path);
@@ -42,7 +44,6 @@ function mongooseNormalizePlugin(schema) {
42
44
  lodashSet__default(copiedRet, path, value.toString());
43
45
  }
44
46
  }
45
- delete copiedRet.__v;
46
47
  if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
47
48
  return toJsonTransform(doc, copiedRet, options);
48
49
  }
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.cjs","sources":["../../src/plugins/normalize.ts"],"sourcesContent":["import lodashGet from 'lodash/get.js';\nimport lodashSet from 'lodash/set.js';\nimport lodashUnset from 'lodash/unset.js';\nimport type {\n Schema,\n Types,\n} from 'mongoose';\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(schema: S) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc: any, ret: any, options: any) {\n const copiedRet = { ...ret };\n copiedRet.id = copiedRet._id?.toHexString();\n delete copiedRet._id;\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) lodashUnset(copiedRet, path);\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value: Types.Decimal128 | undefined = lodashGet(copiedRet, path);\n if (value) lodashSet(copiedRet, path, value.toString());\n }\n }\n\n delete copiedRet.__v;\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc, copiedRet, options);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"names":["lodashUnset","lodashGet","lodashSet"],"mappings":";;;;;;;;;;;;AAQA;;;;;;;;;;;;;AAaG;AACG,SAAU,uBAAuB,CAAmB,MAAS,EAAA;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;AACnC,IAAA,MAAM,eAAe,GAAG,MAAM,EAAE,SAAS;AACzC,IAAA,MAAM,CAAC,GAAG,CACN,QAAQ,EACR;AACI,QAAA,GAAG,MAAM;AACT,QAAA,SAAS,CAAC,GAAQ,EAAE,GAAQ,EAAE,OAAY,EAAA;AACtC,YAAA,MAAM,SAAS,GAAG,EAAE,GAAG,GAAG,EAAE;YAC5B,SAAS,CAAC,EAAE,GAAG,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE;YAC3C,OAAO,SAAS,CAAC,GAAG;AACpB,YAAA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC7B,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO;AAAE,oBAAAA,oBAAW,CAAC,SAAS,EAAE,IAAI,CAAC;gBACtE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,KAAK,YAAY,EAAE;oBAC/C,MAAM,KAAK,GAAiCC,kBAAS,CAAC,SAAS,EAAE,IAAI,CAAC;AACtE,oBAAA,IAAI,KAAK;wBAAEC,kBAAS,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;;;YAI/D,OAAO,SAAS,CAAC,GAAG;AACpB,YAAA,IAAI,eAAe,IAAI,OAAO,eAAe,KAAK,SAAS,EAAE;gBACzD,OAAO,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC;;AAGnD,YAAA,OAAO,SAAS;SACnB;AACJ,KAAA,CACJ;AACL;;;;"}
1
+ {"version":3,"file":"normalize.cjs","sources":["../../src/plugins/normalize.ts"],"sourcesContent":["// @ts-expect-error Ignore this error.\nimport lodashGet from 'lodash/get.js';\n// @ts-expect-error Ignore this error.\nimport lodashSet from 'lodash/set.js';\n// @ts-expect-error Ignore this error.\nimport lodashUnset from 'lodash/unset.js';\nimport { Types } from 'mongoose';\nimport type { Schema } from 'mongoose';\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(schema: S) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc: any, ret: any, options: any) {\n // eslint-disable-next-line style/object-curly-newline\n const { __v, _id, ...copiedRet } = ret;\n copiedRet.id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) lodashUnset(copiedRet, path);\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value: Types.Decimal128 | undefined = lodashGet(copiedRet, path);\n if (value) lodashSet(copiedRet, path, value.toString());\n }\n }\n\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc, copiedRet, options);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"names":["Types","lodashUnset","lodashGet","lodashSet"],"mappings":";;;;;;;;;;;;;AAAA;AASA;;;;;;;;;;;;;AAaG;AACG,SAAU,uBAAuB,CAAmB,MAAS,EAAA;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;AACnC,IAAA,MAAM,eAAe,GAAG,MAAM,EAAE,SAAS;AACzC,IAAA,MAAM,CAAC,GAAG,CACN,QAAQ,EACR;AACI,QAAA,GAAG,MAAM;AACT,QAAA,SAAS,CAAC,GAAQ,EAAE,GAAQ,EAAE,OAAY,EAAA;;YAEtC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,GAAG;AACtC,YAAA,SAAS,CAAC,EAAE,GAAG,GAAG,YAAYA,cAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,GAAG,GAAG;AACtE,YAAA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC7B,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO;AAAE,oBAAAC,oBAAW,CAAC,SAAS,EAAE,IAAI,CAAC;gBACtE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,KAAK,YAAY,EAAE;oBAC/C,MAAM,KAAK,GAAiCC,kBAAS,CAAC,SAAS,EAAE,IAAI,CAAC;AACtE,oBAAA,IAAI,KAAK;wBAAEC,kBAAS,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;;;AAI/D,YAAA,IAAI,eAAe,IAAI,OAAO,eAAe,KAAK,SAAS,EAAE;gBACzD,OAAO,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC;;AAGnD,YAAA,OAAO,SAAS;SACnB;AACJ,KAAA,CACJ;AACL;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/plugins/normalize.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,MAAM,EAET,MAAM,UAAU,CAAC;AAElB;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,QA4BlE"}
1
+ {"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/plugins/normalize.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,MAAM,EAAE,CAAC,QA2BlE"}
@@ -1,7 +1,9 @@
1
1
  import lodashGet from 'lodash/get.js';
2
2
  import lodashSet from 'lodash/set.js';
3
3
  import lodashUnset from 'lodash/unset.js';
4
+ import { Types } from 'mongoose';
4
5
 
6
+ // @ts-expect-error Ignore this error.
5
7
  /**
6
8
  * Mongoose plugin to normalize the JSON output of documents.
7
9
  *
@@ -22,9 +24,9 @@ function mongooseNormalizePlugin(schema) {
22
24
  schema.set('toJSON', {
23
25
  ...toJson,
24
26
  transform(doc, ret, options) {
25
- const copiedRet = { ...ret };
26
- copiedRet.id = copiedRet._id?.toHexString();
27
- delete copiedRet._id;
27
+ // eslint-disable-next-line style/object-curly-newline
28
+ const { __v, _id, ...copiedRet } = ret;
29
+ copiedRet.id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;
28
30
  for (const path in schema.paths) {
29
31
  if (schema.paths[path]?.options?.private)
30
32
  lodashUnset(copiedRet, path);
@@ -34,7 +36,6 @@ function mongooseNormalizePlugin(schema) {
34
36
  lodashSet(copiedRet, path, value.toString());
35
37
  }
36
38
  }
37
- delete copiedRet.__v;
38
39
  if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
39
40
  return toJsonTransform(doc, copiedRet, options);
40
41
  }
@@ -1 +1 @@
1
- {"version":3,"file":"normalize.mjs","sources":["../../src/plugins/normalize.ts"],"sourcesContent":["import lodashGet from 'lodash/get.js';\nimport lodashSet from 'lodash/set.js';\nimport lodashUnset from 'lodash/unset.js';\nimport type {\n Schema,\n Types,\n} from 'mongoose';\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(schema: S) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc: any, ret: any, options: any) {\n const copiedRet = { ...ret };\n copiedRet.id = copiedRet._id?.toHexString();\n delete copiedRet._id;\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) lodashUnset(copiedRet, path);\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value: Types.Decimal128 | undefined = lodashGet(copiedRet, path);\n if (value) lodashSet(copiedRet, path, value.toString());\n }\n }\n\n delete copiedRet.__v;\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc, copiedRet, options);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"names":[],"mappings":";;;;AAQA;;;;;;;;;;;;;AAaG;AACG,SAAU,uBAAuB,CAAmB,MAAS,EAAA;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;AACnC,IAAA,MAAM,eAAe,GAAG,MAAM,EAAE,SAAS;AACzC,IAAA,MAAM,CAAC,GAAG,CACN,QAAQ,EACR;AACI,QAAA,GAAG,MAAM;AACT,QAAA,SAAS,CAAC,GAAQ,EAAE,GAAQ,EAAE,OAAY,EAAA;AACtC,YAAA,MAAM,SAAS,GAAG,EAAE,GAAG,GAAG,EAAE;YAC5B,SAAS,CAAC,EAAE,GAAG,SAAS,CAAC,GAAG,EAAE,WAAW,EAAE;YAC3C,OAAO,SAAS,CAAC,GAAG;AACpB,YAAA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC7B,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO;AAAE,oBAAA,WAAW,CAAC,SAAS,EAAE,IAAI,CAAC;gBACtE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,KAAK,YAAY,EAAE;oBAC/C,MAAM,KAAK,GAAiC,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC;AACtE,oBAAA,IAAI,KAAK;wBAAE,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;;;YAI/D,OAAO,SAAS,CAAC,GAAG;AACpB,YAAA,IAAI,eAAe,IAAI,OAAO,eAAe,KAAK,SAAS,EAAE;gBACzD,OAAO,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC;;AAGnD,YAAA,OAAO,SAAS;SACnB;AACJ,KAAA,CACJ;AACL;;;;"}
1
+ {"version":3,"file":"normalize.mjs","sources":["../../src/plugins/normalize.ts"],"sourcesContent":["// @ts-expect-error Ignore this error.\nimport lodashGet from 'lodash/get.js';\n// @ts-expect-error Ignore this error.\nimport lodashSet from 'lodash/set.js';\n// @ts-expect-error Ignore this error.\nimport lodashUnset from 'lodash/unset.js';\nimport { Types } from 'mongoose';\nimport type { Schema } from 'mongoose';\n\n/**\n * Mongoose plugin to normalize the JSON output of documents.\n *\n * This plugin modifies the `toJSON` method of Mongoose schemas to:\n * - Add an `id` field equal to the string representation of `_id`\n * - Remove the `_id` field\n * - Exclude fields marked as `private` from the JSON output\n * - Convert `Decimal128` fields to strings\n * - Remove the `__v` version key\n *\n * @template S - The type of the schema.\n *\n * @param schema - The Mongoose schema to apply the plugin to.\n */\nexport function mongooseNormalizePlugin<S extends Schema>(schema: S) {\n const toJson = schema.get('toJSON');\n const toJsonTransform = toJson?.transform;\n schema.set(\n 'toJSON',\n {\n ...toJson,\n transform(doc: any, ret: any, options: any) {\n // eslint-disable-next-line style/object-curly-newline\n const { __v, _id, ...copiedRet } = ret;\n copiedRet.id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;\n for (const path in schema.paths) {\n if (schema.paths[path]?.options?.private) lodashUnset(copiedRet, path);\n if (schema.paths[path]?.instance === 'Decimal128') {\n const value: Types.Decimal128 | undefined = lodashGet(copiedRet, path);\n if (value) lodashSet(copiedRet, path, value.toString());\n }\n }\n\n if (toJsonTransform && typeof toJsonTransform !== 'boolean') {\n return toJsonTransform(doc, copiedRet, options);\n }\n\n return copiedRet;\n },\n },\n );\n}\n"],"names":[],"mappings":";;;;;AAAA;AASA;;;;;;;;;;;;;AAaG;AACG,SAAU,uBAAuB,CAAmB,MAAS,EAAA;IAC/D,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC;AACnC,IAAA,MAAM,eAAe,GAAG,MAAM,EAAE,SAAS;AACzC,IAAA,MAAM,CAAC,GAAG,CACN,QAAQ,EACR;AACI,QAAA,GAAG,MAAM;AACT,QAAA,SAAS,CAAC,GAAQ,EAAE,GAAQ,EAAE,OAAY,EAAA;;YAEtC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,SAAS,EAAE,GAAG,GAAG;AACtC,YAAA,SAAS,CAAC,EAAE,GAAG,GAAG,YAAY,KAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,WAAW,EAAE,GAAG,GAAG;AACtE,YAAA,KAAK,MAAM,IAAI,IAAI,MAAM,CAAC,KAAK,EAAE;gBAC7B,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,OAAO;AAAE,oBAAA,WAAW,CAAC,SAAS,EAAE,IAAI,CAAC;gBACtE,IAAI,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE,QAAQ,KAAK,YAAY,EAAE;oBAC/C,MAAM,KAAK,GAAiC,SAAS,CAAC,SAAS,EAAE,IAAI,CAAC;AACtE,oBAAA,IAAI,KAAK;wBAAE,SAAS,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;;;AAI/D,YAAA,IAAI,eAAe,IAAI,OAAO,eAAe,KAAK,SAAS,EAAE;gBACzD,OAAO,eAAe,CAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC;;AAGnD,YAAA,OAAO,SAAS;SACnB;AACJ,KAAA,CACJ;AACL;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "3.0.3",
3
+ "version": "3.0.4",
4
4
  "description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
5
5
  "author": "kiki-kanri",
6
6
  "license": "MIT",
@@ -67,14 +67,14 @@
67
67
  },
68
68
  "devDependencies": {
69
69
  "@kikiutils/changelogen": "^0.8.0",
70
- "@kikiutils/eslint-config": "^0.12.0",
71
- "@kikiutils/tsconfigs": "^4.0.0",
70
+ "@kikiutils/eslint-config": "^0.12.2",
71
+ "@kikiutils/tsconfigs": "^4.1.0",
72
72
  "@types/jest": "^29.5.14",
73
73
  "@types/lodash": "^4.17.16",
74
- "@types/node": "^22.14.0",
74
+ "@types/node": "^22.14.1",
75
75
  "jest": "^29.7.0",
76
- "ts-jest": "^29.3.1",
77
- "ts-project-builder": "^4.0.0"
76
+ "ts-jest": "^29.3.2",
77
+ "ts-project-builder": "^4.0.1"
78
78
  },
79
79
  "pnpm": {
80
80
  "onlyBuiltDependencies": [
@@ -1,10 +1,11 @@
1
+ // @ts-expect-error Ignore this error.
1
2
  import lodashGet from 'lodash/get.js';
3
+ // @ts-expect-error Ignore this error.
2
4
  import lodashSet from 'lodash/set.js';
5
+ // @ts-expect-error Ignore this error.
3
6
  import lodashUnset from 'lodash/unset.js';
4
- import type {
5
- Schema,
6
- Types,
7
- } from 'mongoose';
7
+ import { Types } from 'mongoose';
8
+ import type { Schema } from 'mongoose';
8
9
 
9
10
  /**
10
11
  * Mongoose plugin to normalize the JSON output of documents.
@@ -28,9 +29,9 @@ export function mongooseNormalizePlugin<S extends Schema>(schema: S) {
28
29
  {
29
30
  ...toJson,
30
31
  transform(doc: any, ret: any, options: any) {
31
- const copiedRet = { ...ret };
32
- copiedRet.id = copiedRet._id?.toHexString();
33
- delete copiedRet._id;
32
+ // eslint-disable-next-line style/object-curly-newline
33
+ const { __v, _id, ...copiedRet } = ret;
34
+ copiedRet.id = _id instanceof Types.ObjectId ? _id.toHexString() : _id;
34
35
  for (const path in schema.paths) {
35
36
  if (schema.paths[path]?.options?.private) lodashUnset(copiedRet, path);
36
37
  if (schema.paths[path]?.instance === 'Decimal128') {
@@ -39,7 +40,6 @@ export function mongooseNormalizePlugin<S extends Schema>(schema: S) {
39
40
  }
40
41
  }
41
42
 
42
- delete copiedRet.__v;
43
43
  if (toJsonTransform && typeof toJsonTransform !== 'boolean') {
44
44
  return toJsonTransform(doc, copiedRet, options);
45
45
  }