Files
cgm-remote-monitor/lib/api3/storage/mongoCollection/index.js
T

76 lines
1.7 KiB
JavaScript

'use strict';
/**
* Storage implementation using mongoDB
* @param {Object} ctx
* @param {Object} env
* @param {string} colName - name of the collection in mongo database
*/
function MongoCollection (ctx, env, colName) {
const self = this
, utils = require('./utils')
, find = require('./find')
, modify = require('./modify')
;
self.colName = colName;
self.col = ctx.store.collection(colName);
ctx.store.ensureIndexes(self.col, [ 'identifier',
'srvModified',
'isValid'
]);
self.identifyingFilter = utils.identifyingFilter;
self.findOne = (...args) => find.findOne(self.col, ...args);
self.findOneFilter = (...args) => find.findOneFilter(self.col, ...args);
self.findMany = (...args) => find.findMany(self.col, ...args);
self.insertOne = (...args) => modify.insertOne(self.col, ...args);
self.replaceOne = (...args) => modify.replaceOne(self.col, ...args);
self.updateOne = (...args) => modify.updateOne(self.col, ...args);
self.deleteOne = (...args) => modify.deleteOne(self.col, ...args);
self.deleteManyOr = (...args) => modify.deleteManyOr(self.col, ...args);
/**
* Get server version
*/
self.version = async function version () {
const result = await ctx.store.db.admin().buildInfo();
return {
storage: 'mongodb',
version: result.version
};
};
/**
* Get timestamp (e.g. srvModified) of the last modified document
*/
self.getLastModified = async function getLastModified (fieldName) {
const [ result ] = await self.col.find()
.sort({ [fieldName]: -1 })
.limit(1)
.project({ [fieldName]: 1 })
.toArray();
return result;
}
}
module.exports = MongoCollection;