@datastax/astra-mongoose
Version:
Astra's NodeJS Mongoose compatibility client
57 lines • 1.85 kB
JavaScript
;
// Copyright DataStax, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.setDefaultIdForReplace = setDefaultIdForReplace;
exports.setDefaultIdForUpdate = setDefaultIdForUpdate;
const mongoose_1 = require("mongoose");
function setDefaultIdForReplace(filter, update, options) {
if (!options.upsert) {
return;
}
if ('_id' in filter) {
return;
}
if ('_id' in update) {
return;
}
update._id = new mongoose_1.Types.ObjectId();
}
function setDefaultIdForUpdate(filter, update, options) {
if (!options.upsert) {
return;
}
if ('_id' in filter) {
return;
}
if (_updateHasKey(update, '_id')) {
return;
}
if (update.$setOnInsert == null) {
update.$setOnInsert = {};
}
if (!('_id' in update.$setOnInsert)) {
update.$setOnInsert._id = new mongoose_1.Types.ObjectId();
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _updateHasKey(update, key) {
for (const operator of Object.keys(update)) {
if (update[operator] != null && typeof update[operator] === 'object' && key in update[operator]) {
return true;
}
}
return false;
}
//# sourceMappingURL=setDefaultIdForUpsert.js.map