Comparing version 1.0.2 to 1.0.3
@@ -187,2 +187,3 @@ "use strict"; | ||
!Array.isArray(op.updateOne.update)) { | ||
const { update } = op.updateOne; | ||
operation = { | ||
@@ -192,8 +193,9 @@ updateOne: { | ||
update: { | ||
...op.updateOne.update, | ||
...update, | ||
$setOnInsert: utils_1.cleanSetOnInsert( | ||
// @ts-expect-error Type of the values is not recognized here | ||
$setOnInsert: { | ||
{ | ||
...model.defaults, | ||
...op.updateOne.update.$setOnInsert, | ||
}, | ||
...update.$setOnInsert, | ||
}, update), | ||
}, | ||
@@ -414,18 +416,7 @@ }, | ||
} | ||
const $setOnInsert = { | ||
const $setOnInsert = utils_1.cleanSetOnInsert({ | ||
...(model.defaults || {}), | ||
...finalUpdate.$setOnInsert, | ||
...created, | ||
}; | ||
// Do not attempt to set defaults if properties are present in $set, $push, $inc or $unset | ||
for (const key of Object.keys($setOnInsert)) { | ||
if (Object.prototype.hasOwnProperty.call(finalUpdate.$set || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$push || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$inc || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$unset || {}, key)) { | ||
// @ts-expect-error This is typed as read-only in `@types/mongodb`, but we can mutate it here | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete $setOnInsert[key]; | ||
} | ||
} | ||
}, finalUpdate); | ||
const result = await model.collection.findOneAndUpdate(filter, { | ||
@@ -432,0 +423,0 @@ ...finalUpdate, |
@@ -37,1 +37,2 @@ import type { BulkWriteOperation, ObjectId, OptionalId, UpdateQuery } from 'mongodb'; | ||
export declare function timestampBulkWriteOperation<TSchema>(operation: BulkWriteOperation<TSchema>): BulkWriteOperation<TSchema>; | ||
export declare function cleanSetOnInsert<TSchema>($setOnInsert: NonNullable<UpdateQuery<TSchema>['$setOnInsert']>, update: UpdateQuery<TSchema>): NonNullable<UpdateQuery<TSchema>['$setOnInsert']>; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.timestampBulkWriteOperation = exports.timestampUpdateQuery = exports.getIds = exports.VALIDATION_LEVEL = exports.VALIDATION_ACTIONS = void 0; | ||
exports.cleanSetOnInsert = exports.timestampBulkWriteOperation = exports.timestampUpdateQuery = exports.getIds = exports.VALIDATION_LEVEL = exports.VALIDATION_ACTIONS = void 0; | ||
const mongodb_1 = __importDefault(require("mongodb")); | ||
@@ -128,1 +128,15 @@ var VALIDATION_ACTIONS; | ||
exports.timestampBulkWriteOperation = timestampBulkWriteOperation; | ||
// Clean defaults if properties are present in $set, $push, $inc or $unset | ||
function cleanSetOnInsert($setOnInsert, update) { | ||
for (const key of Object.keys($setOnInsert)) { | ||
if (key in (update.$set || {}) || | ||
key in (update.$push || {}) || | ||
key in (update.$inc || {}) || | ||
key in (update.$unset || {})) { | ||
// @ts-expect-error This is typed as read-only in `@types/mongodb`, but we can mutate it here | ||
delete $setOnInsert[key]; | ||
} | ||
} | ||
return $setOnInsert; | ||
} | ||
exports.cleanSetOnInsert = cleanSetOnInsert; |
import mongodb from 'mongodb'; | ||
import { serializeArguments } from './hooks'; | ||
import { timestampBulkWriteOperation, timestampUpdateQuery, } from './utils'; | ||
import { cleanSetOnInsert, timestampBulkWriteOperation, timestampUpdateQuery, } from './utils'; | ||
function abstractMethod() { | ||
@@ -180,2 +180,3 @@ throw new Error('Collection is not initialized!'); | ||
!Array.isArray(op.updateOne.update)) { | ||
const { update } = op.updateOne; | ||
operation = { | ||
@@ -185,8 +186,9 @@ updateOne: { | ||
update: { | ||
...op.updateOne.update, | ||
...update, | ||
$setOnInsert: cleanSetOnInsert( | ||
// @ts-expect-error Type of the values is not recognized here | ||
$setOnInsert: { | ||
{ | ||
...model.defaults, | ||
...op.updateOne.update.$setOnInsert, | ||
}, | ||
...update.$setOnInsert, | ||
}, update), | ||
}, | ||
@@ -407,18 +409,7 @@ }, | ||
} | ||
const $setOnInsert = { | ||
const $setOnInsert = cleanSetOnInsert({ | ||
...(model.defaults || {}), | ||
...finalUpdate.$setOnInsert, | ||
...created, | ||
}; | ||
// Do not attempt to set defaults if properties are present in $set, $push, $inc or $unset | ||
for (const key of Object.keys($setOnInsert)) { | ||
if (Object.prototype.hasOwnProperty.call(finalUpdate.$set || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$push || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$inc || {}, key) || | ||
Object.prototype.hasOwnProperty.call(finalUpdate.$unset || {}, key)) { | ||
// @ts-expect-error This is typed as read-only in `@types/mongodb`, but we can mutate it here | ||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
delete $setOnInsert[key]; | ||
} | ||
} | ||
}, finalUpdate); | ||
const result = await model.collection.findOneAndUpdate(filter, { | ||
@@ -425,0 +416,0 @@ ...finalUpdate, |
@@ -37,1 +37,2 @@ import type { BulkWriteOperation, ObjectId, OptionalId, UpdateQuery } from 'mongodb'; | ||
export declare function timestampBulkWriteOperation<TSchema>(operation: BulkWriteOperation<TSchema>): BulkWriteOperation<TSchema>; | ||
export declare function cleanSetOnInsert<TSchema>($setOnInsert: NonNullable<UpdateQuery<TSchema>['$setOnInsert']>, update: UpdateQuery<TSchema>): NonNullable<UpdateQuery<TSchema>['$setOnInsert']>; |
@@ -118,1 +118,14 @@ import mongodb from 'mongodb'; | ||
} | ||
// Clean defaults if properties are present in $set, $push, $inc or $unset | ||
export function cleanSetOnInsert($setOnInsert, update) { | ||
for (const key of Object.keys($setOnInsert)) { | ||
if (key in (update.$set || {}) || | ||
key in (update.$push || {}) || | ||
key in (update.$inc || {}) || | ||
key in (update.$unset || {})) { | ||
// @ts-expect-error This is typed as read-only in `@types/mongodb`, but we can mutate it here | ||
delete $setOnInsert[key]; | ||
} | ||
} | ||
return $setOnInsert; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"author": "Plex Inc.", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "mongodb", |
145819
3858