You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/hydro/model/system.js

31 lines
710 B
JavaScript

5 years ago
const db = require('../service/db.js');
const coll = db.collection('system');
async function get(_id) {
5 years ago
const doc = await coll.findOne({ _id });
if (doc) return doc.value;
5 years ago
return null;
}
async function update(_id, operation, config) {
await coll.findOneAndUpdate({ _id }, operation, config);
5 years ago
return get(_id);
}
async function set(_id, value) {
5 years ago
await coll.findOneAndUpdate({ _id }, { $set: { value } }, { upsert: true });
return get(_id);
}
/**
* Increments the counter.
* @returns {number} Integer value after increment.
*/
function inc(field) {
return update(field, { $inc: { value: 1 } }, { upsert: true });
}
module.exports = {
get,
update,
inc,
set,
};