@kikiutils/mongoose 1.0.0-rc.1 → 1.0.0-rc.3
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 +12 -7
- package/constants.d.ts +16 -2
- package/constants.mjs +14 -0
- package/package.json +5 -5
- package/plugins/normalize.d.ts +16 -2
- package/plugins/normalize.mjs +14 -0
- package/types/{paginate/aggregate.d.ts → aggregate-paginate.d.ts} +1 -1
- package/types/data.d.ts +43 -41
- package/types/index.d.ts +57 -2
- package/types/options.d.ts +26 -2
- package/types/{paginate/index.d.ts → paginate.d.ts} +1 -1
- package/types/schema.d.ts +95 -5
- package/utils.d.ts +104 -8
- package/utils.mjs +109 -11
- /package/types/{paginate/aggregate.mjs → aggregate-paginate.mjs} +0 -0
- /package/types/{paginate/index.mjs → paginate.mjs} +0 -0
package/README.md
CHANGED
|
@@ -10,12 +10,17 @@ For mongoose v8 types and utils.
|
|
|
10
10
|
|
|
11
11
|
## Features
|
|
12
12
|
|
|
13
|
-
- Enhanced `toJSON` method with normalization functions
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
13
|
+
- Enhanced `toJSON` method with normalization functions:
|
|
14
|
+
- Auto-converts `_id` to `id`
|
|
15
|
+
- Removes `private` fields from the JSON output (if using the normalize plugin)
|
|
16
|
+
- Converts `Decimal128` fields to strings
|
|
17
|
+
- Removes `__v` version key
|
|
18
|
+
- Commonly used schema settings for fast schema creation with `createCommonMongooseSchemas`.
|
|
19
|
+
- Automatic timestamp field settings based on configuration.
|
|
20
|
+
- Pre-registered `mongoose-paginate-v2` and `mongoose-aggregate-paginate-v2` plugins for pagination support.
|
|
21
|
+
- Automatic Decimal128 field conversion and rounding configuration.
|
|
22
|
+
- TypeScript support for better type safety and IntelliSense.
|
|
23
|
+
- Flexible model creation with `buildMongooseModel`, supporting custom connections and plugin configurations.
|
|
19
24
|
|
|
20
25
|
## Installation
|
|
21
26
|
|
|
@@ -27,7 +32,7 @@ pnpm add @kikiutils/mongoose
|
|
|
27
32
|
yarn add @kikiutils/mongoose
|
|
28
33
|
|
|
29
34
|
# Using npm
|
|
30
|
-
npm
|
|
35
|
+
npm i @kikiutils/mongoose
|
|
31
36
|
```
|
|
32
37
|
|
|
33
38
|
## Environmental Requirements
|
package/constants.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="types/paginate
|
|
2
|
-
/// <reference path="types/paginate
|
|
1
|
+
/// <reference path="types/aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="types/paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -26,6 +26,13 @@
|
|
|
26
26
|
/// <reference types="mongoose/types/inferschematype" />
|
|
27
27
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
28
28
|
import type { Connection } from 'mongoose';
|
|
29
|
+
/**
|
|
30
|
+
* Common Mongoose schemas.
|
|
31
|
+
*
|
|
32
|
+
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
33
|
+
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
34
|
+
* field types and configurations used across multiple models.
|
|
35
|
+
*/
|
|
29
36
|
export declare const commonMongooseSchemas: {
|
|
30
37
|
readonly boolean: {
|
|
31
38
|
readonly defaultFalse: {
|
|
@@ -187,6 +194,13 @@ export declare const commonMongooseSchemas: {
|
|
|
187
194
|
};
|
|
188
195
|
};
|
|
189
196
|
};
|
|
197
|
+
/**
|
|
198
|
+
* Mongoose connections.
|
|
199
|
+
*
|
|
200
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
201
|
+
* database connections to be managed within the application. An optional `default`
|
|
202
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
203
|
+
*/
|
|
190
204
|
export declare const mongooseConnections: Record<string, Connection> & {
|
|
191
205
|
default?: Connection;
|
|
192
206
|
};
|
package/constants.mjs
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
1
|
import { createCommonMongooseSchemas } from "./utils.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Common Mongoose schemas.
|
|
4
|
+
*
|
|
5
|
+
* This constant holds a set of commonly used Mongoose schema definitions, generated
|
|
6
|
+
* by the `createCommonMongooseSchemas` utility function. These schemas include common
|
|
7
|
+
* field types and configurations used across multiple models.
|
|
8
|
+
*/
|
|
2
9
|
export const commonMongooseSchemas = createCommonMongooseSchemas();
|
|
10
|
+
/**
|
|
11
|
+
* Mongoose connections.
|
|
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
|
+
*/
|
|
3
17
|
export const mongooseConnections = {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"repository": {
|
|
@@ -29,10 +29,10 @@
|
|
|
29
29
|
"typescript": "^5.4.5"
|
|
30
30
|
},
|
|
31
31
|
"keywords": [
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
32
|
+
"mongoose",
|
|
33
|
+
"mongoose-plugins",
|
|
34
|
+
"mongoose-types",
|
|
35
|
+
"mongoose-utils"
|
|
36
36
|
],
|
|
37
37
|
"engines": {
|
|
38
38
|
"node": ">=18"
|
package/plugins/normalize.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="../types/paginate
|
|
2
|
-
/// <reference path="../types/paginate
|
|
1
|
+
/// <reference path="../types/aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="../types/paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -26,6 +26,20 @@
|
|
|
26
26
|
/// <reference types="mongoose/types/inferschematype" />
|
|
27
27
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
28
28
|
import type { Schema } from 'mongoose';
|
|
29
|
+
/**
|
|
30
|
+
* Mongoose plugin to normalize the JSON output of documents.
|
|
31
|
+
*
|
|
32
|
+
* This plugin modifies the `toJSON` method of Mongoose schemas to:
|
|
33
|
+
* - Add an `id` field equal to the string representation of `_id`
|
|
34
|
+
* - Remove the `_id` field
|
|
35
|
+
* - Exclude fields marked as `private` from the JSON output
|
|
36
|
+
* - Convert `Decimal128` fields to strings
|
|
37
|
+
* - Remove the `__v` version key
|
|
38
|
+
*
|
|
39
|
+
* @template S - The type of the schema.
|
|
40
|
+
*
|
|
41
|
+
* @param schema - The Mongoose schema to apply the plugin to.
|
|
42
|
+
*/
|
|
29
43
|
export declare const mongooseNormalizePlugin: <S extends Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, {
|
|
30
44
|
[x: string]: unknown;
|
|
31
45
|
}, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<{
|
package/plugins/normalize.mjs
CHANGED
|
@@ -1,4 +1,18 @@
|
|
|
1
1
|
import { get as lodashGet, set as lodashSet, unset as lodashUnset } from 'lodash-es';
|
|
2
|
+
/**
|
|
3
|
+
* Mongoose plugin to normalize the JSON output of documents.
|
|
4
|
+
*
|
|
5
|
+
* This plugin modifies the `toJSON` method of Mongoose schemas to:
|
|
6
|
+
* - Add an `id` field equal to the string representation of `_id`
|
|
7
|
+
* - Remove the `_id` field
|
|
8
|
+
* - Exclude fields marked as `private` from the JSON output
|
|
9
|
+
* - Convert `Decimal128` fields to strings
|
|
10
|
+
* - Remove the `__v` version key
|
|
11
|
+
*
|
|
12
|
+
* @template S - The type of the schema.
|
|
13
|
+
*
|
|
14
|
+
* @param schema - The Mongoose schema to apply the plugin to.
|
|
15
|
+
*/
|
|
2
16
|
export const mongooseNormalizePlugin = (schema) => {
|
|
3
17
|
const toJSON = schema.get('toJSON');
|
|
4
18
|
const toJSONTransform = toJSON?.transform;
|
package/types/data.d.ts
CHANGED
|
@@ -1,68 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for the base data of a Mongoose model, optionally including timestamp fields.
|
|
3
|
+
*
|
|
4
|
+
* This interface extends `_WithTimestampFieldsData` to optionally include `createdAt`
|
|
5
|
+
* and `updatedAt` fields based on the `CreatedAtField` and `UpdatedAtField` flags.
|
|
6
|
+
* It also includes a mandatory `id` field.
|
|
7
|
+
*
|
|
8
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
9
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
10
|
+
*/
|
|
1
11
|
interface _BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends _WithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
|
|
2
12
|
id: string;
|
|
3
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Interface for including timestamp fields conditionally based on the provided flags.
|
|
16
|
+
*
|
|
17
|
+
* This interface conditionally includes `createdAt` and `updatedAt` fields based on
|
|
18
|
+
* the `CreatedAt` and `UpdatedAt` boolean flags. If the flag is true, the respective
|
|
19
|
+
* field is included, otherwise it is set to `never`.
|
|
20
|
+
*
|
|
21
|
+
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
22
|
+
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
23
|
+
*/
|
|
4
24
|
interface _WithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
|
|
5
25
|
createdAt: CreatedAt extends true ? string : never;
|
|
6
26
|
updatedAt: UpdatedAt extends true ? string : never;
|
|
7
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Utility type to omit properties that are set to `never` in a given type.
|
|
30
|
+
*
|
|
31
|
+
* This type alias iterates over the properties of a given type `T` and removes any
|
|
32
|
+
* properties that are set to `never`.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type from which to omit `never` properties.
|
|
35
|
+
*/
|
|
8
36
|
type OmitNever<T> = {
|
|
9
37
|
[P in keyof T as T[P] extends never ? never : P]: T[P];
|
|
10
38
|
};
|
|
11
39
|
declare global {
|
|
12
40
|
/**
|
|
13
|
-
*
|
|
41
|
+
* Type definition for the base data of a Mongoose model, optionally including timestamp fields.
|
|
14
42
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* account: string;
|
|
18
|
-
* name: string;
|
|
19
|
-
* }
|
|
43
|
+
* This type alias uses `OmitNever` to remove any `never` properties from `_BaseMongooseModelData`
|
|
44
|
+
* based on the presence of the `CreatedAtField` and `UpdatedAtField` flags.
|
|
20
45
|
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
* // account: string;
|
|
24
|
-
* // createdAt: string;
|
|
25
|
-
* // id: string;
|
|
26
|
-
* // name: string;
|
|
27
|
-
* // updatedAt: string;
|
|
28
|
-
* // }
|
|
46
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
47
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
29
48
|
*/
|
|
30
49
|
type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<_BaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
|
|
31
50
|
/**
|
|
32
|
-
*
|
|
51
|
+
* Type definition to omit timestamp fields and other specified fields from a given type.
|
|
33
52
|
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* account: string;
|
|
37
|
-
* name: string;
|
|
38
|
-
* }
|
|
53
|
+
* This type alias uses `Omit` to remove `createdAt`, `updatedAt`, and any other specified fields (`OT`)
|
|
54
|
+
* from the type `T`.
|
|
39
55
|
*
|
|
40
|
-
* type
|
|
41
|
-
*
|
|
42
|
-
* // Resulting type:
|
|
43
|
-
* // {
|
|
44
|
-
* // account: string;
|
|
45
|
-
* // id: string;
|
|
46
|
-
* // name: string;
|
|
47
|
-
* // }
|
|
56
|
+
* @template T - The type from which to omit fields.
|
|
57
|
+
* @template OT - Additional fields to omit, specified as a union of string, number, or symbol.
|
|
48
58
|
*/
|
|
49
59
|
type OmitMongooseTimestampAndOtherFields<T, OT extends number | symbol | string = ''> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
|
|
50
60
|
/**
|
|
51
|
-
*
|
|
61
|
+
* Type definition for including timestamp fields conditionally based on the provided flags.
|
|
52
62
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* content: string;
|
|
56
|
-
* type: number;
|
|
57
|
-
* }
|
|
63
|
+
* This type alias uses `OmitNever` to remove any `never` properties from `_WithTimestampFieldsData`
|
|
64
|
+
* based on the presence of the `CreatedAt` and `UpdatedAt` flags.
|
|
58
65
|
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* // content: string;
|
|
62
|
-
* // createdAt: string;
|
|
63
|
-
* // type: number;
|
|
64
|
-
* // }
|
|
65
|
-
* }
|
|
66
|
+
* @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
|
|
67
|
+
* @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
66
68
|
*/
|
|
67
69
|
type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<_WithTimestampFieldsData<CreatedAt, UpdatedAt>>;
|
|
68
70
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="paginate
|
|
2
|
-
/// <reference path="paginate
|
|
1
|
+
/// <reference path="aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -29,13 +29,68 @@ import type { AggregatePaginateModel, HydratedDocument, PaginateModel, QueryWith
|
|
|
29
29
|
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
|
|
30
30
|
}
|
|
31
31
|
declare global {
|
|
32
|
+
/**
|
|
33
|
+
* Type definition for a base Mongoose document type.
|
|
34
|
+
*
|
|
35
|
+
* This type alias omits the `createdAt`, `id`, and `updatedAt` fields from type `T`,
|
|
36
|
+
* and conditionally includes `createdAt` and `updatedAt` fields as `Date` based on
|
|
37
|
+
* the `CreatedAtField` and `UpdatedAtField` flags.
|
|
38
|
+
*
|
|
39
|
+
* @template T - The original document type.
|
|
40
|
+
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
41
|
+
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
42
|
+
*/
|
|
32
43
|
type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = Omit<T, 'createdAt' | 'id' | 'updatedAt'> & (CreatedAtField extends true ? {
|
|
33
44
|
createdAt: Date;
|
|
34
45
|
} : {}) & (UpdatedAtField extends true ? {
|
|
35
46
|
updatedAt: Date;
|
|
36
47
|
} : {});
|
|
48
|
+
/**
|
|
49
|
+
* Type definition for a Mongoose model with pagination and aggregation capabilities.
|
|
50
|
+
*
|
|
51
|
+
* This type alias combines the `AggregatePaginateModel` and `PaginateModel` interfaces,
|
|
52
|
+
* along with custom static methods defined in `BaseModelStatics`.
|
|
53
|
+
*
|
|
54
|
+
* @template RawDocType - The raw document type as stored in MongoDB.
|
|
55
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
56
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
57
|
+
*/
|
|
37
58
|
type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
59
|
+
/**
|
|
60
|
+
* Type definition for a Mongoose document or ObjectId.
|
|
61
|
+
*
|
|
62
|
+
* This type alias represents a value that can be either a Mongoose document, a string,
|
|
63
|
+
* or an ObjectId. It is useful when working with Mongoose models where you might have
|
|
64
|
+
* references to documents that are stored either as objects or as ObjectIds.
|
|
65
|
+
*
|
|
66
|
+
* @template D - The Mongoose document type.
|
|
67
|
+
*/
|
|
38
68
|
type MongooseDocumentOrObjectId<D> = D | string | Types.ObjectId;
|
|
69
|
+
/**
|
|
70
|
+
* Type definition for the return type of a Mongoose `findOne` query.
|
|
71
|
+
*
|
|
72
|
+
* This type alias represents the return type of a `findOne` query in Mongoose.
|
|
73
|
+
* It extends the `QueryWithHelpers` type, indicating that the query can return
|
|
74
|
+
* either a document of type `DocType` or `null` if no document is found. The
|
|
75
|
+
* `QueryHelpers` type parameter allows for the inclusion of additional query
|
|
76
|
+
* helper methods.
|
|
77
|
+
*
|
|
78
|
+
* @template RawDocType - The raw document type as stored in MongoDB.
|
|
79
|
+
* @template DocType - The Mongoose document type after applying schema transformations.
|
|
80
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
81
|
+
*/
|
|
39
82
|
type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = {}> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne'>;
|
|
83
|
+
/**
|
|
84
|
+
* Type definition for a hydrated Mongoose document.
|
|
85
|
+
*
|
|
86
|
+
* This type alias represents a hydrated document in Mongoose, which is a fully
|
|
87
|
+
* populated document that includes instance methods and query helpers. It extends
|
|
88
|
+
* the `HydratedDocument` type from Mongoose, allowing for additional instance
|
|
89
|
+
* methods and query helpers to be specified.
|
|
90
|
+
*
|
|
91
|
+
* @template DocType - The Mongoose document type after applying schema transformations.
|
|
92
|
+
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
93
|
+
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
94
|
+
*/
|
|
40
95
|
type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
41
96
|
}
|
package/types/options.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="paginate
|
|
2
|
-
/// <reference path="paginate
|
|
1
|
+
/// <reference path="aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -26,13 +26,37 @@
|
|
|
26
26
|
/// <reference types="mongoose/types/inferschematype" />
|
|
27
27
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
28
28
|
import type { Connection, SchemaTimestampsConfig } from 'mongoose';
|
|
29
|
+
/**
|
|
30
|
+
* Interface for options used when building a Mongoose model.
|
|
31
|
+
*
|
|
32
|
+
* This interface defines the optional configuration settings that can be provided
|
|
33
|
+
* when creating a Mongoose model. It includes options for specifying the database
|
|
34
|
+
* connection, enabling the normalize plugin, and configuring timestamp fields.
|
|
35
|
+
*/
|
|
29
36
|
export interface BuildMongooseModelOptions {
|
|
37
|
+
/**
|
|
38
|
+
* The Mongoose connection to be used for the model.
|
|
39
|
+
*
|
|
40
|
+
* This is an optional setting that allows specifying a custom connection
|
|
41
|
+
* for the model.
|
|
42
|
+
*/
|
|
30
43
|
connection?: Connection;
|
|
31
44
|
/**
|
|
45
|
+
* Flag to enable or disable the normalize plugin.
|
|
46
|
+
*
|
|
47
|
+
* This flag indicates whether the normalize plugin should be enabled.
|
|
48
|
+
* The default value is `true`.
|
|
49
|
+
*
|
|
32
50
|
* @default true
|
|
33
51
|
*/
|
|
34
52
|
enableNormalizePlugin?: boolean;
|
|
35
53
|
/**
|
|
54
|
+
* Configuration for Mongoose timestamps.
|
|
55
|
+
*
|
|
56
|
+
* This option allows enabling or disabling timestamps, or providing a
|
|
57
|
+
* custom timestamp configuration using `SchemaTimestampsConfig`.
|
|
58
|
+
* The default value is `true`.
|
|
59
|
+
*
|
|
36
60
|
* @default true
|
|
37
61
|
*/
|
|
38
62
|
timestamps?: boolean | SchemaTimestampsConfig;
|
package/types/schema.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="paginate
|
|
2
|
-
/// <reference path="paginate
|
|
1
|
+
/// <reference path="aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -27,11 +27,28 @@
|
|
|
27
27
|
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
28
28
|
import type Decimal from 'decimal.js';
|
|
29
29
|
import type { ObjectIdSchemaDefinition, SchemaTypeOptions, StringSchemaDefinition, Types } from 'mongoose';
|
|
30
|
+
/**
|
|
31
|
+
* Base attributes for Mongoose schemas.
|
|
32
|
+
*
|
|
33
|
+
* This type defines common attributes that can be applied to any schema field.
|
|
34
|
+
*/
|
|
30
35
|
export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
|
|
36
|
+
/**
|
|
37
|
+
* Additional attributes specific to string schema fields in Mongoose.
|
|
38
|
+
*
|
|
39
|
+
* This type extends the base schema attributes with string-specific attributes
|
|
40
|
+
* like `lowercase`, `short`, `trim`, and `uppercase`.
|
|
41
|
+
*/
|
|
31
42
|
export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
|
|
43
|
+
/**
|
|
44
|
+
* Interface for options to create common Mongoose schemas.
|
|
45
|
+
*
|
|
46
|
+
* This interface defines options for creating common Mongoose schemas, including
|
|
47
|
+
* settings for automatic rounding and fixed decimal places for Decimal128 fields.
|
|
48
|
+
*/
|
|
32
49
|
export interface CreateCommonMongooseSchemasOptions {
|
|
33
50
|
/**
|
|
34
|
-
*
|
|
51
|
+
* Configuration for automatic rounding and fixed decimal places for Decimal128 fields.
|
|
35
52
|
*/
|
|
36
53
|
autoRoundAndToFixedDecimal128?: {
|
|
37
54
|
/**
|
|
@@ -41,27 +58,100 @@ export interface CreateCommonMongooseSchemasOptions {
|
|
|
41
58
|
*/
|
|
42
59
|
places?: number;
|
|
43
60
|
/**
|
|
44
|
-
* Rounding
|
|
61
|
+
* Rounding mode to be used.
|
|
45
62
|
*
|
|
46
63
|
* @default Decimal.ROUND_DOWN
|
|
47
64
|
*/
|
|
48
65
|
rounding?: Decimal.Rounding;
|
|
49
66
|
};
|
|
50
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Interface for defining a Mongoose ObjectId reference schema field.
|
|
70
|
+
*
|
|
71
|
+
* This interface allows specifying attributes for an ObjectId reference field,
|
|
72
|
+
* including whether the field is private, the reference model name, and standard
|
|
73
|
+
* schema options like `required` and `unique`.
|
|
74
|
+
*
|
|
75
|
+
* @template R - The type of the reference model name.
|
|
76
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
77
|
+
*/
|
|
51
78
|
export interface MongooseObjectIdRefSchema<R extends string, T extends BaseSchemaAttribute[]> {
|
|
79
|
+
/**
|
|
80
|
+
* Indicates whether the field is private.
|
|
81
|
+
*
|
|
82
|
+
* If the 'private' attribute is included in `T`, this is `true`, otherwise it
|
|
83
|
+
* can be `boolean` or `undefined`. If using the normalize plugin, fields marked
|
|
84
|
+
* as private will be automatically excluded from the `toJSON` output.
|
|
85
|
+
*/
|
|
52
86
|
private: 'private' extends T[number] ? true : boolean | undefined;
|
|
87
|
+
/**
|
|
88
|
+
* The model that `populate()` should use if populating this path.
|
|
89
|
+
*/
|
|
53
90
|
ref: R;
|
|
91
|
+
/**
|
|
92
|
+
* If true, attach a required validator to this path, which ensures this path
|
|
93
|
+
* path cannot be set to a nullish value. If a function, Mongoose calls the
|
|
94
|
+
* function and only checks for nullish values if the function returns a truthy value.
|
|
95
|
+
*/
|
|
54
96
|
required: 'required' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['required'];
|
|
97
|
+
/**
|
|
98
|
+
* The schema type definition for ObjectId.
|
|
99
|
+
*/
|
|
55
100
|
type: ObjectIdSchemaDefinition;
|
|
101
|
+
/**
|
|
102
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
103
|
+
* will build a unique index on this path when the
|
|
104
|
+
* model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
|
|
105
|
+
*/
|
|
56
106
|
unique: 'unique' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['unique'];
|
|
57
107
|
}
|
|
108
|
+
/**
|
|
109
|
+
* Interface for defining a Mongoose string schema field.
|
|
110
|
+
*
|
|
111
|
+
* This interface allows specifying attributes for a string schema field, including
|
|
112
|
+
* whether the field should be lowercased, trimmed, or uppercased, as well as standard
|
|
113
|
+
* schema options like `required` and `unique`.
|
|
114
|
+
*
|
|
115
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
116
|
+
*/
|
|
58
117
|
export interface MongooseStringSchema<T extends MongooseStringSchemaAttribute[]> {
|
|
118
|
+
/** If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in `String#toLowerCase()`. */
|
|
59
119
|
lowercase: 'lowercase' extends T[number] ? boolean : SchemaTypeOptions<string>['lowercase'];
|
|
60
|
-
|
|
120
|
+
/**
|
|
121
|
+
* The maximum length of the string.
|
|
122
|
+
*
|
|
123
|
+
* If the 'short' attribute is included in `T`, this is set to 16. Otherwise, it
|
|
124
|
+
* takes the value from `SchemaTypeOptions<string>['maxlength']`.
|
|
125
|
+
*
|
|
126
|
+
* If set, Mongoose will add a custom validator that ensures the given string's `length` is at most the given number.
|
|
127
|
+
*/
|
|
128
|
+
maxlength: 'short' extends T[number] ? 16 : SchemaTypeOptions<string>['maxlength'];
|
|
129
|
+
/**
|
|
130
|
+
* Indicates whether the field is private.
|
|
131
|
+
*
|
|
132
|
+
* If the 'private' attribute is included in `T`, this is `true`, otherwise it
|
|
133
|
+
* can be `boolean` or `undefined`. If using the normalize plugin, fields marked
|
|
134
|
+
* as private will be automatically excluded from the `toJSON` output.
|
|
135
|
+
*/
|
|
61
136
|
private: 'private' extends T[number] ? true : boolean | undefined;
|
|
137
|
+
/**
|
|
138
|
+
* If true, attach a required validator to this path, which ensures this path
|
|
139
|
+
* path cannot be set to a nullish value. If a function, Mongoose calls the
|
|
140
|
+
* function and only checks for nullish values if the function returns a truthy value.
|
|
141
|
+
*/
|
|
62
142
|
required: 'required' extends T[number] ? true : SchemaTypeOptions<string>['required'];
|
|
143
|
+
/** If truthy, Mongoose will add a custom setter that removes leading and trailing whitespace using JavaScript's built-in `String#trim()`. */
|
|
63
144
|
trim: 'trim' extends T[number] ? true : SchemaTypeOptions<string>['trim'];
|
|
145
|
+
/**
|
|
146
|
+
* The schema type definition for string.
|
|
147
|
+
*/
|
|
64
148
|
type: StringSchemaDefinition;
|
|
149
|
+
/**
|
|
150
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
151
|
+
* will build a unique index on this path when the
|
|
152
|
+
* model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
|
|
153
|
+
*/
|
|
65
154
|
unique: 'unique' extends T[number] ? true : SchemaTypeOptions<string>['unique'];
|
|
155
|
+
/** If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in `String#toUpperCase()`. */
|
|
66
156
|
uppercase: 'uppercase' extends T[number] ? boolean : SchemaTypeOptions<string>['uppercase'];
|
|
67
157
|
}
|
package/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/// <reference path="types/paginate
|
|
2
|
-
/// <reference path="types/paginate
|
|
1
|
+
/// <reference path="types/aggregate-paginate.d.ts" />
|
|
2
|
+
/// <reference path="types/paginate.d.ts" />
|
|
3
3
|
/// <reference types="mongoose/types/aggregate" />
|
|
4
4
|
/// <reference types="mongoose/types/callback" />
|
|
5
5
|
/// <reference types="mongoose/types/collection" />
|
|
@@ -28,8 +28,46 @@
|
|
|
28
28
|
import mongoose from 'mongoose';
|
|
29
29
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
30
30
|
import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Builds and returns a Mongoose model with specified schema and options.
|
|
33
|
+
*
|
|
34
|
+
* This function sets up a Mongoose model with optional plugins and timestamp settings.
|
|
35
|
+
* It can optionally use a specified connection or the default connection.
|
|
36
|
+
*
|
|
37
|
+
* Determine the connection to use:
|
|
38
|
+
* 1. Use the connection provided in the options, if available.
|
|
39
|
+
* 2. Otherwise, use the default connection stored in `mongooseConnections`.
|
|
40
|
+
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
41
|
+
* or fallback to a local MongoDB instance.
|
|
42
|
+
*
|
|
43
|
+
* @template DocType - The type of the document.
|
|
44
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
45
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
46
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
47
|
+
*
|
|
48
|
+
* @param collection - The name of the collection.
|
|
49
|
+
* @param name - The name of the model.
|
|
50
|
+
* @param schema - The Mongoose schema to use for the model.
|
|
51
|
+
* @param options - Optional settings for building the model.
|
|
52
|
+
*
|
|
53
|
+
* @returns The created Mongoose model.
|
|
54
|
+
*/
|
|
55
|
+
export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
|
|
56
|
+
/**
|
|
57
|
+
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
58
|
+
*
|
|
59
|
+
* This function generates a set of commonly used schema definitions, such as boolean,
|
|
60
|
+
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
61
|
+
* options for Decimal128 fields.
|
|
62
|
+
*
|
|
63
|
+
* @template T - The type for custom schemas.
|
|
64
|
+
*
|
|
65
|
+
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
66
|
+
* @param options - Optional settings for creating the common schemas.
|
|
67
|
+
*
|
|
68
|
+
* @returns The merged common and custom schemas.
|
|
69
|
+
*/
|
|
70
|
+
export declare function createCommonMongooseSchemas<T extends {}>(customSchemas?: T, options?: CreateCommonMongooseSchemasOptions): {
|
|
33
71
|
readonly boolean: {
|
|
34
72
|
readonly defaultFalse: {
|
|
35
73
|
readonly nonRequired: {
|
|
@@ -190,7 +228,65 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
|
|
|
190
228
|
};
|
|
191
229
|
};
|
|
192
230
|
} & T;
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Creates a Mongoose schema definition for an ObjectId reference with specified attributes.
|
|
233
|
+
*
|
|
234
|
+
* This function generates a schema definition for an ObjectId reference field, including
|
|
235
|
+
* attributes such as 'private', 'required', and 'unique'.
|
|
236
|
+
*
|
|
237
|
+
* @template R - The type of the reference model name.
|
|
238
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
239
|
+
*
|
|
240
|
+
* @param refModelName - The name of the reference model.
|
|
241
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
242
|
+
*
|
|
243
|
+
* @returns The generated ObjectId reference schema definition.
|
|
244
|
+
*/
|
|
245
|
+
export declare function createMongooseObjectIdRefSchema<R extends string, T extends BaseSchemaAttribute[]>(refModelName: R, ...attributes: T): MongooseObjectIdRefSchema<R, T>;
|
|
246
|
+
/**
|
|
247
|
+
* Creates a Mongoose schema definition for a string field with specified attributes.
|
|
248
|
+
*
|
|
249
|
+
* This function generates a schema definition for a string field, including attributes
|
|
250
|
+
* such as 'private', 'required', 'short', 'trim', and 'unique'. If the 'short' attribute
|
|
251
|
+
* is included, the `maxlength` is set to 16.
|
|
252
|
+
*
|
|
253
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
254
|
+
*
|
|
255
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
256
|
+
*
|
|
257
|
+
* @returns The generated string schema definition.
|
|
258
|
+
*/
|
|
259
|
+
export declare function createMongooseStringSchema<T extends MongooseStringSchemaAttribute[]>(...attributes: T): MongooseStringSchema<T>;
|
|
260
|
+
/**
|
|
261
|
+
* Converts a document or ObjectId to a document using the specified model.
|
|
262
|
+
*
|
|
263
|
+
* This function takes a document or ObjectId and returns the corresponding document
|
|
264
|
+
* from the database using the specified model. Optionally, specific fields can be selected.
|
|
265
|
+
*
|
|
266
|
+
* @template D - The type of the hydrated document.
|
|
267
|
+
* @template DocType - The type of the document.
|
|
268
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
269
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
270
|
+
*
|
|
271
|
+
* @param documentOrObjectId - The document or ObjectId to convert.
|
|
272
|
+
* @param model - The Mongoose model to use for the conversion.
|
|
273
|
+
* @param selectFields - Optional fields to select from the document.
|
|
274
|
+
*
|
|
275
|
+
* @returns The corresponding document or null if not found.
|
|
276
|
+
*/
|
|
277
|
+
export declare function mongooseDocumentOrObjectIdToDocument<D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>, DocType, InstanceMethodsAndOverrides, QueryHelpers>(documentOrObjectId: MongooseDocumentOrObjectId<D>, model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, selectFields?: string[]): Promise<D | null>;
|
|
278
|
+
/**
|
|
279
|
+
* Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
|
|
280
|
+
*
|
|
281
|
+
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
282
|
+
* converting the values to strings for easier use.
|
|
283
|
+
*
|
|
284
|
+
* @template DocType - The type of the document.
|
|
285
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
286
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
287
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
288
|
+
*
|
|
289
|
+
* @param schema - The Mongoose schema to modify.
|
|
290
|
+
* @param fields - The Decimal128 fields to add the getter to.
|
|
291
|
+
*/
|
|
292
|
+
export declare function setupDecimal128FieldsToStringGetter<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, ...fields: string[]): void;
|
package/utils.mjs
CHANGED
|
@@ -6,15 +6,53 @@ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
|
6
6
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
7
7
|
import { mongooseConnections } from "./constants.mjs";
|
|
8
8
|
import mongooseNormalizePlugin from "./plugins/normalize.mjs";
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Builds and returns a Mongoose model with specified schema and options.
|
|
11
|
+
*
|
|
12
|
+
* This function sets up a Mongoose model with optional plugins and timestamp settings.
|
|
13
|
+
* It can optionally use a specified connection or the default connection.
|
|
14
|
+
*
|
|
15
|
+
* Determine the connection to use:
|
|
16
|
+
* 1. Use the connection provided in the options, if available.
|
|
17
|
+
* 2. Otherwise, use the default connection stored in `mongooseConnections`.
|
|
18
|
+
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
19
|
+
* or fallback to a local MongoDB instance.
|
|
20
|
+
*
|
|
21
|
+
* @template DocType - The type of the document.
|
|
22
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
23
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
24
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
25
|
+
*
|
|
26
|
+
* @param collection - The name of the collection.
|
|
27
|
+
* @param name - The name of the model.
|
|
28
|
+
* @param schema - The Mongoose schema to use for the model.
|
|
29
|
+
* @param options - Optional settings for building the model.
|
|
30
|
+
*
|
|
31
|
+
* @returns The created Mongoose model.
|
|
32
|
+
*/
|
|
33
|
+
export function buildMongooseModel(collection, name, schema, options) {
|
|
10
34
|
if (options?.enableNormalizePlugin !== false)
|
|
11
35
|
schema.plugin(mongooseNormalizePlugin);
|
|
12
36
|
schema.plugin(mongooseAggregatePaginate);
|
|
13
37
|
schema.plugin(mongoosePaginate);
|
|
14
38
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
15
39
|
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
|
|
16
|
-
}
|
|
17
|
-
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
43
|
+
*
|
|
44
|
+
* This function generates a set of commonly used schema definitions, such as boolean,
|
|
45
|
+
* number, objectId, and string schemas, with optional custom configurations and rounding
|
|
46
|
+
* options for Decimal128 fields.
|
|
47
|
+
*
|
|
48
|
+
* @template T - The type for custom schemas.
|
|
49
|
+
*
|
|
50
|
+
* @param customSchemas - Optional custom schemas to merge with the common schemas.
|
|
51
|
+
* @param options - Optional settings for creating the common schemas.
|
|
52
|
+
*
|
|
53
|
+
* @returns The merged common and custom schemas.
|
|
54
|
+
*/
|
|
55
|
+
export function createCommonMongooseSchemas(customSchemas, options) {
|
|
18
56
|
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
19
57
|
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
20
58
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
@@ -126,13 +164,40 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
126
164
|
}
|
|
127
165
|
}
|
|
128
166
|
}, customSchemas);
|
|
129
|
-
}
|
|
130
|
-
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Creates a Mongoose schema definition for an ObjectId reference with specified attributes.
|
|
170
|
+
*
|
|
171
|
+
* This function generates a schema definition for an ObjectId reference field, including
|
|
172
|
+
* attributes such as 'private', 'required', and 'unique'.
|
|
173
|
+
*
|
|
174
|
+
* @template R - The type of the reference model name.
|
|
175
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
176
|
+
*
|
|
177
|
+
* @param refModelName - The name of the reference model.
|
|
178
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
179
|
+
*
|
|
180
|
+
* @returns The generated ObjectId reference schema definition.
|
|
181
|
+
*/
|
|
182
|
+
export function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
|
|
131
183
|
const schema = { ref: refModelName, type: mongoose.Schema.Types.ObjectId };
|
|
132
184
|
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
133
185
|
return schema;
|
|
134
|
-
}
|
|
135
|
-
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Creates a Mongoose schema definition for a string field with specified attributes.
|
|
189
|
+
*
|
|
190
|
+
* This function generates a schema definition for a string field, including attributes
|
|
191
|
+
* such as 'private', 'required', 'short', 'trim', and 'unique'. If the 'short' attribute
|
|
192
|
+
* is included, the `maxlength` is set to 16.
|
|
193
|
+
*
|
|
194
|
+
* @template T - An array of attributes to apply to the schema field.
|
|
195
|
+
*
|
|
196
|
+
* @param attributes - The attributes to apply to the schema field.
|
|
197
|
+
*
|
|
198
|
+
* @returns The generated string schema definition.
|
|
199
|
+
*/
|
|
200
|
+
export function createMongooseStringSchema(...attributes) {
|
|
136
201
|
const schema = { type: String };
|
|
137
202
|
new Set(attributes).forEach((attribute) => {
|
|
138
203
|
if (attribute === 'short')
|
|
@@ -140,10 +205,43 @@ export const createMongooseStringSchema = (...attributes) => {
|
|
|
140
205
|
schema[attribute] = true;
|
|
141
206
|
});
|
|
142
207
|
return schema;
|
|
143
|
-
}
|
|
144
|
-
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Converts a document or ObjectId to a document using the specified model.
|
|
211
|
+
*
|
|
212
|
+
* This function takes a document or ObjectId and returns the corresponding document
|
|
213
|
+
* from the database using the specified model. Optionally, specific fields can be selected.
|
|
214
|
+
*
|
|
215
|
+
* @template D - The type of the hydrated document.
|
|
216
|
+
* @template DocType - The type of the document.
|
|
217
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
218
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
219
|
+
*
|
|
220
|
+
* @param documentOrObjectId - The document or ObjectId to convert.
|
|
221
|
+
* @param model - The Mongoose model to use for the conversion.
|
|
222
|
+
* @param selectFields - Optional fields to select from the document.
|
|
223
|
+
*
|
|
224
|
+
* @returns The corresponding document or null if not found.
|
|
225
|
+
*/
|
|
226
|
+
export async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, selectFields) {
|
|
145
227
|
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
|
|
146
228
|
return (await model.findById(documentOrObjectId).select(selectFields || []));
|
|
147
229
|
return documentOrObjectId;
|
|
148
|
-
}
|
|
149
|
-
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
|
|
233
|
+
*
|
|
234
|
+
* This function adds a getter to the specified Decimal128 fields in the schema,
|
|
235
|
+
* converting the values to strings for easier use.
|
|
236
|
+
*
|
|
237
|
+
* @template DocType - The type of the document.
|
|
238
|
+
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
239
|
+
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
240
|
+
* @template QueryHelpers - The type for additional query helpers.
|
|
241
|
+
*
|
|
242
|
+
* @param schema - The Mongoose schema to modify.
|
|
243
|
+
* @param fields - The Decimal128 fields to add the getter to.
|
|
244
|
+
*/
|
|
245
|
+
export function setupDecimal128FieldsToStringGetter(schema, ...fields) {
|
|
246
|
+
fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|
|
247
|
+
}
|
|
File without changes
|
|
File without changes
|