@kikiutils/mongoose 2.3.0 → 3.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/dist/plugins/normalize.cjs +9 -9
- package/dist/plugins/normalize.cjs.map +1 -1
- package/dist/plugins/normalize.d.ts +0 -1
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.mjs +4 -3
- package/dist/plugins/normalize.mjs.map +1 -1
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/boolean.cjs.map +1 -1
- package/dist/schema-builders/boolean.d.ts.map +1 -1
- package/dist/schema-builders/boolean.mjs.map +1 -1
- package/dist/schema-builders/date.cjs.map +1 -1
- package/dist/schema-builders/date.d.ts.map +1 -1
- package/dist/schema-builders/date.mjs.map +1 -1
- package/dist/schema-builders/decimal128.cjs.map +1 -1
- package/dist/schema-builders/decimal128.d.ts.map +1 -1
- package/dist/schema-builders/decimal128.mjs.map +1 -1
- package/dist/schema-builders/index.cjs +9 -13
- package/dist/schema-builders/index.cjs.map +1 -1
- package/dist/schema-builders/index.d.ts +8 -19
- package/dist/schema-builders/index.d.ts.map +1 -1
- package/dist/schema-builders/index.mjs +7 -19
- package/dist/schema-builders/index.mjs.map +1 -1
- package/dist/schema-builders/number.cjs.map +1 -1
- package/dist/schema-builders/number.d.ts.map +1 -1
- package/dist/schema-builders/number.mjs.map +1 -1
- package/dist/schema-builders/object-id.cjs.map +1 -1
- package/dist/schema-builders/object-id.d.ts.map +1 -1
- package/dist/schema-builders/object-id.mjs.map +1 -1
- package/dist/schema-builders/ref.cjs.map +1 -1
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.mjs.map +1 -1
- package/dist/schema-builders/string.cjs.map +1 -1
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.mjs.map +1 -1
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts.map +1 -1
- package/dist/utils.cjs +24 -24
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +15 -15
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.mjs +24 -24
- package/dist/utils.mjs.map +1 -1
- package/package.json +12 -7
- package/src/plugins/normalize.ts +3 -9
- package/src/schema-builders/boolean.ts +4 -1
- package/src/schema-builders/date.ts +4 -1
- package/src/schema-builders/decimal128.ts +5 -5
- package/src/schema-builders/index.ts +14 -20
- package/src/schema-builders/number.ts +4 -1
- package/src/schema-builders/object-id.ts +8 -4
- package/src/schema-builders/ref.ts +11 -7
- package/src/schema-builders/string.ts +8 -4
- package/src/types/data.ts +4 -1
- package/src/types/index.ts +15 -3
- package/src/types/options.ts +6 -1
- package/src/utils.ts +27 -27
|
@@ -15,7 +15,10 @@ import { createBaseSchemaBuilderFactory } from './base';
|
|
|
15
15
|
export type ExtendRefSchemaBuilder<
|
|
16
16
|
Props extends BaseProps,
|
|
17
17
|
ExtraOmitFields extends string,
|
|
18
|
-
> = Omit<
|
|
18
|
+
> = Omit<
|
|
19
|
+
RefSchemaBuilder<Props, ExtraOmitFields>,
|
|
20
|
+
ExtraOmitFields | keyof Props
|
|
21
|
+
>;
|
|
19
22
|
|
|
20
23
|
interface BaseProps {
|
|
21
24
|
type: typeof Schema.Types.ObjectId;
|
|
@@ -40,9 +43,10 @@ export interface RefSchemaBuilder<
|
|
|
40
43
|
| { message?: M; values: Readonlyable<Array<null | O>> },
|
|
41
44
|
M extends string,
|
|
42
45
|
O extends Types.ObjectId,
|
|
43
|
-
>(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
>(value: T) => ExtendRefSchemaBuilder<
|
|
47
|
+
Merge<Props, { enum: T }>,
|
|
48
|
+
ExtraOmitFields
|
|
49
|
+
>;
|
|
46
50
|
|
|
47
51
|
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<
|
|
48
52
|
Merge<Props, { index: T }>,
|
|
@@ -58,8 +62,8 @@ export interface RefSchemaBuilder<
|
|
|
58
62
|
|
|
59
63
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.ObjectId);
|
|
60
64
|
|
|
61
|
-
export function refSchemaBuilder<
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
export function refSchemaBuilder<T extends ((this: any, doc: any) => Model<any> | string) | Model<any> | string>(
|
|
66
|
+
ref: T,
|
|
67
|
+
) {
|
|
64
68
|
return baseBuilderFactory({ ref }) as RefSchemaBuilder<{ ref: T; type: typeof Schema.Types.ObjectId }>;
|
|
65
69
|
}
|
|
@@ -15,7 +15,10 @@ import { createBaseSchemaBuilderFactory } from './base';
|
|
|
15
15
|
export type ExtendStringSchemaBuilder<
|
|
16
16
|
Props extends BaseProps,
|
|
17
17
|
ExtraOmitFields extends string,
|
|
18
|
-
> = Omit<
|
|
18
|
+
> = Omit<
|
|
19
|
+
StringSchemaBuilder<Props, ExtraOmitFields>,
|
|
20
|
+
ExtraOmitFields | keyof Props
|
|
21
|
+
>;
|
|
19
22
|
|
|
20
23
|
interface BaseProps {
|
|
21
24
|
type: StringSchemaDefinition;
|
|
@@ -45,9 +48,10 @@ export interface StringSchemaBuilder<
|
|
|
45
48
|
| { message?: M; values: Readonlyable<Array<null | S>> },
|
|
46
49
|
M extends string,
|
|
47
50
|
S extends string,
|
|
48
|
-
>(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
>(value: T) => ExtendStringSchemaBuilder<
|
|
52
|
+
Merge<Props, { enum: T }>,
|
|
53
|
+
ExtraOmitFields
|
|
54
|
+
>;
|
|
51
55
|
|
|
52
56
|
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<
|
|
53
57
|
Merge<Props, { index: T }>,
|
package/src/types/data.ts
CHANGED
|
@@ -76,7 +76,10 @@ declare global {
|
|
|
76
76
|
type OmitMongooseTimestampAndOtherFields<
|
|
77
77
|
T,
|
|
78
78
|
OT extends number | string | symbol = never,
|
|
79
|
-
> = Omit<
|
|
79
|
+
> = Omit<
|
|
80
|
+
T,
|
|
81
|
+
'createdAt' | 'updatedAt' | OT
|
|
82
|
+
>;
|
|
80
83
|
|
|
81
84
|
/**
|
|
82
85
|
* Type definition for including timestamp fields conditionally based on the provided flags.
|
package/src/types/index.ts
CHANGED
|
@@ -32,7 +32,11 @@ declare global {
|
|
|
32
32
|
* @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
|
|
33
33
|
* @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
|
|
34
34
|
*/
|
|
35
|
-
type BaseMongooseDocType<
|
|
35
|
+
type BaseMongooseDocType<
|
|
36
|
+
T,
|
|
37
|
+
CreatedAtField extends boolean = true,
|
|
38
|
+
UpdatedAtField extends boolean = true,
|
|
39
|
+
> =
|
|
36
40
|
& IfElse<CreatedAtField, { createdAt: Date }, object>
|
|
37
41
|
& IfElse<UpdatedAtField, { updatedAt: Date }, object>
|
|
38
42
|
& Omit<T, 'createdAt' | 'id' | 'updatedAt'>;
|
|
@@ -47,7 +51,11 @@ declare global {
|
|
|
47
51
|
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
48
52
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
49
53
|
*/
|
|
50
|
-
type BaseMongoosePaginateModel<
|
|
54
|
+
type BaseMongoosePaginateModel<
|
|
55
|
+
RawDocType,
|
|
56
|
+
InstanceMethodsAndOverrides = object,
|
|
57
|
+
QueryHelpers = object,
|
|
58
|
+
> =
|
|
51
59
|
& AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>
|
|
52
60
|
& BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>
|
|
53
61
|
& PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>;
|
|
@@ -108,5 +116,9 @@ declare global {
|
|
|
108
116
|
DocType,
|
|
109
117
|
InstanceMethodsAndOverrides = object,
|
|
110
118
|
QueryHelpers = object,
|
|
111
|
-
> = HydratedDocument<
|
|
119
|
+
> = HydratedDocument<
|
|
120
|
+
DocType,
|
|
121
|
+
InstanceMethodsAndOverrides,
|
|
122
|
+
QueryHelpers
|
|
123
|
+
>;
|
|
112
124
|
}
|
package/src/types/options.ts
CHANGED
|
@@ -59,6 +59,11 @@ export interface CustomMongooseOptions {
|
|
|
59
59
|
InstanceMethodsAndOverrides = object,
|
|
60
60
|
QueryHelpers = object,
|
|
61
61
|
>(
|
|
62
|
-
schema: Schema<
|
|
62
|
+
schema: Schema<
|
|
63
|
+
DocType,
|
|
64
|
+
Model,
|
|
65
|
+
InstanceMethodsAndOverrides,
|
|
66
|
+
QueryHelpers
|
|
67
|
+
>
|
|
63
68
|
) => void;
|
|
64
69
|
}
|
package/src/utils.ts
CHANGED
|
@@ -6,11 +6,37 @@ import mongoosePaginate from 'mongoose-paginate-v2';
|
|
|
6
6
|
import { customMongooseOptions } from './_internals';
|
|
7
7
|
import { mongooseConnections } from './constants';
|
|
8
8
|
import type { setCustomMongooseOptions } from './options';
|
|
9
|
-
import mongooseNormalizePlugin from './plugins/normalize';
|
|
9
|
+
import { mongooseNormalizePlugin } from './plugins/normalize';
|
|
10
10
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
11
11
|
|
|
12
12
|
export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* Asserts that a Mongoose update operation was successful by checking that it was acknowledged
|
|
16
|
+
* and that at least the expected number of documents were modified.
|
|
17
|
+
*
|
|
18
|
+
* This function is intended for use with `updateOne`, `updateMany`, or any update operation
|
|
19
|
+
* that returns a MongoDB `UpdateResult`.
|
|
20
|
+
*
|
|
21
|
+
* @template T - The shape of the document being updated.
|
|
22
|
+
* @param updatePromise - A promise returned from a Mongoose update operation.
|
|
23
|
+
* @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.
|
|
24
|
+
* @throws Will throw an error if:
|
|
25
|
+
* - The operation was not acknowledged by MongoDB,
|
|
26
|
+
* - Fewer documents were modified than expected.
|
|
27
|
+
*/
|
|
28
|
+
export async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(
|
|
29
|
+
updatePromise: Promise<mongo.UpdateResult<T>>,
|
|
30
|
+
expectedModifiedCount = 1,
|
|
31
|
+
) {
|
|
32
|
+
const updateResult = await updatePromise;
|
|
33
|
+
if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');
|
|
34
|
+
if (updateResult.modifiedCount < expectedModifiedCount) {
|
|
35
|
+
// eslint-disable-next-line style/max-len
|
|
36
|
+
throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
14
40
|
/**
|
|
15
41
|
* Builds and returns a Mongoose model with specified schema and options.
|
|
16
42
|
*
|
|
@@ -59,32 +85,6 @@ export function buildMongooseModel<
|
|
|
59
85
|
return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
|
|
60
86
|
}
|
|
61
87
|
|
|
62
|
-
/**
|
|
63
|
-
* Ensures that a MongoDB update operation was acknowledged and modified
|
|
64
|
-
* at least the expected number of documents.
|
|
65
|
-
*
|
|
66
|
-
* This utility is intended for use with `updateOne`, `updateMany`, or any
|
|
67
|
-
* update operation that returns a `UpdateResult`.
|
|
68
|
-
*
|
|
69
|
-
* @template T - The shape of the document being updated.
|
|
70
|
-
* @param updatePromise - A Promise returned from a MongoDB update operation (e.g., `updateOne`, `updateMany`).
|
|
71
|
-
* @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.
|
|
72
|
-
* @throws Will throw an error if:
|
|
73
|
-
* - The update operation was not acknowledged,
|
|
74
|
-
* - Fewer documents were modified than expected.
|
|
75
|
-
*/
|
|
76
|
-
export async function ensureUpdateSuccess<T extends mongo.BSON.Document>(
|
|
77
|
-
updatePromise: Promise<mongo.UpdateResult<T>>,
|
|
78
|
-
expectedModifiedCount = 1,
|
|
79
|
-
) {
|
|
80
|
-
const updateResult = await updatePromise;
|
|
81
|
-
if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');
|
|
82
|
-
if (updateResult.modifiedCount < expectedModifiedCount) {
|
|
83
|
-
// eslint-disable-next-line style/max-len
|
|
84
|
-
throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
88
|
/**
|
|
89
89
|
* Converts a document or ObjectId to a document using the specified model.
|
|
90
90
|
*
|