@kikiutils/mongoose 1.0.5 → 1.2.0
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 +20 -63
- package/_connections.cjs +12 -0
- package/_connections.d.ts +9 -0
- package/_connections.mjs +10 -0
- package/constants.cjs +16 -0
- package/constants.d.ts +1 -11
- package/constants.mjs +6 -10
- package/package.json +29 -15
- package/plugins/normalize.cjs +53 -0
- package/plugins/normalize.mjs +6 -3
- package/types/index.d.ts +4 -1
- package/utils.cjs +265 -0
- package/utils.mjs +14 -11
- package/types/aggregate-paginate.mjs +0 -7
- package/types/data.mjs +0 -1
- package/types/index.mjs +0 -1
- package/types/options.mjs +0 -1
- package/types/paginate.mjs +0 -1
- package/types/schema.mjs +0 -1
package/README.md
CHANGED
|
@@ -4,85 +4,42 @@
|
|
|
4
4
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
5
|
[![License][license-src]][license-href]
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.
|
|
8
8
|
|
|
9
9
|
- [✨ Release Notes](./CHANGELOG.md)
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
- TypeScript support for better type safety and IntelliSense.
|
|
23
|
-
- Flexible model creation with `buildMongooseModel`, supporting custom connections and plugin configurations.
|
|
13
|
+
- ✨ Flexible Connections: Choose to use a specific connection or fall back to a default connection, with an automatic creation of a connection if none is provided
|
|
14
|
+
- ✨ Plugin Integration: Automatically integrates `mongoose-aggregate-paginate-v2`, `mongoose-paginate-v2`, and a custom normalization plugin for enhanced JSON output
|
|
15
|
+
- 🛠 Predefined Schemas: Provides a set of commonly used schema definitions such as boolean, decimal128, number, objectId, and string
|
|
16
|
+
- 🛠 Customizable: Allows for custom schemas and settings, including automatic rounding and fixed decimal places for `Decimal128` fields
|
|
17
|
+
- 🔄 Enhanced JSON Output: Adds an `id` field, removes `_id` and `__v` fields, excludes private fields, and converts `Decimal128` fields to strings in JSON output
|
|
18
|
+
- 🧩 Reference Schema Creation: Easily create schema definitions for ObjectId references with attributes like `private`, `required`, and `unique`
|
|
19
|
+
- 🧩 String Schema Creation: Generate string schema definitions with attributes such as `private`, `required`, `short`, `trim`, and `unique`
|
|
20
|
+
- 🧩 Decimal128 Field Conversion: Set up getters to convert `Decimal128` fields to strings for easier handling in documents
|
|
21
|
+
- 🧩 Document Conversion: Convert documents or ObjectIds to documents using specified models, with optional field selection
|
|
24
22
|
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
# Using pnpm
|
|
29
|
-
pnpm add @kikiutils/mongoose
|
|
30
|
-
|
|
31
|
-
# Using yarn
|
|
32
|
-
yarn add @kikiutils/mongoose
|
|
33
|
-
|
|
34
|
-
# Using npm
|
|
35
|
-
npm i @kikiutils/mongoose
|
|
36
|
-
|
|
37
|
-
# Using bun
|
|
38
|
-
bun add @kikiutils/mongoose
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## Environmental Requirements
|
|
42
|
-
|
|
43
|
-
- ESM only
|
|
44
|
-
- Mongoose v8
|
|
45
|
-
- NodeJS 18 or higher
|
|
46
|
-
|
|
47
|
-
## Usage
|
|
23
|
+
## Environment Requirements
|
|
48
24
|
|
|
49
|
-
|
|
25
|
+
- Mongoose version 8.x
|
|
26
|
+
- Node.js version 18 or higher
|
|
50
27
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
```bash
|
|
54
|
-
cd examples
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
2. Install the dependencies
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
pnpm i
|
|
61
|
-
```
|
|
28
|
+
## Installation
|
|
62
29
|
|
|
63
|
-
|
|
30
|
+
Add dependency (example using pnpm).
|
|
64
31
|
|
|
65
32
|
```bash
|
|
66
|
-
|
|
33
|
+
pnpm add @kikiutils/mongoose
|
|
67
34
|
```
|
|
68
35
|
|
|
69
|
-
|
|
70
|
-
5. Run the example code
|
|
71
|
-
|
|
72
|
-
- If you use bun, run this command directly
|
|
73
|
-
```bash
|
|
74
|
-
bun --watch run ./src/index.ts
|
|
75
|
-
```
|
|
36
|
+
You can also use yarn, npm, or bun to add the dependency.
|
|
76
37
|
|
|
77
|
-
|
|
78
|
-
```bash
|
|
79
|
-
pnpm run build
|
|
80
|
-
MONGODB_URI="mongodb://127.0.0.1:27017/kikiutils-mongoose-test?directConnection=true" node ./dist/index.mjs
|
|
81
|
-
```
|
|
38
|
+
That's it! You're ready to use this package in your project. Check out the instructions for [usage](#usage) below ✨.
|
|
82
39
|
|
|
83
|
-
|
|
40
|
+
# Usage
|
|
84
41
|
|
|
85
|
-
|
|
42
|
+
Please refer to the [examples](./examples/README.md) folder for usage instructions of this package.
|
|
86
43
|
|
|
87
44
|
## License
|
|
88
45
|
|
package/_connections.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mongoose connections.
|
|
5
|
+
*
|
|
6
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
7
|
+
* database connections to be managed within the application. An optional `default`
|
|
8
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
9
|
+
*/
|
|
10
|
+
const mongooseConnections = {};
|
|
11
|
+
|
|
12
|
+
exports.mongooseConnections = mongooseConnections;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MongooseConnections } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Mongoose connections.
|
|
4
|
+
*
|
|
5
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
6
|
+
* database connections to be managed within the application. An optional `default`
|
|
7
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
8
|
+
*/
|
|
9
|
+
export declare const mongooseConnections: MongooseConnections;
|
package/_connections.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mongoose connections.
|
|
3
|
+
*
|
|
4
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
5
|
+
* database connections to be managed within the application. An optional `default`
|
|
6
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
7
|
+
*/
|
|
8
|
+
const mongooseConnections = {};
|
|
9
|
+
|
|
10
|
+
export { mongooseConnections };
|
package/constants.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const utils = require('./utils.cjs');
|
|
4
|
+
const _connections = require('./_connections.cjs');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Common Mongoose schemas.
|
|
8
|
+
*
|
|
9
|
+
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
10
|
+
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
11
|
+
* field types and configurations used across multiple models.
|
|
12
|
+
*/
|
|
13
|
+
const commonMongooseSchemas = utils.createCommonMongooseSchemas();
|
|
14
|
+
|
|
15
|
+
exports.mongooseConnections = _connections.mongooseConnections;
|
|
16
|
+
exports.commonMongooseSchemas = commonMongooseSchemas;
|
package/constants.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export { mongooseConnections } from './_connections';
|
|
2
2
|
/**
|
|
3
3
|
* Common Mongoose schemas.
|
|
4
4
|
*
|
|
@@ -167,13 +167,3 @@ export declare const commonMongooseSchemas: {
|
|
|
167
167
|
};
|
|
168
168
|
};
|
|
169
169
|
};
|
|
170
|
-
/**
|
|
171
|
-
* Mongoose connections.
|
|
172
|
-
*
|
|
173
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
174
|
-
* database connections to be managed within the application. An optional `default`
|
|
175
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
176
|
-
*/
|
|
177
|
-
export declare const mongooseConnections: Record<string, Connection> & {
|
|
178
|
-
default?: Connection;
|
|
179
|
-
};
|
package/constants.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { createCommonMongooseSchemas } from
|
|
1
|
+
import { createCommonMongooseSchemas } from './utils.mjs';
|
|
2
|
+
export { mongooseConnections } from './_connections.mjs';
|
|
3
|
+
|
|
2
4
|
/**
|
|
3
5
|
* Common Mongoose schemas.
|
|
4
6
|
*
|
|
@@ -6,12 +8,6 @@ import { createCommonMongooseSchemas } from "./utils.mjs";
|
|
|
6
8
|
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
7
9
|
* field types and configurations used across multiple models.
|
|
8
10
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
14
|
-
* database connections to be managed within the application. An optional `default`
|
|
15
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
16
|
-
*/
|
|
17
|
-
export const mongooseConnections = {};
|
|
11
|
+
const commonMongooseSchemas = createCommonMongooseSchemas();
|
|
12
|
+
|
|
13
|
+
export { commonMongooseSchemas };
|
package/package.json
CHANGED
|
@@ -1,49 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "1.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.2.0",
|
|
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
|
"license": "MIT",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
|
|
10
10
|
},
|
|
11
|
-
"type": "module",
|
|
12
11
|
"scripts": {
|
|
13
|
-
"build": "
|
|
12
|
+
"build": "rollup -c --configPlugin typescript=\"{ tsconfig: './tsconfig.rollup.json' }\"",
|
|
14
13
|
"bumplog": "changelogen --bump"
|
|
15
14
|
},
|
|
16
15
|
"dependencies": {
|
|
17
16
|
"decimal.js": "^10.4.3",
|
|
18
|
-
"lodash
|
|
19
|
-
"mongoose": "^8.
|
|
17
|
+
"lodash": "^4.17.21",
|
|
18
|
+
"mongoose": "^8.5.1",
|
|
20
19
|
"mongoose-aggregate-paginate-v2": "^1.1.1",
|
|
21
20
|
"mongoose-paginate-v2": "^1.8.2"
|
|
22
21
|
},
|
|
23
22
|
"devDependencies": {
|
|
24
23
|
"@kikiutils/prettierrc": "^1.3.2",
|
|
25
|
-
"@kikiutils/tsconfigs": "^
|
|
26
|
-
"@
|
|
27
|
-
"@
|
|
24
|
+
"@kikiutils/tsconfigs": "^3.0.1",
|
|
25
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
26
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
27
|
+
"@types/lodash": "^4.17.7",
|
|
28
|
+
"@types/node": "^20.14.11",
|
|
28
29
|
"changelogen": "^0.5.5",
|
|
29
|
-
"
|
|
30
|
+
"rollup": "^4.19.0",
|
|
31
|
+
"rollup-plugin-delete": "^2.0.0",
|
|
32
|
+
"rollup-plugin-node-externals": "^7.1.2"
|
|
30
33
|
},
|
|
31
34
|
"keywords": [
|
|
35
|
+
"Decimal128",
|
|
36
|
+
"json",
|
|
37
|
+
"mongo",
|
|
32
38
|
"mongoose",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
39
|
+
"normalization",
|
|
40
|
+
"pagination",
|
|
41
|
+
"plugin",
|
|
42
|
+
"schema"
|
|
36
43
|
],
|
|
37
44
|
"engines": {
|
|
38
45
|
"node": ">=18"
|
|
39
46
|
},
|
|
40
47
|
"exports": {
|
|
41
48
|
"./*": {
|
|
42
|
-
"
|
|
49
|
+
"import": "./*.mjs",
|
|
50
|
+
"require": "./*.cjs",
|
|
43
51
|
"types": "./*.d.ts"
|
|
44
52
|
},
|
|
53
|
+
"./_connections": {
|
|
54
|
+
"import": null,
|
|
55
|
+
"require": null,
|
|
56
|
+
"types": null
|
|
57
|
+
},
|
|
45
58
|
"./plugins/*": {
|
|
46
|
-
"
|
|
59
|
+
"import": "./plugins/*.mjs",
|
|
60
|
+
"require": "./plugins/*.cjs",
|
|
47
61
|
"types": "./plugins/*.d.ts"
|
|
48
62
|
},
|
|
49
63
|
"./types": {
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const lodash = require('lodash');
|
|
6
|
+
|
|
7
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
8
|
+
|
|
9
|
+
const lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
|
|
10
|
+
|
|
11
|
+
const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash__default;
|
|
12
|
+
/**
|
|
13
|
+
* Mongoose plugin to normalize the JSON output of documents.
|
|
14
|
+
*
|
|
15
|
+
* This plugin modifies the `toJSON` method of Mongoose schemas to:
|
|
16
|
+
* - Add an `id` field equal to the string representation of `_id`
|
|
17
|
+
* - Remove the `_id` field
|
|
18
|
+
* - Exclude fields marked as `private` from the JSON output
|
|
19
|
+
* - Convert `Decimal128` fields to strings
|
|
20
|
+
* - Remove the `__v` version key
|
|
21
|
+
*
|
|
22
|
+
* @template S - The type of the schema.
|
|
23
|
+
*
|
|
24
|
+
* @param schema - The Mongoose schema to apply the plugin to.
|
|
25
|
+
*/
|
|
26
|
+
const mongooseNormalizePlugin = (schema) => {
|
|
27
|
+
const toJSON = schema.get('toJSON');
|
|
28
|
+
const toJSONTransform = toJSON?.transform;
|
|
29
|
+
schema.set('toJSON', {
|
|
30
|
+
...toJSON,
|
|
31
|
+
transform(doc, ret, options) {
|
|
32
|
+
const copiedRet = { ...ret };
|
|
33
|
+
copiedRet.id = copiedRet._id?.toHexString();
|
|
34
|
+
delete copiedRet._id;
|
|
35
|
+
for (const path in schema.paths) {
|
|
36
|
+
if (schema.paths[path]?.options?.private)
|
|
37
|
+
lodashUnset(copiedRet, path);
|
|
38
|
+
if (schema.paths[path]?.instance === 'Decimal128') {
|
|
39
|
+
const value = lodashGet(copiedRet, path);
|
|
40
|
+
if (value)
|
|
41
|
+
lodashSet(copiedRet, path, value.toString());
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
delete copiedRet.__v;
|
|
45
|
+
if (toJSONTransform && typeof toJSONTransform !== 'boolean')
|
|
46
|
+
return toJSONTransform(doc, copiedRet, options);
|
|
47
|
+
return copiedRet;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
exports.default = mongooseNormalizePlugin;
|
|
53
|
+
exports.mongooseNormalizePlugin = mongooseNormalizePlugin;
|
package/plugins/normalize.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
|
|
3
|
+
const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash;
|
|
2
4
|
/**
|
|
3
5
|
* Mongoose plugin to normalize the JSON output of documents.
|
|
4
6
|
*
|
|
@@ -13,7 +15,7 @@ import { get as lodashGet, set as lodashSet, unset as lodashUnset } from 'lodash
|
|
|
13
15
|
*
|
|
14
16
|
* @param schema - The Mongoose schema to apply the plugin to.
|
|
15
17
|
*/
|
|
16
|
-
|
|
18
|
+
const mongooseNormalizePlugin = (schema) => {
|
|
17
19
|
const toJSON = schema.get('toJSON');
|
|
18
20
|
const toJSONTransform = toJSON?.transform;
|
|
19
21
|
schema.set('toJSON', {
|
|
@@ -38,4 +40,5 @@ export const mongooseNormalizePlugin = (schema) => {
|
|
|
38
40
|
}
|
|
39
41
|
});
|
|
40
42
|
};
|
|
41
|
-
|
|
43
|
+
|
|
44
|
+
export { mongooseNormalizePlugin as default, mongooseNormalizePlugin };
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import type { AggregatePaginateModel, HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
|
|
1
|
+
import type { AggregatePaginateModel, Connection, HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
|
|
2
2
|
export type {} from './aggregate-paginate';
|
|
3
3
|
export type {} from './paginate';
|
|
4
4
|
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
|
|
5
5
|
}
|
|
6
|
+
export interface MongooseConnections {
|
|
7
|
+
default?: Connection;
|
|
8
|
+
}
|
|
6
9
|
declare global {
|
|
7
10
|
/**
|
|
8
11
|
* Type definition for a base Mongoose document type.
|
package/utils.cjs
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Decimal = require('decimal.js');
|
|
4
|
+
const lodash = require('lodash');
|
|
5
|
+
const mongoose = require('mongoose');
|
|
6
|
+
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
|
|
7
|
+
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
8
|
+
const _connections = require('./_connections.cjs');
|
|
9
|
+
const normalize = require('./plugins/normalize.cjs');
|
|
10
|
+
|
|
11
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
12
|
+
|
|
13
|
+
const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
|
|
14
|
+
const lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
|
|
15
|
+
const mongoose__default = /*#__PURE__*/_interopDefaultCompat(mongoose);
|
|
16
|
+
const mongooseAggregatePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongooseAggregatePaginate);
|
|
17
|
+
const mongoosePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongoosePaginate);
|
|
18
|
+
|
|
19
|
+
const { merge } = lodash__default;
|
|
20
|
+
/**
|
|
21
|
+
* Builds and returns a Mongoose model with specified schema and options.
|
|
22
|
+
*
|
|
23
|
+
* This function sets up a Mongoose model with optional plugins and timestamp settings.
|
|
24
|
+
* It can optionally use a specified connection or the default connection.
|
|
25
|
+
*
|
|
26
|
+
* Determine the connection to use:
|
|
27
|
+
* 1. Use the connection provided in the options, if available.
|
|
28
|
+
* 2. Otherwise, use the default connection stored in `mongooseConnections`.
|
|
29
|
+
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
30
|
+
* or fallback to a local MongoDB instance.
|
|
31
|
+
*
|
|
32
|
+
* @template DocType - The type of the document.
|
|
33
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
34
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
35
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
36
|
+
*
|
|
37
|
+
* @param collection - The name of the collection.
|
|
38
|
+
* @param name - The name of the model.
|
|
39
|
+
* @param schema - The Mongoose schema to use for the model.
|
|
40
|
+
* @param options - Optional settings for building the model.
|
|
41
|
+
*
|
|
42
|
+
* @returns The created Mongoose model.
|
|
43
|
+
*/
|
|
44
|
+
function buildMongooseModel(collection, name, schema, options) {
|
|
45
|
+
if (options?.enableNormalizePlugin !== false)
|
|
46
|
+
schema.plugin(normalize.mongooseNormalizePlugin);
|
|
47
|
+
schema.plugin(mongooseAggregatePaginate__default);
|
|
48
|
+
schema.plugin(mongoosePaginate__default);
|
|
49
|
+
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
50
|
+
return (options?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
54
|
+
*
|
|
55
|
+
* This function generates a set of commonly used schema definitions, such as boolean,
|
|
56
|
+
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
57
|
+
* options for Decimal128 fields.
|
|
58
|
+
*
|
|
59
|
+
* @template T - The type for custom schemas.
|
|
60
|
+
*
|
|
61
|
+
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
62
|
+
* @param options - Optional settings for creating the common schemas.
|
|
63
|
+
*
|
|
64
|
+
* @returns The merged common and custom schemas.
|
|
65
|
+
*/
|
|
66
|
+
function createCommonMongooseSchemas(customSchemas, options) {
|
|
67
|
+
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
68
|
+
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal__default.ROUND_DOWN;
|
|
69
|
+
const baseAutoRoundAndToFixedDecimal128 = {
|
|
70
|
+
set: (value) => new Decimal__default(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
|
|
71
|
+
type: mongoose__default.Schema.Types.Decimal128
|
|
72
|
+
};
|
|
73
|
+
const baseRequiredBoolean = { required: true, type: Boolean };
|
|
74
|
+
const baseRequiredNumber = { required: true, type: Number };
|
|
75
|
+
const baseRequiredObjectId = { required: true, type: mongoose__default.Schema.Types.ObjectId };
|
|
76
|
+
return merge({
|
|
77
|
+
boolean: {
|
|
78
|
+
defaultFalse: {
|
|
79
|
+
nonRequired: { default: false, type: Boolean },
|
|
80
|
+
required: { ...baseRequiredBoolean, default: false }
|
|
81
|
+
},
|
|
82
|
+
defaultTrue: {
|
|
83
|
+
nonRequired: { default: true, type: Boolean },
|
|
84
|
+
required: { ...baseRequiredBoolean, default: true }
|
|
85
|
+
},
|
|
86
|
+
nonRequired: { type: Boolean },
|
|
87
|
+
required: baseRequiredBoolean
|
|
88
|
+
},
|
|
89
|
+
decimal128: {
|
|
90
|
+
autoRoundAndToFixed: {
|
|
91
|
+
nonRequired: baseAutoRoundAndToFixedDecimal128,
|
|
92
|
+
required: { ...baseAutoRoundAndToFixedDecimal128, required: true }
|
|
93
|
+
},
|
|
94
|
+
nonRequired: { type: mongoose__default.Schema.Types.Decimal128 },
|
|
95
|
+
required: { required: true, type: mongoose__default.Schema.Types.Decimal128 }
|
|
96
|
+
},
|
|
97
|
+
number: {
|
|
98
|
+
nonRequired: { type: Number },
|
|
99
|
+
required: baseRequiredNumber,
|
|
100
|
+
unique: {
|
|
101
|
+
nonRequired: { type: Number, unique: true },
|
|
102
|
+
required: { ...baseRequiredNumber, unique: true }
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
objectId: {
|
|
106
|
+
nonRequired: { type: mongoose__default.Schema.Types.ObjectId },
|
|
107
|
+
required: baseRequiredObjectId,
|
|
108
|
+
unique: {
|
|
109
|
+
nonRequired: { type: mongoose__default.Schema.Types.ObjectId, unique: true },
|
|
110
|
+
required: { ...baseRequiredObjectId, unique: true }
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
string: {
|
|
114
|
+
nonRequired: createMongooseStringSchema(),
|
|
115
|
+
private: {
|
|
116
|
+
nonRequired: createMongooseStringSchema('private'),
|
|
117
|
+
required: createMongooseStringSchema('private', 'required'),
|
|
118
|
+
short: {
|
|
119
|
+
nonRequired: createMongooseStringSchema('private', 'short'),
|
|
120
|
+
required: createMongooseStringSchema('private', 'required', 'short'),
|
|
121
|
+
trimmed: {
|
|
122
|
+
nonRequired: createMongooseStringSchema('private', 'short', 'trim'),
|
|
123
|
+
required: createMongooseStringSchema('private', 'required', 'short', 'trim'),
|
|
124
|
+
unique: {
|
|
125
|
+
nonRequired: createMongooseStringSchema('private', 'short', 'trim', 'unique'),
|
|
126
|
+
required: createMongooseStringSchema('private', 'required', 'short', 'trim', 'unique')
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
unique: {
|
|
130
|
+
nonRequired: createMongooseStringSchema('private', 'short', 'unique'),
|
|
131
|
+
required: createMongooseStringSchema('private', 'required', 'short', 'unique')
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
trimmed: {
|
|
135
|
+
nonRequired: createMongooseStringSchema('private', 'trim'),
|
|
136
|
+
required: createMongooseStringSchema('private', 'required', 'trim'),
|
|
137
|
+
unique: {
|
|
138
|
+
nonRequired: createMongooseStringSchema('private', 'trim', 'unique'),
|
|
139
|
+
required: createMongooseStringSchema('private', 'required', 'trim', 'unique')
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
unique: {
|
|
143
|
+
nonRequired: createMongooseStringSchema('private', 'unique'),
|
|
144
|
+
required: createMongooseStringSchema('private', 'required', 'unique')
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
required: createMongooseStringSchema('required'),
|
|
148
|
+
short: {
|
|
149
|
+
nonRequired: createMongooseStringSchema('short'),
|
|
150
|
+
required: createMongooseStringSchema('required', 'short'),
|
|
151
|
+
trimmed: {
|
|
152
|
+
nonRequired: createMongooseStringSchema('short', 'trim'),
|
|
153
|
+
required: createMongooseStringSchema('required', 'short', 'trim'),
|
|
154
|
+
unique: {
|
|
155
|
+
nonRequired: createMongooseStringSchema('short', 'trim', 'unique'),
|
|
156
|
+
required: createMongooseStringSchema('required', 'short', 'trim', 'unique')
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
unique: {
|
|
160
|
+
nonRequired: createMongooseStringSchema('short', 'unique'),
|
|
161
|
+
required: createMongooseStringSchema('required', 'short', 'unique')
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
trimmed: {
|
|
165
|
+
nonRequired: createMongooseStringSchema('trim'),
|
|
166
|
+
required: createMongooseStringSchema('required', 'trim'),
|
|
167
|
+
unique: {
|
|
168
|
+
nonRequired: createMongooseStringSchema('trim', 'unique'),
|
|
169
|
+
required: createMongooseStringSchema('required', 'trim', 'unique')
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
unique: {
|
|
173
|
+
nonRequired: createMongooseStringSchema('unique'),
|
|
174
|
+
required: createMongooseStringSchema('required', 'unique')
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}, customSchemas);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Creates a Mongoose schema definition for an ObjectId reference with specified attributes.
|
|
181
|
+
*
|
|
182
|
+
* This function generates a schema definition for an ObjectId reference field, including
|
|
183
|
+
* attributes such as 'private', 'required', and 'unique'.
|
|
184
|
+
*
|
|
185
|
+
* @template R - The type of the reference model name.
|
|
186
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
187
|
+
*
|
|
188
|
+
* @param refModelName - The name of the reference model.
|
|
189
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
190
|
+
*
|
|
191
|
+
* @returns The generated ObjectId reference schema definition.
|
|
192
|
+
*/
|
|
193
|
+
function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
|
|
194
|
+
const schema = { ref: refModelName, type: mongoose__default.Schema.Types.ObjectId };
|
|
195
|
+
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
196
|
+
return schema;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Creates a Mongoose schema definition for a string field with specified attributes.
|
|
200
|
+
*
|
|
201
|
+
* This function generates a schema definition for a string field, including attributes
|
|
202
|
+
* such as 'private', 'required', 'short', 'trim', and 'unique'. If the 'short' attribute
|
|
203
|
+
* is included, the `maxlength` is set to 16.
|
|
204
|
+
*
|
|
205
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
206
|
+
*
|
|
207
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
208
|
+
*
|
|
209
|
+
* @returns The generated string schema definition.
|
|
210
|
+
*/
|
|
211
|
+
function createMongooseStringSchema(...attributes) {
|
|
212
|
+
const schema = { type: String };
|
|
213
|
+
new Set(attributes).forEach((attribute) => {
|
|
214
|
+
if (attribute === 'short')
|
|
215
|
+
return (schema.maxlength = 16);
|
|
216
|
+
schema[attribute] = true;
|
|
217
|
+
});
|
|
218
|
+
return schema;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Converts a document or ObjectId to a document using the specified model.
|
|
222
|
+
*
|
|
223
|
+
* This function takes a document or ObjectId and returns the corresponding document
|
|
224
|
+
* from the database using the specified model. Optionally, specific fields can be selected.
|
|
225
|
+
*
|
|
226
|
+
* @template D - The type of the hydrated document.
|
|
227
|
+
* @template DocType - The type of the document.
|
|
228
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
229
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
230
|
+
*
|
|
231
|
+
* @param documentOrObjectId - The document or ObjectId to convert.
|
|
232
|
+
* @param model - The Mongoose model to use for the conversion.
|
|
233
|
+
* @param selectFields - Optional fields to select from the document.
|
|
234
|
+
*
|
|
235
|
+
* @returns The corresponding document or null if not found.
|
|
236
|
+
*/
|
|
237
|
+
async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, selectFields) {
|
|
238
|
+
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof mongoose.Types.ObjectId)
|
|
239
|
+
return (await model.findById(documentOrObjectId).select(selectFields || []));
|
|
240
|
+
return documentOrObjectId;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
|
|
244
|
+
*
|
|
245
|
+
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
246
|
+
* converting the values to strings for easier use.
|
|
247
|
+
*
|
|
248
|
+
* @template DocType - The type of the document.
|
|
249
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
250
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
251
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
252
|
+
*
|
|
253
|
+
* @param schema - The Mongoose schema to modify.
|
|
254
|
+
* @param fields - The Decimal128 fields to add the getter to.
|
|
255
|
+
*/
|
|
256
|
+
function setupDecimal128FieldsToStringGetter(schema, ...fields) {
|
|
257
|
+
fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
exports.buildMongooseModel = buildMongooseModel;
|
|
261
|
+
exports.createCommonMongooseSchemas = createCommonMongooseSchemas;
|
|
262
|
+
exports.createMongooseObjectIdRefSchema = createMongooseObjectIdRefSchema;
|
|
263
|
+
exports.createMongooseStringSchema = createMongooseStringSchema;
|
|
264
|
+
exports.mongooseDocumentOrObjectIdToDocument = mongooseDocumentOrObjectIdToDocument;
|
|
265
|
+
exports.setupDecimal128FieldsToStringGetter = setupDecimal128FieldsToStringGetter;
|
package/utils.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
|
-
import
|
|
3
|
-
import mongoose from 'mongoose';
|
|
4
|
-
import { Types } from 'mongoose';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
import mongoose, { Types } from 'mongoose';
|
|
5
4
|
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
6
5
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
7
|
-
import { mongooseConnections } from
|
|
8
|
-
import mongooseNormalizePlugin from
|
|
6
|
+
import { mongooseConnections } from './_connections.mjs';
|
|
7
|
+
import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
|
|
8
|
+
|
|
9
|
+
const { merge } = lodash;
|
|
9
10
|
/**
|
|
10
11
|
* Builds and returns a Mongoose model with specified schema and options.
|
|
11
12
|
*
|
|
@@ -30,7 +31,7 @@ import mongooseNormalizePlugin from "./plugins/normalize.mjs";
|
|
|
30
31
|
*
|
|
31
32
|
* @returns The created Mongoose model.
|
|
32
33
|
*/
|
|
33
|
-
|
|
34
|
+
function buildMongooseModel(collection, name, schema, options) {
|
|
34
35
|
if (options?.enableNormalizePlugin !== false)
|
|
35
36
|
schema.plugin(mongooseNormalizePlugin);
|
|
36
37
|
schema.plugin(mongooseAggregatePaginate);
|
|
@@ -52,7 +53,7 @@ export function buildMongooseModel(collection, name, schema, options) {
|
|
|
52
53
|
*
|
|
53
54
|
* @returns The merged common and custom schemas.
|
|
54
55
|
*/
|
|
55
|
-
|
|
56
|
+
function createCommonMongooseSchemas(customSchemas, options) {
|
|
56
57
|
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
57
58
|
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
58
59
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
@@ -179,7 +180,7 @@ export function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
179
180
|
*
|
|
180
181
|
* @returns The generated ObjectId reference schema definition.
|
|
181
182
|
*/
|
|
182
|
-
|
|
183
|
+
function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
|
|
183
184
|
const schema = { ref: refModelName, type: mongoose.Schema.Types.ObjectId };
|
|
184
185
|
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
185
186
|
return schema;
|
|
@@ -197,7 +198,7 @@ export function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
|
|
|
197
198
|
*
|
|
198
199
|
* @returns The generated string schema definition.
|
|
199
200
|
*/
|
|
200
|
-
|
|
201
|
+
function createMongooseStringSchema(...attributes) {
|
|
201
202
|
const schema = { type: String };
|
|
202
203
|
new Set(attributes).forEach((attribute) => {
|
|
203
204
|
if (attribute === 'short')
|
|
@@ -223,7 +224,7 @@ export function createMongooseStringSchema(...attributes) {
|
|
|
223
224
|
*
|
|
224
225
|
* @returns The corresponding document or null if not found.
|
|
225
226
|
*/
|
|
226
|
-
|
|
227
|
+
async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, selectFields) {
|
|
227
228
|
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
|
|
228
229
|
return (await model.findById(documentOrObjectId).select(selectFields || []));
|
|
229
230
|
return documentOrObjectId;
|
|
@@ -242,6 +243,8 @@ export async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, m
|
|
|
242
243
|
* @param schema - The Mongoose schema to modify.
|
|
243
244
|
* @param fields - The Decimal128 fields to add the getter to.
|
|
244
245
|
*/
|
|
245
|
-
|
|
246
|
+
function setupDecimal128FieldsToStringGetter(schema, ...fields) {
|
|
246
247
|
fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|
|
247
248
|
}
|
|
249
|
+
|
|
250
|
+
export { buildMongooseModel, createCommonMongooseSchemas, createMongooseObjectIdRefSchema, createMongooseStringSchema, mongooseDocumentOrObjectIdToDocument, setupDecimal128FieldsToStringGetter };
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Based on type declarations for mongoose-paginate-v2 1.3.
|
|
3
|
-
//
|
|
4
|
-
// Thanks to knyuwork <https://github.com/knyuwork>
|
|
5
|
-
// and LiRen Tu <https://github.com/tuliren> for their contribution
|
|
6
|
-
// Used with mongoose-paginate, redefined and renamed here to avoid conflicts.
|
|
7
|
-
export default mongooseAggregatePaginate;
|
package/types/data.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/types/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/types/options.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/types/paginate.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default _;
|
package/types/schema.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|