@kikiutils/mongoose 9.0.5 → 10.0.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 +13 -25
- package/dist/_internals.d.ts +10 -1
- package/dist/_internals.d.ts.map +1 -1
- package/dist/_internals.js +20 -1
- package/dist/_internals.js.map +1 -1
- package/dist/assertions.d.ts +5 -2
- package/dist/assertions.d.ts.map +1 -1
- package/dist/assertions.js +10 -4
- package/dist/assertions.js.map +1 -1
- package/dist/builders.d.ts +9 -4
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +9 -9
- package/dist/builders.js.map +1 -1
- package/dist/connection.d.ts +3 -0
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +7 -1
- package/dist/connection.js.map +1 -1
- package/dist/documents.d.ts +8 -0
- package/dist/documents.d.ts.map +1 -0
- package/dist/documents.js +10 -0
- package/dist/documents.js.map +1 -0
- package/dist/errors/duplicate-key.d.ts +9 -0
- package/dist/errors/duplicate-key.d.ts.map +1 -0
- package/dist/errors/duplicate-key.js +15 -0
- package/dist/errors/duplicate-key.js.map +1 -0
- package/dist/errors/mongoose-update-conflict-error.d.ts +4 -0
- package/dist/errors/mongoose-update-conflict-error.d.ts.map +1 -0
- package/dist/errors/mongoose-update-conflict-error.js +7 -0
- package/dist/errors/mongoose-update-conflict-error.js.map +1 -0
- package/dist/options.d.ts +15 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +19 -2
- package/dist/options.js.map +1 -1
- package/dist/plugins/assertions.d.ts +30 -9
- package/dist/plugins/assertions.d.ts.map +1 -1
- package/dist/plugins/assertions.js +32 -4
- package/dist/plugins/assertions.js.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.d.ts +3 -15
- package/dist/plugins/lean-decimal128-to-string.d.ts.map +1 -1
- package/dist/plugins/lean-decimal128-to-string.js +30 -23
- package/dist/plugins/lean-decimal128-to-string.js.map +1 -1
- package/dist/plugins/normalize.d.ts +3 -40
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.js +98 -44
- package/dist/plugins/normalize.js.map +1 -1
- package/dist/schema-builders/_internals/array.d.ts +9 -0
- package/dist/schema-builders/_internals/array.d.ts.map +1 -0
- package/dist/schema-builders/_internals/array.js +12 -0
- package/dist/schema-builders/_internals/array.js.map +1 -0
- package/dist/schema-builders/_internals/boolean.d.ts +8 -0
- package/dist/schema-builders/_internals/boolean.d.ts.map +1 -0
- package/dist/schema-builders/_internals/boolean.js +9 -0
- package/dist/schema-builders/_internals/boolean.js.map +1 -0
- package/dist/schema-builders/_internals/date.d.ts +8 -0
- package/dist/schema-builders/_internals/date.d.ts.map +1 -0
- package/dist/schema-builders/_internals/date.js +9 -0
- package/dist/schema-builders/_internals/date.js.map +1 -0
- package/dist/schema-builders/_internals/decimal128.d.ts +36 -0
- package/dist/schema-builders/_internals/decimal128.d.ts.map +1 -0
- package/dist/schema-builders/_internals/decimal128.js +38 -0
- package/dist/schema-builders/_internals/decimal128.js.map +1 -0
- package/dist/schema-builders/_internals/int32.d.ts +21 -0
- package/dist/schema-builders/_internals/int32.d.ts.map +1 -0
- package/dist/schema-builders/_internals/int32.js +29 -0
- package/dist/schema-builders/_internals/int32.js.map +1 -0
- package/dist/schema-builders/_internals/mixed.d.ts +9 -0
- package/dist/schema-builders/_internals/mixed.d.ts.map +1 -0
- package/dist/schema-builders/_internals/mixed.js +10 -0
- package/dist/schema-builders/_internals/mixed.js.map +1 -0
- package/dist/schema-builders/_internals/number.d.ts +21 -0
- package/dist/schema-builders/_internals/number.d.ts.map +1 -0
- package/dist/schema-builders/_internals/number.js +20 -0
- package/dist/schema-builders/_internals/number.js.map +1 -0
- package/dist/schema-builders/_internals/object-id.d.ts +9 -0
- package/dist/schema-builders/_internals/object-id.d.ts.map +1 -0
- package/dist/schema-builders/_internals/object-id.js +10 -0
- package/dist/schema-builders/_internals/object-id.js.map +1 -0
- package/dist/schema-builders/_internals/ref.d.ts +8 -0
- package/dist/schema-builders/_internals/ref.d.ts.map +1 -0
- package/dist/schema-builders/_internals/ref.js +13 -0
- package/dist/schema-builders/_internals/ref.js.map +1 -0
- package/dist/schema-builders/_internals/shared.d.ts +104 -0
- package/dist/schema-builders/_internals/shared.d.ts.map +1 -0
- package/dist/schema-builders/_internals/shared.js +128 -0
- package/dist/schema-builders/_internals/shared.js.map +1 -0
- package/dist/schema-builders/_internals/string.d.ts +37 -0
- package/dist/schema-builders/_internals/string.d.ts.map +1 -0
- package/dist/schema-builders/_internals/string.js +39 -0
- package/dist/schema-builders/_internals/string.js.map +1 -0
- package/dist/schema-builders/index.d.ts +12 -13
- package/dist/schema-builders/index.js +11 -13
- package/dist/transaction.d.ts +10 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +9 -0
- package/dist/transaction.js.map +1 -0
- package/dist/types/data.d.ts +3 -5
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +15 -11
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/paginate.d.ts +4 -19
- package/dist/types/paginate.d.ts.map +1 -1
- package/package.json +17 -19
- package/src/_internals.ts +31 -1
- package/src/assertions.ts +20 -11
- package/src/builders.ts +26 -18
- package/src/connection.ts +8 -2
- package/src/documents.ts +12 -0
- package/src/errors/duplicate-key.ts +14 -0
- package/src/errors/mongoose-update-conflict-error.ts +3 -0
- package/src/options.ts +31 -1
- package/src/plugins/assertions.ts +117 -49
- package/src/plugins/lean-decimal128-to-string.ts +47 -41
- package/src/plugins/normalize.ts +169 -99
- package/src/schema-builders/_internals/array.ts +15 -0
- package/src/schema-builders/_internals/boolean.ts +10 -0
- package/src/schema-builders/_internals/date.ts +11 -0
- package/src/schema-builders/_internals/decimal128.ts +79 -0
- package/src/schema-builders/_internals/int32.ts +58 -0
- package/src/schema-builders/_internals/mixed.ts +12 -0
- package/src/schema-builders/_internals/number.ts +71 -0
- package/src/schema-builders/_internals/object-id.ts +13 -0
- package/src/schema-builders/_internals/ref.ts +18 -0
- package/src/schema-builders/_internals/shared.ts +302 -0
- package/src/schema-builders/_internals/string.ts +113 -0
- package/src/schema-builders/index.ts +26 -35
- package/src/transaction.ts +22 -0
- package/src/types/data.ts +5 -10
- package/src/types/index.ts +32 -22
- package/src/types/paginate.ts +9 -24
- package/dist/schema-builders/_internals.d.ts +0 -18
- package/dist/schema-builders/_internals.d.ts.map +0 -1
- package/dist/schema-builders/_internals.js +0 -36
- package/dist/schema-builders/_internals.js.map +0 -1
- package/dist/schema-builders/base.d.ts +0 -5
- package/dist/schema-builders/base.d.ts.map +0 -1
- package/dist/schema-builders/base.js +0 -33
- package/dist/schema-builders/base.js.map +0 -1
- package/dist/schema-builders/bigint.d.ts +0 -39
- package/dist/schema-builders/bigint.d.ts.map +0 -1
- package/dist/schema-builders/bigint.js +0 -47
- package/dist/schema-builders/bigint.js.map +0 -1
- package/dist/schema-builders/boolean.d.ts +0 -33
- package/dist/schema-builders/boolean.d.ts.map +0 -1
- package/dist/schema-builders/boolean.js +0 -6
- package/dist/schema-builders/boolean.js.map +0 -1
- package/dist/schema-builders/date.d.ts +0 -49
- package/dist/schema-builders/date.d.ts.map +0 -1
- package/dist/schema-builders/date.js +0 -6
- package/dist/schema-builders/date.js.map +0 -1
- package/dist/schema-builders/decimal128.d.ts +0 -79
- package/dist/schema-builders/decimal128.d.ts.map +0 -1
- package/dist/schema-builders/decimal128.js +0 -47
- package/dist/schema-builders/decimal128.js.map +0 -1
- package/dist/schema-builders/double.d.ts +0 -39
- package/dist/schema-builders/double.d.ts.map +0 -1
- package/dist/schema-builders/double.js +0 -51
- package/dist/schema-builders/double.js.map +0 -1
- package/dist/schema-builders/int32.d.ts +0 -39
- package/dist/schema-builders/int32.d.ts.map +0 -1
- package/dist/schema-builders/int32.js +0 -47
- package/dist/schema-builders/int32.js.map +0 -1
- package/dist/schema-builders/mixed.d.ts +0 -33
- package/dist/schema-builders/mixed.d.ts.map +0 -1
- package/dist/schema-builders/mixed.js +0 -7
- package/dist/schema-builders/mixed.js.map +0 -1
- package/dist/schema-builders/number.d.ts +0 -49
- package/dist/schema-builders/number.d.ts.map +0 -1
- package/dist/schema-builders/number.js +0 -6
- package/dist/schema-builders/number.js.map +0 -1
- package/dist/schema-builders/object-id.d.ts +0 -43
- package/dist/schema-builders/object-id.d.ts.map +0 -1
- package/dist/schema-builders/object-id.js +0 -7
- package/dist/schema-builders/object-id.js.map +0 -1
- package/dist/schema-builders/ref.d.ts +0 -44
- package/dist/schema-builders/ref.d.ts.map +0 -1
- package/dist/schema-builders/ref.js +0 -10
- package/dist/schema-builders/ref.js.map +0 -1
- package/dist/schema-builders/string.d.ts +0 -138
- package/dist/schema-builders/string.d.ts.map +0 -1
- package/dist/schema-builders/string.js +0 -48
- package/dist/schema-builders/string.js.map +0 -1
- package/dist/schema-builders/uuid.d.ts +0 -33
- package/dist/schema-builders/uuid.d.ts.map +0 -1
- package/dist/schema-builders/uuid.js +0 -7
- package/dist/schema-builders/uuid.js.map +0 -1
- package/dist/types/_internals.d.ts +0 -24
- package/dist/types/_internals.d.ts.map +0 -1
- package/dist/types/_internals.js +0 -1
- package/dist/types/aggregate-paginate.d.ts +0 -82
- package/dist/types/aggregate-paginate.d.ts.map +0 -1
- package/dist/types/aggregate-paginate.js +0 -1
- package/dist/types/options.d.ts +0 -23
- package/dist/types/options.d.ts.map +0 -1
- package/dist/types/options.js +0 -1
- package/src/schema-builders/_internals.ts +0 -64
- package/src/schema-builders/base.ts +0 -57
- package/src/schema-builders/bigint.ts +0 -156
- package/src/schema-builders/boolean.ts +0 -45
- package/src/schema-builders/date.ts +0 -80
- package/src/schema-builders/decimal128.ts +0 -196
- package/src/schema-builders/double.ts +0 -166
- package/src/schema-builders/int32.ts +0 -157
- package/src/schema-builders/mixed.ts +0 -37
- package/src/schema-builders/number.ts +0 -80
- package/src/schema-builders/object-id.ts +0 -60
- package/src/schema-builders/ref.ts +0 -67
- package/src/schema-builders/string.ts +0 -258
- package/src/schema-builders/uuid.ts +0 -48
- package/src/types/_internals.ts +0 -27
- package/src/types/aggregate-paginate.ts +0 -115
- package/src/types/options.ts +0 -46
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
import { uuidSchemaBuilder } from "./uuid.js";
|
|
13
|
-
export { bigintSchemaBuilder as bigint, bigintSchemaBuilder, booleanSchemaBuilder as boolean, booleanSchemaBuilder, dateSchemaBuilder as date, dateSchemaBuilder, decimal128SchemaBuilder as decimal128, decimal128SchemaBuilder, doubleSchemaBuilder as double, doubleSchemaBuilder, int32SchemaBuilder as int32, int32SchemaBuilder, mixedSchemaBuilder as mixed, mixedSchemaBuilder, numberSchemaBuilder as number, numberSchemaBuilder, objectIdSchemaBuilder as objectId, objectIdSchemaBuilder, refSchemaBuilder as ref, refSchemaBuilder, stringSchemaBuilder as string, stringSchemaBuilder, uuidSchemaBuilder as uuid, uuidSchemaBuilder };
|
|
1
|
+
import { array } from "./_internals/array.js";
|
|
2
|
+
import { boolean } from "./_internals/boolean.js";
|
|
3
|
+
import { date } from "./_internals/date.js";
|
|
4
|
+
import { decimal128 } from "./_internals/decimal128.js";
|
|
5
|
+
import { int32 } from "./_internals/int32.js";
|
|
6
|
+
import { mixed } from "./_internals/mixed.js";
|
|
7
|
+
import { number } from "./_internals/number.js";
|
|
8
|
+
import { objectId } from "./_internals/object-id.js";
|
|
9
|
+
import { ref } from "./_internals/ref.js";
|
|
10
|
+
import { string } from "./_internals/string.js";
|
|
11
|
+
export { array, boolean, date, decimal128, int32, mixed, number, objectId, ref, string };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ClientSession, Connection } from "mongoose";
|
|
2
|
+
export interface WithMongooseTransactionOptions {
|
|
3
|
+
connection?: Connection;
|
|
4
|
+
/**
|
|
5
|
+
* A non-null session runs `fn` with it instead of in a new transaction, so callers inside a transaction join it.
|
|
6
|
+
*/
|
|
7
|
+
session?: ClientSession | null;
|
|
8
|
+
}
|
|
9
|
+
export declare function withMongooseTransaction<T>(fn: (session: ClientSession) => Promise<T>, options?: WithMongooseTransactionOptions): Promise<T>;
|
|
10
|
+
//# sourceMappingURL=transaction.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.d.ts","names":[],"sources":["../src/transaction.ts"],"mappings":";iBAOiB;EACb,aAAa;;;;EAIb,UAAU;;wBAGQ,wBAAwB,GAC1C,KAAK,SAAS,kBAAkB,QAAQ,IACxC,UAAU,iCACX,QAAQ"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { getDefaultMongooseConnection } from "./connection.js";
|
|
2
|
+
// src/transaction.ts
|
|
3
|
+
async function withMongooseTransaction(fn, options) {
|
|
4
|
+
if (options?.session) return await fn(options.session);
|
|
5
|
+
return await (options?.connection ?? getDefaultMongooseConnection()).transaction(fn);
|
|
6
|
+
}
|
|
7
|
+
export { withMongooseTransaction };
|
|
8
|
+
|
|
9
|
+
//# sourceMappingURL=transaction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction.js","names":[],"sources":["../src/transaction.ts"],"sourcesContent":["import type {\n ClientSession,\n Connection,\n} from 'mongoose';\n\nimport { getDefaultMongooseConnection } from './connection';\n\nexport interface WithMongooseTransactionOptions {\n connection?: Connection;\n /**\n * A non-null session runs `fn` with it instead of in a new transaction, so callers inside a transaction join it.\n */\n session?: ClientSession | null;\n}\n\nexport async function withMongooseTransaction<T>(\n fn: (session: ClientSession) => Promise<T>,\n options?: WithMongooseTransactionOptions,\n): Promise<T> {\n if (options?.session) return await fn(options.session);\n return await (options?.connection ?? getDefaultMongooseConnection()).transaction(fn);\n}\n"],"mappings":";;AAeA,eAAsB,wBAClB,IACA,SACU;CACV,IAAI,SAAS,SAAS,OAAO,MAAM,GAAG,QAAQ,OAAO;CACrD,OAAO,OAAO,SAAS,cAAc,6BAA6B,EAAA,CAAG,YAAY,EAAE;AACvF"}
|
package/dist/types/data.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Types } from "mongoose";
|
|
1
2
|
import { SetFieldType } from "type-fest";
|
|
2
|
-
import { BaseMongooseDocType
|
|
3
|
+
import { BaseMongooseDocType } from "./index.js";
|
|
3
4
|
export type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = (CreatedAtField extends true ? {
|
|
4
5
|
createdAt: string;
|
|
5
6
|
} : unknown) & (UpdatedAtField extends true ? {
|
|
@@ -7,8 +8,5 @@ export type BaseMongooseModelData<CreatedAtField extends boolean = true, Updated
|
|
|
7
8
|
} : unknown) & {
|
|
8
9
|
id: string;
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
* Converts data fields to document fields using the same `_id` rules as BaseMongooseDocType.
|
|
12
|
-
*/
|
|
13
|
-
export type DataToBaseMongooseDocType<T, ObjectIdFields extends keyof T = never, DateFields extends Exclude<keyof T, ObjectIdFields> = never, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true, IdField extends boolean = true> = BaseMongooseDocType<SetFieldsToObjectId<SetFieldType<T, DateFields, Date>, ObjectIdFields>, CreatedAtField, UpdatedAtField, IdField>;
|
|
11
|
+
export type DataToBaseMongooseDocType<T, ObjectIdFields extends keyof T = never, DateFields extends Exclude<keyof T, ObjectIdFields> = never, CreatedAtField extends boolean = 'createdAt' extends keyof T ? true : false, UpdatedAtField extends boolean = 'updatedAt' extends keyof T ? true : false, IdField extends boolean = true> = BaseMongooseDocType<SetFieldType<SetFieldType<T, DateFields, Date>, ObjectIdFields, Types.ObjectId>, CreatedAtField, UpdatedAtField, IdField>;
|
|
14
12
|
//# sourceMappingURL=data.d.ts.map
|
package/dist/types/data.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.d.ts","names":[],"sources":["../../src/types/data.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"data.d.ts","names":[],"sources":["../../src/types/data.ts"],"mappings":";;;YAKY,sBAAsB,uCAAuC,0CACpE;EAAgC;gBAChC;EAAgC;;EAC/B;;YAEM,0BACR,GACA,6BAA6B,WAC7B,mBAAmB,cAAc,GAAG,yBACpC,2DAA2D,kBAC3D,2DAA2D,kBAC3D,kCACA,oBACA,aAAa,aAAa,GAAG,YAAY,OAAO,gBAAgB,MAAM,WACtE,gBACA,gBACA"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
import { HydratedDocument, QueryWithHelpers, Require_id, Types } from "mongoose";
|
|
2
|
-
import {
|
|
3
|
-
import { AggregatePaginateModel } from "./aggregate-paginate.js";
|
|
2
|
+
import { MongooseAssertionsDocumentMethods, MongooseAssertionsModelStatics } from "../plugins/assertions.js";
|
|
4
3
|
import { PaginateModel } from "./paginate.js";
|
|
5
|
-
/**
|
|
6
|
-
* Raw document type with an ObjectId by default, or the `_id` type declared in T.
|
|
7
|
-
* Set IdField to false for schemas with `_id: false`. Nested fields are left unchanged.
|
|
8
|
-
*/
|
|
9
4
|
export type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true, IdField extends boolean = true> = (CreatedAtField extends true ? {
|
|
10
5
|
createdAt: Date;
|
|
11
6
|
} : unknown) & (IdField extends true ? Require_id<Omit<T, 'createdAt' | 'id' | 'updatedAt'>> : Omit<T, '_id' | 'createdAt' | 'id' | 'updatedAt'>) & (UpdatedAtField extends true ? {
|
|
12
7
|
updatedAt: Date;
|
|
13
8
|
} : unknown);
|
|
14
|
-
export type
|
|
15
|
-
|
|
9
|
+
export type BaseMongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, BaseMongooseInstanceMethods<DocType, InstanceMethodsAndOverrides>, QueryHelpers>;
|
|
10
|
+
type BaseMongooseInstanceMethods<RawDocType, InstanceMethodsAndOverrides> = BaseMongooseDocumentMethods<RawDocType> & InstanceMethodsAndOverrides & MongooseAssertionsDocumentMethods<RawDocType>;
|
|
11
|
+
export type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = BaseMongooseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers> & MongooseAssertionsModelStatics<RawDocType> & PaginateModel<RawDocType, QueryHelpers, BaseMongooseInstanceMethods<RawDocType, InstanceMethodsAndOverrides>>;
|
|
16
12
|
export type MongooseDocumentOrObjectId<D> = D | string | Types.ObjectId;
|
|
17
13
|
export type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne', InstanceMethodsAndOverrides>;
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
export type OmitMongooseTimestampAndOtherFields<T, K extends PropertyKey = never> = Omit<T, 'createdAt' | 'updatedAt' | K>;
|
|
15
|
+
/**
|
|
16
|
+
* Augment this interface to type methods added to the documents of every built model, e.g. from a
|
|
17
|
+
* `addBeforeMongooseModelBuildHook` hook.
|
|
18
|
+
*/
|
|
19
|
+
export interface BaseMongooseDocumentMethods<RawDocType> {}
|
|
20
|
+
/**
|
|
21
|
+
* Augment this interface to type statics added to every built model, e.g. from a `addBeforeMongooseModelBuildHook`
|
|
22
|
+
* hook.
|
|
23
|
+
*/
|
|
21
24
|
export interface BaseMongooseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {}
|
|
25
|
+
export {};
|
|
22
26
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"mappings":";;;YAcY,oBACR,GACA,uCACA,uCACA,mCAEC;EAAgC,WAAW;gBAE5C,uBACM,WAAW,KAAK,wCAChB,KAAK,iDAEV;EAAgC,WAAW;;YAEpC,6BAA6B,SAAS,sCAAsC,yBACpF,iBAAiB,SAAS,4BAA4B,SAAS,8BAA8B;KAE5F,4BAA4B,YAAY,+BACzC,4BAA4B,cAC5B,8BACA,kCAAkC;YAE1B,0BAA0B,YAAY,sCAAsC,yBACpF,yBAAyB,YAAY,6BAA6B,gBAClE,+BAA+B,cAC/B,cACE,YACA,cACA,4BAA4B,YAAY;YAGlC,2BAA2B,KAAK,aAAa,MAAM;YACnD,0BACR,YACA,SACA,uBACA,wCACA,iBACI,gBACA,SACA,cACA,uBAEA;YAGI,oCAAoC,GAAG,UAAU,uBAAuB,KAChF,+BAC4B;;;;;iBASf,4BAA4B;;;;;iBAQ5B,yBAAyB,YAAY,sCAAsC"}
|
package/dist/types/paginate.d.ts
CHANGED
|
@@ -38,9 +38,6 @@ export interface PaginateOptions {
|
|
|
38
38
|
options?: QueryOptions;
|
|
39
39
|
page?: number;
|
|
40
40
|
/**
|
|
41
|
-
* Whether to apply pagination to the query results.
|
|
42
|
-
*
|
|
43
|
-
* @remarks
|
|
44
41
|
* If `false`, the pagination plugin does not apply its page-based `skip` or `limit`. Explicit query options
|
|
45
42
|
* and other query conditions still apply.
|
|
46
43
|
*
|
|
@@ -74,24 +71,12 @@ export interface PaginateResult<T> {
|
|
|
74
71
|
totalDocs: number;
|
|
75
72
|
totalPages: number;
|
|
76
73
|
}
|
|
77
|
-
export interface SubDocumentPagingOptions {
|
|
78
|
-
limit?: number;
|
|
79
|
-
page?: number;
|
|
80
|
-
populate?: PopulateOptions | PopulateOptions[] | string | string[];
|
|
81
|
-
}
|
|
82
|
-
export interface SubPaginateOptions {
|
|
83
|
-
pagination?: boolean;
|
|
84
|
-
pagingOptions?: SubDocumentPagingOptions;
|
|
85
|
-
populate?: PopulateOptions | PopulateOptions[] | string | string[];
|
|
86
|
-
read?: PaginateReadOptions;
|
|
87
|
-
select?: object | string;
|
|
88
|
-
}
|
|
89
74
|
declare module 'mongoose' {
|
|
90
|
-
interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType =
|
|
75
|
+
interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = unknown, QueryOp = 'find', TDocOverrides = Record<string, never>> {
|
|
91
76
|
paginate: {
|
|
92
|
-
<O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType,
|
|
93
|
-
<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<UserType,
|
|
94
|
-
<UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType,
|
|
77
|
+
<O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType, TDocOverrides, THelpers, O>>>;
|
|
78
|
+
<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<UserType, TDocOverrides, THelpers, O>>>;
|
|
79
|
+
<UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType, TDocOverrides, THelpers, PaginateOptions>>>;
|
|
95
80
|
};
|
|
96
81
|
}
|
|
97
82
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paginate.d.ts","names":[],"sources":["../../src/types/paginate.ts"],"mappings":";YAUY,iBACR,GACA,UACA,eACA,UAAU,4BACV,yBACE,2BAA2B,GAAG,KAC9B,iBAAiB,GAAG,UAAU;KAE/B,2BAA2B,GAAG,UAAU,mBACzC,+BACM;EAAM;IACN,qCACI;GAAO;IACP;iBAEG,qBAAqB;EAClC,OAAO;EACP,cAAc;EACd,cAAc;EACd,QAAQ;EACR,OAAO;EACP,WAAW;EACX,OAAO;EACP,gBAAgB;EAChB,WAAW;EACX,YAAY;EACZ,aAAa;;iBAGA,cAAc,YAAY,uBAAuB,8CAA8C,MAC5G,YACA,cACA;EAEA;KACK,UAAU,iBACP,QAAQ,YAAY,aACpB,UAAU,GACV,YACI,UACA,QAAQ,eAAe,iBAAiB,YAAY,6BAA6B,cAAc,eAEpG,QAAQ,eAAe,iBAAiB,YAAY,6BAA6B,cAAc;KAEjG,WAAW,YAAY,UAAU,kBAAkB,iBAChD,QAAQ,YAAY,aACpB,UAAU,GACV,YACI,UACA,QAAQ,eAAe,iBAAiB,UAAU,6BAA6B,cAAc,eAElG,QAAQ,eAAe,iBAAiB,UAAU,6BAA6B,cAAc;KAE/F,WAAW,YACR,QAAQ,YAAY,aACpB,UAAU,iBACV,YACI,UACA,QAAQ,eACJ,iBAAiB,UAAU,6BAA6B,cAAc,6BAG/E,QACC,eAAe,iBAAiB,UAAU,6BAA6B,cAAc;;;iBAKhF;EACb;EACA,YAAY,MAAM;EAClB,eAAe;EACf;EACA;EACA;EACA;EACA;EACA;EACA,UAAU;EACV
|
|
1
|
+
{"version":3,"file":"paginate.d.ts","names":[],"sources":["../../src/types/paginate.ts"],"mappings":";YAUY,iBACR,GACA,UACA,eACA,UAAU,4BACV,yBACE,2BAA2B,GAAG,KAC9B,iBAAiB,GAAG,UAAU;KAE/B,2BAA2B,GAAG,UAAU,mBACzC,+BACM;EAAM;IACN,qCACI;GAAO;IACP;iBAEG,qBAAqB;EAClC,OAAO;EACP,cAAc;EACd,cAAc;EACd,QAAQ;EACR,OAAO;EACP,WAAW;EACX,OAAO;EACP,gBAAgB;EAChB,WAAW;EACX,YAAY;EACZ,aAAa;;iBAGA,cAAc,YAAY,uBAAuB,8CAA8C,MAC5G,YACA,cACA;EAEA;KACK,UAAU,iBACP,QAAQ,YAAY,aACpB,UAAU,GACV,YACI,UACA,QAAQ,eAAe,iBAAiB,YAAY,6BAA6B,cAAc,eAEpG,QAAQ,eAAe,iBAAiB,YAAY,6BAA6B,cAAc;KAEjG,WAAW,YAAY,UAAU,kBAAkB,iBAChD,QAAQ,YAAY,aACpB,UAAU,GACV,YACI,UACA,QAAQ,eAAe,iBAAiB,UAAU,6BAA6B,cAAc,eAElG,QAAQ,eAAe,iBAAiB,UAAU,6BAA6B,cAAc;KAE/F,WAAW,YACR,QAAQ,YAAY,aACpB,UAAU,iBACV,YACI,UACA,QAAQ,eACJ,iBAAiB,UAAU,6BAA6B,cAAc,6BAG/E,QACC,eAAe,iBAAiB,UAAU,6BAA6B,cAAc;;;iBAKhF;EACb;EACA,YAAY,MAAM;EAClB,eAAe;EACf;EACA;EACA;EACA;EACA;EACA;EACA,UAAU;EACV;;;;;;;EAQA;EACA,WAAW,kBAAkB;EAC7B;EACA,OAAO;EACP;EACA;EACA,yBAAyB;EACzB;;iBAGa;EACb;EACA;;iBAGa,eAAe;GAC3B,gDAAgD;EACjD,MAAM;EACN;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;YAIU,MACN,YAEA,SACA,WAAW,sBACX,sBAEA,kBACA,gBAAgB;IAEhB;OACK,UAAU,iBAAiB,UAAU,IAAI,QACtC,eAAe,iBAAiB,YAAY,eAAe,UAAU;OAGxE,WAAW,YAAY,UAAU,kBAAkB,iBAAiB,UAAU,IAAI,QAC/E,eAAe,iBAAiB,UAAU,eAAe,UAAU;OAGtE,WAAW,YAAY,UAAU,kBAAkB,QAChD,eAAe,iBAAiB,UAAU,eAAe,UAAU;;;;iBAOlE,EAAE,QAAQ"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "10.0.0",
|
|
5
|
+
"description": "Typed Mongoose schema builders and a buildMongooseModel that applies pagination, JSON normalization, lean Decimal128-to-string conversion and update assertions, plus connection, transaction and duplicate-key helpers. ESM only.",
|
|
6
6
|
"author": "kiki-kanri",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"repository": {
|
|
@@ -28,21 +28,20 @@
|
|
|
28
28
|
"./assertions": "./dist/assertions.js",
|
|
29
29
|
"./builders": "./dist/builders.js",
|
|
30
30
|
"./connection": "./dist/connection.js",
|
|
31
|
+
"./documents": "./dist/documents.js",
|
|
31
32
|
"./options": "./dist/options.js",
|
|
32
33
|
"./schema-builders": "./dist/schema-builders/index.js",
|
|
34
|
+
"./transaction": "./dist/transaction.js",
|
|
33
35
|
"./types": {
|
|
34
36
|
"types": "./dist/types/index.d.ts",
|
|
35
37
|
"default": null
|
|
36
38
|
},
|
|
39
|
+
"./errors/*": "./dist/errors/*.js",
|
|
37
40
|
"./plugins/*": "./dist/plugins/*.js",
|
|
38
|
-
"./schema-builders/*": "./dist/schema-builders/*.js",
|
|
39
41
|
"./types/*": {
|
|
40
42
|
"types": "./dist/types/*.d.ts",
|
|
41
43
|
"default": null
|
|
42
44
|
},
|
|
43
|
-
"./schema-builders/_internals": null,
|
|
44
|
-
"./schema-builders/index": null,
|
|
45
|
-
"./types/_internals": null,
|
|
46
45
|
"./types/index": null,
|
|
47
46
|
"./package.json": "./package.json"
|
|
48
47
|
},
|
|
@@ -51,17 +50,17 @@
|
|
|
51
50
|
"./src"
|
|
52
51
|
],
|
|
53
52
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
53
|
+
"node": ">=24.15.0"
|
|
55
54
|
},
|
|
56
55
|
"scripts": {
|
|
57
56
|
"build": "tsdown",
|
|
58
57
|
"bumplog": "changelogen --bump --hideAuthorEmail",
|
|
59
58
|
"knip:deps": "knip --dependencies",
|
|
60
59
|
"knip:exports": "knip --exports",
|
|
61
|
-
"lint": "eslint --max-warnings 0",
|
|
62
|
-
"lint:fix": "eslint --fix --max-warnings 0",
|
|
60
|
+
"lint": "eslint --cache --concurrency=auto --max-warnings 0",
|
|
61
|
+
"lint:fix": "eslint --cache --concurrency=auto --fix --max-warnings 0",
|
|
63
62
|
"prepack": "pnpm run build",
|
|
64
|
-
"release": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && changelogen --hideAuthorEmail --push --release && npm publish",
|
|
63
|
+
"release": "pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build && changelogen --clean --hideAuthorEmail --push --release && npm publish",
|
|
65
64
|
"test": "vitest run --coverage",
|
|
66
65
|
"test:watch": "vitest watch --coverage",
|
|
67
66
|
"typecheck": "tsc -b --noEmit"
|
|
@@ -70,8 +69,7 @@
|
|
|
70
69
|
"dependencies": {
|
|
71
70
|
"decimal.js": "^10.6.0",
|
|
72
71
|
"es-toolkit": "^1.53.2",
|
|
73
|
-
"mongoose": "^9.
|
|
74
|
-
"mongoose-aggregate-paginate-v2": "^1.1.5",
|
|
72
|
+
"mongoose": "^9.13.1",
|
|
75
73
|
"mongoose-paginate-v2": "^1.9.5",
|
|
76
74
|
"ts-extras": "^3.0.0",
|
|
77
75
|
"type-fest": "^6.0.3"
|
|
@@ -79,18 +77,18 @@
|
|
|
79
77
|
"devDependencies": {
|
|
80
78
|
"@antfu/eslint-config": "^10.0.0",
|
|
81
79
|
"@arethetypeswrong/core": "^0.18.5",
|
|
82
|
-
"@kikiutils/eslint-config": "^11.
|
|
83
|
-
"@kikiutils/tsconfigs": "^
|
|
84
|
-
"@types/node": "^26.6.
|
|
85
|
-
"@vitest/coverage-v8": "^5.1.
|
|
80
|
+
"@kikiutils/eslint-config": "^11.5.0",
|
|
81
|
+
"@kikiutils/tsconfigs": "^8.0.0",
|
|
82
|
+
"@types/node": "^26.6.11",
|
|
83
|
+
"@vitest/coverage-v8": "^5.1.2",
|
|
86
84
|
"changelogen": "^0.6.2",
|
|
87
|
-
"eslint": "^10.
|
|
88
|
-
"knip": "^6.
|
|
85
|
+
"eslint": "^10.16.0",
|
|
86
|
+
"knip": "^6.49.0",
|
|
89
87
|
"mongoose-lean-getters": "^2.3.1",
|
|
90
88
|
"publint": "^0.3.25",
|
|
91
89
|
"tsdown": "^0.23.7",
|
|
92
90
|
"typescript": "^6.0.3",
|
|
93
91
|
"unplugin-unused": "^0.6.0",
|
|
94
|
-
"vitest": "^5.1.
|
|
92
|
+
"vitest": "^5.1.2"
|
|
95
93
|
}
|
|
96
94
|
}
|
package/src/_internals.ts
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
import type { Connection } from 'mongoose';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
BeforeMongooseModelBuildHook,
|
|
5
|
+
CustomMongooseOptions,
|
|
6
|
+
} from './options';
|
|
4
7
|
|
|
8
|
+
export const beforeModelBuildHooks: BeforeMongooseModelBuildHook[] = [];
|
|
5
9
|
export const customMongooseOptions: CustomMongooseOptions = {};
|
|
6
10
|
export const mongooseConnectionState: { defaultConnection?: Connection } = {};
|
|
11
|
+
|
|
12
|
+
const exponentNotationPattern = /^([-+]?)(\d+)(?:\.(\d+))?e([-+]\d+)$/i;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* `Decimal128#toString()` switches to exponent notation (`1E-8`, `0E-8`) when the adjusted exponent is below -6, as the
|
|
16
|
+
* BSON Decimal128 spec requires. This moves the decimal point instead, keeping the sign and the number of decimal
|
|
17
|
+
* places, and returns every other value unchanged so trailing zeros such as `12.30` are kept.
|
|
18
|
+
*/
|
|
19
|
+
export function decimal128ToPlainString(value: { toString: () => string }) {
|
|
20
|
+
const text = value.toString();
|
|
21
|
+
const match = exponentNotationPattern.exec(text);
|
|
22
|
+
if (!match) return text;
|
|
23
|
+
const [
|
|
24
|
+
, sign = '',
|
|
25
|
+
integerDigits = '',
|
|
26
|
+
fractionDigits = '',
|
|
27
|
+
exponent = '0',
|
|
28
|
+
] = match;
|
|
29
|
+
|
|
30
|
+
const digits = integerDigits + fractionDigits;
|
|
31
|
+
const pointIndex = integerDigits.length + Number(exponent);
|
|
32
|
+
if (pointIndex <= 0) return `${sign}0.${'0'.repeat(-pointIndex)}${digits}`;
|
|
33
|
+
const integerPart = digits.padEnd(pointIndex, '0').slice(0, pointIndex).replace(/^0+(?=\d)/, '');
|
|
34
|
+
const fractionPart = digits.slice(pointIndex);
|
|
35
|
+
return `${sign}${integerPart}${fractionPart ? `.${fractionPart}` : ''}`;
|
|
36
|
+
}
|
package/src/assertions.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
Document,
|
|
3
|
-
mongo,
|
|
4
|
-
} from 'mongoose';
|
|
1
|
+
import type { mongo } from 'mongoose';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Matched documents whose values were already equal to the update do not count as modified.
|
|
5
|
+
*/
|
|
6
|
+
export async function assertMongooseUpdateSuccess(
|
|
7
|
+
update: PromiseLike<mongo.UpdateResult<any>>,
|
|
8
|
+
minModifiedCount = 1,
|
|
9
9
|
) {
|
|
10
|
-
|
|
11
|
-
if (!
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
if (typeof minModifiedCount !== 'number') throw new TypeError('minModifiedCount must be a number');
|
|
11
|
+
if (!Number.isSafeInteger(minModifiedCount) || minModifiedCount < 1) {
|
|
12
|
+
throw new RangeError('minModifiedCount must be a positive safe integer');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const updateResult = await update;
|
|
16
|
+
if (!updateResult.acknowledged) {
|
|
17
|
+
throw new Error('Update was not acknowledged (Mongoose also reports this when the cast update is empty)');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const modifiedCount = updateResult.modifiedCount + updateResult.upsertedCount;
|
|
21
|
+
if (modifiedCount < minModifiedCount) {
|
|
22
|
+
throw new Error(`Expected to modify at least ${minModifiedCount} document(s), but modified ${modifiedCount}`);
|
|
14
23
|
}
|
|
15
24
|
}
|
package/src/builders.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type {
|
|
2
|
+
Connection,
|
|
3
|
+
Schema,
|
|
4
|
+
} from 'mongoose';
|
|
3
5
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
4
6
|
|
|
5
|
-
import {
|
|
7
|
+
import { beforeModelBuildHooks } from './_internals';
|
|
6
8
|
import { getDefaultMongooseConnection } from './connection';
|
|
7
9
|
import { mongooseAssertionsPlugin } from './plugins/assertions';
|
|
8
10
|
import { mongooseLeanDecimal128ToStringPlugin } from './plugins/lean-decimal128-to-string';
|
|
11
|
+
import type { MongooseLeanDecimal128ToStringPluginOptions } from './plugins/lean-decimal128-to-string';
|
|
9
12
|
import { mongooseNormalizePlugin } from './plugins/normalize';
|
|
10
13
|
import type { BaseMongoosePaginateModel } from './types';
|
|
11
|
-
import type { BuildMongooseModelOptions } from './types/options';
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
const builtSchemas = new WeakSet<object>();
|
|
16
|
+
|
|
17
|
+
export interface BuildMongooseModelOptions {
|
|
18
|
+
connection?: Connection;
|
|
19
|
+
plugins?: {
|
|
20
|
+
leanDecimal128ToString?: boolean | MongooseLeanDecimal128ToStringPluginOptions;
|
|
21
|
+
normalize?: boolean;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
14
24
|
|
|
15
25
|
export function buildMongooseModel<
|
|
16
26
|
DocType,
|
|
@@ -23,27 +33,25 @@ export function buildMongooseModel<
|
|
|
23
33
|
schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
24
34
|
options?: BuildMongooseModelOptions,
|
|
25
35
|
): Model {
|
|
36
|
+
// Resolve first so a missing default connection fails before the schema is mutated.
|
|
37
|
+
const connection = options?.connection ?? getDefaultMongooseConnection();
|
|
38
|
+
// Hook settings such as `toJSON` and `timestamps` must be in place before the plugins and defaults below read them,
|
|
39
|
+
// and a rebuilt schema has its `toJSON` options locked. Copied because a hook may remove itself while running.
|
|
40
|
+
if (!builtSchemas.has(schema)) [...beforeModelBuildHooks].forEach((hook) => hook(schema));
|
|
41
|
+
builtSchemas.add(schema);
|
|
26
42
|
const leanDecimal128ToStringPluginOptions = options?.plugins?.leanDecimal128ToString;
|
|
27
43
|
if (leanDecimal128ToStringPluginOptions !== false) {
|
|
28
44
|
schema.plugin(
|
|
29
45
|
mongooseLeanDecimal128ToStringPlugin,
|
|
30
|
-
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const normalizePluginOptions = options?.plugins?.normalize;
|
|
35
|
-
if (normalizePluginOptions !== false) {
|
|
36
|
-
schema.plugin(
|
|
37
|
-
mongooseNormalizePlugin,
|
|
38
|
-
typeof normalizePluginOptions === 'object' ? normalizePluginOptions : undefined,
|
|
46
|
+
leanDecimal128ToStringPluginOptions === true ? undefined : leanDecimal128ToStringPluginOptions,
|
|
39
47
|
);
|
|
40
48
|
}
|
|
41
49
|
|
|
42
|
-
schema.plugin(mongooseAggregatePaginate);
|
|
43
50
|
schema.plugin(mongooseAssertionsPlugin);
|
|
44
51
|
schema.plugin(mongoosePaginate);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
52
|
+
// Schema#set('timestamps') registers the timestamp hooks again, so only fill in the default.
|
|
53
|
+
if (schema.get('timestamps') === undefined) schema.set('timestamps', true);
|
|
54
|
+
// Registered last because it locks the schema's `toJSON` options.
|
|
55
|
+
if (options?.plugins?.normalize !== false) schema.plugin(mongooseNormalizePlugin);
|
|
48
56
|
return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
|
|
49
57
|
}
|
package/src/connection.ts
CHANGED
|
@@ -6,9 +6,15 @@ import {
|
|
|
6
6
|
mongooseConnectionState,
|
|
7
7
|
} from './_internals';
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @throws {Error} If `MONGODB_URI` is unset or empty while the default connection has not been created.
|
|
11
|
+
*/
|
|
9
12
|
export function getDefaultMongooseConnection(): Connection {
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
if (mongooseConnectionState.defaultConnection) return mongooseConnectionState.defaultConnection;
|
|
14
|
+
const uri = process.env.MONGODB_URI;
|
|
15
|
+
if (!uri) throw new Error('MONGODB_URI must be set before the default connection is created.');
|
|
16
|
+
return mongooseConnectionState.defaultConnection = mongoose.createConnection(
|
|
17
|
+
uri,
|
|
12
18
|
customMongooseOptions.defaultConnectionOptions,
|
|
13
19
|
);
|
|
14
20
|
}
|
package/src/documents.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Types } from 'mongoose';
|
|
2
|
+
|
|
3
|
+
export type MongooseDocumentId<T> = T extends string | Types.ObjectId ? T : T extends { _id: infer Id } ? Id : never;
|
|
4
|
+
|
|
5
|
+
export function getMongooseDocumentId<T extends string | Types.ObjectId | { _id: unknown }>(
|
|
6
|
+
value: T,
|
|
7
|
+
): MongooseDocumentId<T> {
|
|
8
|
+
const input: string | Types.ObjectId | { _id: unknown } = value;
|
|
9
|
+
// `_bsontype` also recognizes ObjectIds created by another copy of the bson package.
|
|
10
|
+
if (typeof input === 'string' || input instanceof Types.ObjectId || ('_bsontype' in input && input._bsontype === 'ObjectId')) return input as never;
|
|
11
|
+
return input._id as never;
|
|
12
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { mongo } from 'mongoose';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Also matches a bulk write error, e.g. from an unordered `insertMany`, when any of its write errors is a duplicate
|
|
5
|
+
* key; the driver sets only the first write error's code on the error itself.
|
|
6
|
+
*/
|
|
7
|
+
export function isMongooseDuplicateKeyError(error: unknown): error is mongo.MongoServerError & { code: 11000 } {
|
|
8
|
+
if (error instanceof mongo.MongoBulkWriteError) {
|
|
9
|
+
const writeErrors = Array.isArray(error.writeErrors) ? error.writeErrors : [error.writeErrors];
|
|
10
|
+
if (writeErrors.some((writeError) => writeError?.code === 11000)) return true;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return error instanceof mongo.MongoServerError && error.code === 11000;
|
|
14
|
+
}
|
package/src/options.ts
CHANGED
|
@@ -1,9 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ConnectOptions,
|
|
3
|
+
Schema,
|
|
4
|
+
} from 'mongoose';
|
|
5
|
+
|
|
1
6
|
import {
|
|
7
|
+
beforeModelBuildHooks,
|
|
2
8
|
customMongooseOptions,
|
|
3
9
|
mongooseConnectionState,
|
|
4
10
|
} from './_internals';
|
|
5
|
-
import type { CustomMongooseOptions } from './types/options';
|
|
6
11
|
|
|
12
|
+
export type BeforeMongooseModelBuildHook = (schema: Schema<any, any, any, any>) => void;
|
|
13
|
+
|
|
14
|
+
export interface CustomMongooseOptions {
|
|
15
|
+
defaultConnectionOptions?: ConnectOptions;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Hooks run in registration order on every schema passed to `buildMongooseModel`, once per schema, before the
|
|
20
|
+
* package plugins and the `timestamps` default are applied.
|
|
21
|
+
*
|
|
22
|
+
* @returns A function that removes the hook.
|
|
23
|
+
*/
|
|
24
|
+
export function addBeforeMongooseModelBuildHook(hook: BeforeMongooseModelBuildHook) {
|
|
25
|
+
// Wrapping keeps each registration distinct, so removing one never removes another of the same function.
|
|
26
|
+
const registration: BeforeMongooseModelBuildHook = (schema) => hook(schema);
|
|
27
|
+
beforeModelBuildHooks.push(registration);
|
|
28
|
+
return () => {
|
|
29
|
+
const index = beforeModelBuildHooks.indexOf(registration);
|
|
30
|
+
if (index !== -1) beforeModelBuildHooks.splice(index, 1);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @throws {Error} If `defaultConnectionOptions` is set after the default connection has been created.
|
|
36
|
+
*/
|
|
7
37
|
export function setCustomMongooseOptions<K extends keyof CustomMongooseOptions>(
|
|
8
38
|
key: K,
|
|
9
39
|
value: CustomMongooseOptions[K],
|