@linkedmink/multilevel-aging-cache-mongoose
Version:
Package implements Mongoose for @linkedmink/multilevel-aging-cache
98 lines (97 loc) • 3.81 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongooseProvider = void 0;
const multilevel_aging_cache_1 = require("@linkedmink/multilevel-aging-cache");
const Helpers_1 = require("./Helpers");
/**
* Use mongodb as a persistent storage mechanism with Mongoose documents
*/
class MongooseProvider {
/**
* @param model The object returned by 'mongoose'.model function
* @param options Configuration for this data provider
*/
constructor(model, options) {
this.model = model;
this.options = options;
this.isPersistable = true;
this.logger = multilevel_aging_cache_1.Logger.get(MongooseProvider.name);
this.keyProperty = this.options.keyProperty ?? '_id';
this.updateRecordAge = (value) => (0, Helpers_1.setDotSeperatedPropertyValue)(value.value, this.options.ageProperty, this.options.numberToAgeFunc ? this.options.numberToAgeFunc(value.age) : value.age);
this.getAgedValue = (value) => {
const ageValue = (0, Helpers_1.getDotSeperatedPropertyValue)(value, this.options.ageProperty);
const age = this.options.ageToNumberFunc
? this.options.ageToNumberFunc(ageValue)
: ageValue;
return { age, value };
};
this.execIgnoreError = (query) => query.exec().catch(e => {
this.logger.verbose({ message: e });
return null;
});
}
/**
* @param key The key to retrieve
* @returns The value if retreiving was successful or null
*/
async get(key) {
const query = this.options.keyFunc
? this.model.findOne(this.options.keyFunc(key))
: this.model.findById(key);
const result = await this.execIgnoreError(query);
return result !== null ? this.getAgedValue(result) : null;
}
/**
* @param key The key to set
* @param value The value to set
* @returns The value written if successful or null
*/
set(key, value) {
this.updateRecordAge(value);
return new Promise((resolve, reject) => {
value.value.save((error, doc) => {
if (!error) {
return resolve(this.getAgedValue(doc));
}
if ((0, Helpers_1.isMongooseValidationError)(error)) {
throw error;
}
else {
this.logger.info({ message: error });
}
return resolve(null);
});
});
}
/**
* @param key The key to the value to delete
* @returns The value deleted or boolean (value | true is success). A provider
* is not required to return a value
*/
async delete(key) {
const query = this.options.keyFunc
? this.model.findOneAndDelete(this.options.keyFunc(key))
: this.model.findByIdAndDelete(key);
const result = await this.execIgnoreError(query);
return result !== null;
}
/**
* @returns The keys that are currently available in the provider
*/
async keys() {
const query = this.model.find().select(this.keyProperty);
const result = await this.execIgnoreError(query);
if (result === null) {
return [];
}
return result.map(r => (0, Helpers_1.getDotSeperatedPropertyValue)(r, this.keyProperty));
}
/**
* @returns The number of elements in this storage system
*/
async size() {
const result = await this.execIgnoreError(this.model.countDocuments());
return result === null ? 0 : result;
}
}
exports.MongooseProvider = MongooseProvider;