@kikiutils/mongoose 0.5.0 → 1.0.0-rc.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 kiki-kanri
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,49 @@
1
1
  # @kikiutils/mongoose
2
2
 
3
+ [![npm version][npm-version-src]][npm-version-href]
4
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
+ [![License][license-src]][license-href]
6
+
3
7
  For mongoose v8 types and utils.
4
8
 
9
+ - [✨ Release Notes](/CHANGELOG.md)
10
+
11
+ ## Features
12
+
13
+ - Enhanced `toJSON` method with normalization functions, auto-converts `_id` to `id`, and more.
14
+ - Commonly used schema settings for fast schema creation.
15
+ - Automatic timestamp field settings.
16
+ - Pre-registered `mongoose-paginate-v2` and `mongoose-aggregate-paginate-v2` plugins.
17
+ - Automatic Decimal128 field conversion and processing.
18
+ - TypeScript support.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ # Using pnpm
24
+ pnpm add @kikiutils/mongoose
25
+
26
+ # Using yarn
27
+ yarn add @kikiutils/mongoose
28
+
29
+ # Using npm
30
+ npm install @kikiutils/mongoose
31
+ ```
32
+
33
+ ## Environmental Requirements
34
+
35
+ - ESM only
36
+ - Mongoose v8
37
+ - NodeJS 18 or higher
38
+
5
39
  **Detailed documentation will be added in future releases.**
40
+
41
+ <!-- Badges -->
42
+ [npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
43
+ [npm-version-href]: https://npmjs.com/package/@kikiutils/mongoose
44
+
45
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
46
+ [npm-downloads-href]: https://npmjs.com/package/@kikiutils/mongoose
47
+
48
+ [license-src]: https://img.shields.io/npm/l/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
49
+ [license-href]: https://npmjs.com/package/@kikiutils/mongoose
package/constants.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /// <reference path="types/paginate/aggregate.d.ts" />
2
+ /// <reference path="types/paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,7 +24,7 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
25
- /// <reference types="mongoose-paginate-v2" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
26
28
  import type { Connection } from 'mongoose';
27
29
  export declare const commonMongooseSchemas: {
28
30
  readonly boolean: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "0.5.0",
3
+ "version": "1.0.0-rc.0",
4
4
  "license": "MIT",
5
5
  "author": "kiki-kanri",
6
6
  "repository": {
@@ -15,14 +15,15 @@
15
15
  "dependencies": {
16
16
  "decimal.js": "^10.4.3",
17
17
  "lodash-es": "^4.17.21",
18
- "mongoose": "^8.3.2",
19
- "mongoose-paginate-v2": "^1.8.0"
18
+ "mongoose": "^8.4.0",
19
+ "mongoose-aggregate-paginate-v2": "^1.0.7",
20
+ "mongoose-paginate-v2": "^1.8.1"
20
21
  },
21
22
  "devDependencies": {
22
23
  "@kikiutils/prettierrc": "^1.3.1",
23
24
  "@kikiutils/tsconfigs": "^2.2.0",
24
25
  "@types/lodash-es": "^4.17.12",
25
- "@types/node": "^20.12.7",
26
+ "@types/node": "^20.12.12",
26
27
  "changelogen": "^0.5.5",
27
28
  "tsc-multi": "^1.1.0",
28
29
  "typescript": "^5.4.5"
@@ -30,6 +31,7 @@
30
31
  "keywords": [
31
32
  "mongooose",
32
33
  "mongooose-plugins",
34
+ "mongooose-types",
33
35
  "mongooose-utils"
34
36
  ],
35
37
  "engines": {
@@ -1,3 +1,5 @@
1
+ /// <reference path="../types/paginate/aggregate.d.ts" />
2
+ /// <reference path="../types/paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,7 +24,7 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
25
- /// <reference types="mongoose-paginate-v2" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
26
28
  import type { Schema } from 'mongoose';
27
29
  export declare const mongooseNormalizePlugin: <S extends Schema<any, import("mongoose").Model<any, any, any, any, any, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, {
28
30
  [x: string]: unknown;
package/types/data.d.ts CHANGED
@@ -9,8 +9,61 @@ type OmitNever<T> = {
9
9
  [P in keyof T as T[P] extends never ? never : P]: T[P];
10
10
  };
11
11
  declare global {
12
+ /**
13
+ * Global type for base mongoose model data.
14
+ *
15
+ * @example
16
+ * interface UserData extends BaseMongooseModelData {
17
+ * account: string;
18
+ * name: string;
19
+ * }
20
+ *
21
+ * // Resulting type:
22
+ * // {
23
+ * // account: string;
24
+ * // createdAt: string;
25
+ * // id: string;
26
+ * // name: string;
27
+ * // updatedAt: string;
28
+ * // }
29
+ */
12
30
  type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<_BaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
31
+ /**
32
+ * Global type to omit timestamp fields and optionally other fields from a given type.
33
+ *
34
+ * @example
35
+ * interface UserData extends BaseMongooseModelData {
36
+ * account: string;
37
+ * name: string;
38
+ * }
39
+ *
40
+ * type UserDataWithoutTimestamps = OmitMongooseTimestampAndOtherFields<UserData>;
41
+ *
42
+ * // Resulting type:
43
+ * // {
44
+ * // account: string;
45
+ * // id: string;
46
+ * // name: string;
47
+ * // }
48
+ */
13
49
  type OmitMongooseTimestampAndOtherFields<T, OT extends number | symbol | string = ''> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
50
+ /**
51
+ * Global type for handling optional timestamp fields.
52
+ *
53
+ * @example
54
+ * interface UserLogData extends WithMongooseTimestampFieldsData<true, false> {
55
+ * content: string;
56
+ * type: number;
57
+ * }
58
+ *
59
+ * // Resulting type:
60
+ * // {
61
+ * // content: string;
62
+ * // createdAt: string;
63
+ * // type: number;
64
+ * // }
65
+ * }
66
+ */
14
67
  type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<_WithTimestampFieldsData<CreatedAt, UpdatedAt>>;
15
68
  }
16
69
  export {};
package/types/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /// <reference path="paginate/aggregate.d.ts" />
2
+ /// <reference path="paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,8 +24,8 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
25
28
  import type { HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
26
- export {} from 'mongoose-paginate-v2';
27
29
  export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
28
30
  }
29
31
  declare global {
package/types/index.mjs CHANGED
@@ -1 +1 @@
1
- export {} from 'mongoose-paginate-v2';
1
+ export {};
@@ -1,3 +1,5 @@
1
+ /// <reference path="paginate/aggregate.d.ts" />
2
+ /// <reference path="paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,7 +24,7 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
25
- /// <reference types="mongoose-paginate-v2" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
26
28
  import type { Connection, SchemaTimestampsConfig } from 'mongoose';
27
29
  export interface BuildMongooseModelOptions {
28
30
  connection?: Connection;
@@ -0,0 +1,91 @@
1
+ /// <reference path="index.d.ts" />
2
+ /// <reference types="mongoose/types/aggregate" />
3
+ /// <reference types="mongoose/types/callback" />
4
+ /// <reference types="mongoose/types/collection" />
5
+ /// <reference types="mongoose/types/connection" />
6
+ /// <reference types="mongoose/types/cursor" />
7
+ /// <reference types="mongoose/types/document" />
8
+ /// <reference types="mongoose/types/error" />
9
+ /// <reference types="mongoose/types/expressions" />
10
+ /// <reference types="mongoose/types/helpers" />
11
+ /// <reference types="mongoose/types/middlewares" />
12
+ /// <reference types="mongoose/types/indexes" />
13
+ /// <reference types="mongoose/types/models" />
14
+ /// <reference types="mongoose/types/mongooseoptions" />
15
+ /// <reference types="mongoose/types/pipelinestage" />
16
+ /// <reference types="mongoose/types/populate" />
17
+ /// <reference types="mongoose/types/query" />
18
+ /// <reference types="mongoose/types/schemaoptions" />
19
+ /// <reference types="mongoose/types/schematypes" />
20
+ /// <reference types="mongoose/types/session" />
21
+ /// <reference types="mongoose/types/types" />
22
+ /// <reference types="mongoose/types/utility" />
23
+ /// <reference types="mongoose/types/validation" />
24
+ /// <reference types="mongoose/types/virtuals" />
25
+ /// <reference types="mongoose/types/inferschematype" />
26
+ /// <reference types="mongoose/types/inferrawdoctype" />
27
+ import type { AggregatePaginateOptions, Schema } from 'mongoose';
28
+ declare module 'mongoose' {
29
+ interface AggregateCustomLabels<T = string | undefined | boolean> {
30
+ totalDocs?: T;
31
+ docs?: T;
32
+ limit?: T;
33
+ page?: T;
34
+ nextPage?: T;
35
+ prevPage?: T;
36
+ hasNextPage?: T;
37
+ hasPrevPage?: T;
38
+ totalPages?: T;
39
+ pagingCounter?: T;
40
+ meta?: T;
41
+ }
42
+ interface AggregatePaginateOptions {
43
+ sort?: object | string;
44
+ offset?: number;
45
+ page?: number;
46
+ limit?: number;
47
+ customLabels?: AggregateCustomLabels;
48
+ pagination?: boolean;
49
+ allowDiskUse?: boolean;
50
+ countQuery?: object;
51
+ useFacet?: boolean;
52
+ }
53
+ interface AggregatePaginateQueryPopulateOptions {
54
+ /** space delimited path(s) to populate */
55
+ path: string;
56
+ /** optional fields to select */
57
+ select?: any;
58
+ /** optional query conditions to match */
59
+ match?: any;
60
+ /** optional model to use for population */
61
+ model?: string | Model<any> | undefined;
62
+ /** optional query options like sort, limit, etc */
63
+ options?: any;
64
+ /** deep populate */
65
+ populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[] | undefined;
66
+ }
67
+ interface AggregatePaginateResult<T> {
68
+ docs: T[];
69
+ totalDocs: number;
70
+ limit: number;
71
+ page?: number;
72
+ totalPages: number;
73
+ nextPage?: number | null;
74
+ prevPage?: number | null;
75
+ pagingCounter: number;
76
+ hasPrevPage: boolean;
77
+ hasNextPage: boolean;
78
+ meta?: any;
79
+ [customLabel: string]: T[] | number | boolean | null | undefined;
80
+ }
81
+ interface AggregatePaginateModel<D> extends Model<D> {
82
+ aggregatePaginate<T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void): Promise<AggregatePaginateResult<T>>;
83
+ }
84
+ }
85
+ declare function mongooseAggregatePaginate(schema: Schema): void;
86
+ declare namespace mongooseAggregatePaginate {
87
+ const aggregatePaginate: {
88
+ options: AggregatePaginateOptions;
89
+ };
90
+ }
91
+ export default mongooseAggregatePaginate;
@@ -0,0 +1,6 @@
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
+ export default mongooseAggregatePaginate;
@@ -0,0 +1,123 @@
1
+ /// <reference path="aggregate.d.ts" />
2
+ /// <reference types="mongoose/types/aggregate" />
3
+ /// <reference types="mongoose/types/callback" />
4
+ /// <reference types="mongoose/types/collection" />
5
+ /// <reference types="mongoose/types/connection" />
6
+ /// <reference types="mongoose/types/cursor" />
7
+ /// <reference types="mongoose/types/document" />
8
+ /// <reference types="mongoose/types/error" />
9
+ /// <reference types="mongoose/types/expressions" />
10
+ /// <reference types="mongoose/types/helpers" />
11
+ /// <reference types="mongoose/types/middlewares" />
12
+ /// <reference types="mongoose/types/indexes" />
13
+ /// <reference types="mongoose/types/models" />
14
+ /// <reference types="mongoose/types/mongooseoptions" />
15
+ /// <reference types="mongoose/types/pipelinestage" />
16
+ /// <reference types="mongoose/types/populate" />
17
+ /// <reference types="mongoose/types/query" />
18
+ /// <reference types="mongoose/types/schemaoptions" />
19
+ /// <reference types="mongoose/types/schematypes" />
20
+ /// <reference types="mongoose/types/session" />
21
+ /// <reference types="mongoose/types/types" />
22
+ /// <reference types="mongoose/types/utility" />
23
+ /// <reference types="mongoose/types/validation" />
24
+ /// <reference types="mongoose/types/virtuals" />
25
+ /// <reference types="mongoose/types/inferschematype" />
26
+ /// <reference types="mongoose/types/inferrawdoctype" />
27
+ import type { FilterQuery, mongo, PaginateOptions, Schema } from 'mongoose';
28
+ declare module 'mongoose' {
29
+ interface PaginateCustomLabels<T = string | undefined | boolean> {
30
+ totalDocs?: T;
31
+ docs?: T;
32
+ limit?: T;
33
+ page?: T;
34
+ nextPage?: T;
35
+ prevPage?: T;
36
+ hasNextPage?: T;
37
+ hasPrevPage?: T;
38
+ totalPages?: T;
39
+ pagingCounter?: T;
40
+ meta?: T;
41
+ }
42
+ interface PaginateReadOptions {
43
+ pref: string;
44
+ tags?: any[];
45
+ }
46
+ interface PaginateOptions {
47
+ select?: object | string;
48
+ collation?: mongo.CollationOptions;
49
+ sort?: object | string;
50
+ populate?: PopulateOptions[] | string[] | PopulateOptions | string | PopulateOptions;
51
+ projection?: any;
52
+ lean?: boolean;
53
+ leanWithId?: boolean;
54
+ offset?: number;
55
+ page?: number;
56
+ limit?: number;
57
+ customLabels?: PaginateCustomLabels;
58
+ pagination?: boolean;
59
+ useEstimatedCount?: boolean;
60
+ useCustomCountFn?: () => Promise<number>;
61
+ forceCountFn?: boolean;
62
+ allowDiskUse?: boolean;
63
+ read?: PaginateReadOptions;
64
+ options?: QueryOptions;
65
+ }
66
+ interface SubPaginateOptions {
67
+ select?: object | string;
68
+ populate?: PopulateOptions[] | string[] | PopulateOptions | string | PopulateOptions;
69
+ pagination?: boolean;
70
+ read?: PaginateReadOptions;
71
+ pagingOptions: SubDocumentPagingOptions | undefined;
72
+ }
73
+ interface SubDocumentPagingOptions {
74
+ populate?: PopulateOptions[] | string[] | PopulateOptions | string | PopulateOptions;
75
+ page?: number;
76
+ limit?: number;
77
+ }
78
+ interface PaginateResult<T> {
79
+ docs: T[];
80
+ totalDocs: number;
81
+ limit: number;
82
+ hasPrevPage: boolean;
83
+ hasNextPage: boolean;
84
+ page?: number;
85
+ totalPages: number;
86
+ offset: number;
87
+ prevPage?: number | null;
88
+ nextPage?: number | null;
89
+ pagingCounter: number;
90
+ meta?: any;
91
+ [customLabel: string]: T[] | number | boolean | null | undefined;
92
+ }
93
+ type PaginateDocument<T, TMethods, TVirtuals, O extends PaginateOptions = {}> = O['lean'] extends true ? (O['leanWithId'] extends true ? T & {
94
+ id: string;
95
+ } : T) : HydratedDocument<T, TMethods, TVirtuals>;
96
+ interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}> extends Model<T, TQueryHelpers, TMethods> {
97
+ paginate<O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, O>>>;
98
+ }
99
+ interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}> extends Model<T, TQueryHelpers, TMethods> {
100
+ paginate<UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, O>>>;
101
+ }
102
+ interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}> extends Model<T, TQueryHelpers, TMethods> {
103
+ paginate<UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, PaginateOptions>>>;
104
+ }
105
+ }
106
+ declare function _(schema: Schema): void;
107
+ declare namespace _ {
108
+ const paginate: {
109
+ options: PaginateOptions;
110
+ };
111
+ const paginateSubDocs: {
112
+ options: PaginateOptions;
113
+ };
114
+ class PaginationParameters<T, O extends PaginateOptions> {
115
+ constructor(request: {
116
+ query?: Record<string, any>;
117
+ });
118
+ get: () => [FilterQuery<T>, O];
119
+ getOptions: () => O;
120
+ getQuery: () => FilterQuery<T>;
121
+ }
122
+ }
123
+ export default _;
@@ -0,0 +1 @@
1
+ export default _;
package/types/schema.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /// <reference path="paginate/aggregate.d.ts" />
2
+ /// <reference path="paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,18 +24,25 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
25
- /// <reference types="mongoose-paginate-v2" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
26
28
  import type Decimal from 'decimal.js';
27
29
  import type { ObjectIdSchemaDefinition, SchemaTypeOptions, StringSchemaDefinition, Types } from 'mongoose';
28
30
  export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
29
31
  export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
30
32
  export interface CreateCommonMongooseSchemasOptions {
33
+ /**
34
+ * Options for auto-rounding and fixing decimal places for Decimal128 fields.
35
+ */
31
36
  autoRoundAndToFixedDecimal128?: {
32
37
  /**
38
+ * Number of decimal places to round to.
39
+ *
33
40
  * @default 2
34
41
  */
35
42
  places?: number;
36
43
  /**
44
+ * Rounding strategy to use.
45
+ *
37
46
  * @default Decimal.ROUND_DOWN
38
47
  */
39
48
  rounding?: Decimal.Rounding;
package/utils.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ /// <reference path="types/paginate/aggregate.d.ts" />
2
+ /// <reference path="types/paginate/index.d.ts" />
1
3
  /// <reference types="mongoose/types/aggregate" />
2
4
  /// <reference types="mongoose/types/callback" />
3
5
  /// <reference types="mongoose/types/collection" />
@@ -22,7 +24,7 @@
22
24
  /// <reference types="mongoose/types/validation" />
23
25
  /// <reference types="mongoose/types/virtuals" />
24
26
  /// <reference types="mongoose/types/inferschematype" />
25
- /// <reference types="mongoose-paginate-v2" />
27
+ /// <reference types="mongoose/types/inferrawdoctype" />
26
28
  import mongoose from 'mongoose';
27
29
  import type { BuildMongooseModelOptions } from './types/options';
28
30
  import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
@@ -190,4 +192,5 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
190
192
  } & T;
191
193
  export declare const createMongooseObjectIdRefSchema: <R extends string, T extends BaseSchemaAttribute[]>(refModelName: R, ...attributes: T) => MongooseObjectIdRefSchema<R, T>;
192
194
  export declare const createMongooseStringSchema: <T extends MongooseStringSchemaAttribute[]>(...attributes: T) => MongooseStringSchema<T>;
195
+ export declare const mongooseDocumentOrObjectIdToDocument: <D extends mongoose.IfAny<DocType, any, InstanceMethodsAndOverrides extends Record<string, never> ? mongoose.Document<unknown, QueryHelpers, DocType> & mongoose.Require_id<DocType> : mongoose.IfAny<InstanceMethodsAndOverrides, mongoose.Document<unknown, QueryHelpers, DocType> & mongoose.Require_id<DocType>, mongoose.Document<unknown, QueryHelpers, DocType> & Omit<mongoose.Require_id<DocType>, keyof InstanceMethodsAndOverrides> & InstanceMethodsAndOverrides>>, DocType, InstanceMethodsAndOverrides, QueryHelpers>(documentOrObjectId: MongooseDocumentOrObjectId<D>, model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, selectFields?: string[]) => Promise<D | null>;
193
196
  export declare const setupDecimal128FieldsToStringGetter: <DocType, Model extends mongoose.PaginateModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, ...fields: string[]) => void;
package/utils.mjs CHANGED
@@ -1,12 +1,15 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { merge } from 'lodash-es';
3
3
  import mongoose from 'mongoose';
4
+ import { Types } from 'mongoose';
5
+ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
4
6
  import mongoosePaginate from 'mongoose-paginate-v2';
5
7
  import { mongooseConnections } from "./constants.mjs";
6
8
  import mongooseNormalizePlugin from "./plugins/normalize.mjs";
7
9
  export const buildMongooseModel = (collection, name, schema, options) => {
8
10
  if (options?.enableNormalizePlugin !== false)
9
11
  schema.plugin(mongooseNormalizePlugin);
12
+ schema.plugin(mongooseAggregatePaginate);
10
13
  schema.plugin(mongoosePaginate);
11
14
  schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
12
15
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
@@ -138,4 +141,9 @@ export const createMongooseStringSchema = (...attributes) => {
138
141
  });
139
142
  return schema;
140
143
  };
144
+ export const mongooseDocumentOrObjectIdToDocument = async (documentOrObjectId, model, selectFields) => {
145
+ if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
146
+ return (await model.findById(documentOrObjectId).select(selectFields || []));
147
+ return documentOrObjectId;
148
+ };
141
149
  export const setupDecimal128FieldsToStringGetter = (schema, ...fields) => fields.forEach((field) => schema.path(field).get((value) => value?.toString()));