@kikiutils/mongoose 2.1.8 → 2.3.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/options.cjs +3 -1
- package/dist/options.cjs.map +1 -1
- package/dist/options.d.ts +1 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.mjs +3 -1
- package/dist/options.mjs.map +1 -1
- package/dist/plugins/normalize.cjs +2 -1
- package/dist/plugins/normalize.cjs.map +1 -1
- package/dist/plugins/normalize.d.ts.map +1 -1
- package/dist/plugins/normalize.mjs +2 -1
- package/dist/plugins/normalize.mjs.map +1 -1
- package/dist/schema-builders/base.cjs.map +1 -1
- package/dist/schema-builders/base.d.ts +3 -1
- package/dist/schema-builders/base.d.ts.map +1 -1
- package/dist/schema-builders/base.mjs.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 +4 -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 +4 -1
- package/dist/schema-builders/decimal128.mjs.map +1 -1
- package/dist/schema-builders/index.d.ts +2 -4
- package/dist/schema-builders/index.d.ts.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 +3 -1
- package/dist/schema-builders/ref.cjs.map +1 -1
- package/dist/schema-builders/ref.d.ts +1 -1
- package/dist/schema-builders/ref.d.ts.map +1 -1
- package/dist/schema-builders/ref.mjs +3 -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 +4 -2
- package/dist/schema-builders/string.d.ts.map +1 -1
- package/dist/schema-builders/string.mjs.map +1 -1
- package/dist/types/aggregate-paginate.d.ts.map +1 -1
- package/dist/types/data.d.ts.map +1 -1
- package/dist/types/index.d.ts +2 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/options.d.ts +7 -4
- package/dist/types/options.d.ts.map +1 -1
- package/dist/types/paginate.d.ts.map +1 -1
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/utils.cjs +30 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.ts +16 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.mjs +30 -4
- package/dist/utils.mjs.map +1 -1
- package/package.json +7 -7
- package/src/options.ts +6 -1
- package/src/plugins/normalize.ts +4 -1
- package/src/schema-builders/base.ts +8 -1
- package/src/schema-builders/boolean.ts +21 -4
- package/src/schema-builders/date.ts +43 -9
- package/src/schema-builders/decimal128.ts +41 -10
- package/src/schema-builders/number.ts +39 -6
- package/src/schema-builders/object-id.ts +21 -4
- package/src/schema-builders/ref.ts +27 -5
- package/src/schema-builders/string.ts +61 -12
- package/src/types/aggregate-paginate.ts +5 -1
- package/src/types/data.ts +16 -4
- package/src/types/index.ts +7 -2
- package/src/types/options.ts +7 -4
- package/src/types/paginate.ts +27 -6
- package/src/types/utils.ts +7 -1
- package/src/utils.ts +35 -3
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { mongo } from 'mongoose';
|
|
1
2
|
import mongoose, { Types } from 'mongoose';
|
|
2
3
|
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
3
4
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
@@ -51,9 +52,37 @@ export function buildMongooseModel<
|
|
|
51
52
|
if (options?.enableNormalizePlugin !== false) schema.plugin(mongooseNormalizePlugin);
|
|
52
53
|
schema.plugin(mongooseAggregatePaginate);
|
|
53
54
|
schema.plugin(mongoosePaginate);
|
|
54
|
-
schema.set('timestamps', options?.timestamps ?? true);
|
|
55
|
+
schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
|
|
55
56
|
customMongooseOptions.beforeModelBuild?.(schema);
|
|
56
|
-
|
|
57
|
+
// eslint-disable-next-line style/max-len
|
|
58
|
+
const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
|
|
59
|
+
return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
|
|
60
|
+
}
|
|
61
|
+
|
|
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
|
+
}
|
|
57
86
|
}
|
|
58
87
|
|
|
59
88
|
/**
|
|
@@ -83,6 +112,9 @@ export async function mongooseDocumentOrObjectIdToDocument<
|
|
|
83
112
|
model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
|
|
84
113
|
selectFields?: string[],
|
|
85
114
|
): Promise<D | null> {
|
|
86
|
-
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
|
|
115
|
+
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {
|
|
116
|
+
return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
87
119
|
return documentOrObjectId;
|
|
88
120
|
}
|