@linkedmink/multilevel-aging-cache-mongoose
Version:
Package implements Mongoose for @linkedmink/multilevel-aging-cache
41 lines (40 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMongooseValidationError = exports.setDotSeperatedPropertyValue = exports.getDotSeperatedPropertyValue = void 0;
const mongoose_1 = require("mongoose");
function getDotSeperatedPropertyValue(search, path) {
const pathParts = path.split('.');
let currentProp = search;
for (let i = 0; i < pathParts.length; i++) {
if (currentProp[pathParts[i]] !== undefined) {
currentProp = currentProp[pathParts[i]];
}
else {
throw new mongoose_1.Error(`${path} does not exist on object`);
}
}
return currentProp;
}
exports.getDotSeperatedPropertyValue = getDotSeperatedPropertyValue;
function setDotSeperatedPropertyValue(search, path, setValue) {
const pathParts = path.split('.');
let currentProp = search;
for (let i = 0; i < pathParts.length; i++) {
if (i === pathParts.length - 1) {
currentProp[pathParts[i]] = setValue;
return;
}
if (currentProp[pathParts[i]] !== undefined) {
currentProp = currentProp[pathParts[i]];
}
else {
throw new mongoose_1.Error(`${path} does not exist on object`);
}
}
}
exports.setDotSeperatedPropertyValue = setDotSeperatedPropertyValue;
function isMongooseValidationError(value) {
const error = value;
return error.name === 'ValidationError' && error.errors !== undefined;
}
exports.isMongooseValidationError = isMongooseValidationError;