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.
48 lines
1.0 KiB
JavaScript
48 lines
1.0 KiB
JavaScript
const db = require('../service/db');
|
|
|
|
const coll = db.collection('system');
|
|
|
|
async function get(_id) {
|
|
const doc = await coll.findOne({ _id });
|
|
if (doc) return doc.value;
|
|
return null;
|
|
}
|
|
|
|
async function getMany(keys) {
|
|
const docs = await coll.find({ _id: { $in: keys } }).toArray();
|
|
const dict = {};
|
|
const res = [];
|
|
for (const doc of docs) {
|
|
dict[doc._id] = doc.value;
|
|
}
|
|
for (const key of keys) {
|
|
res.push(dict[key] || null);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
async function update(_id, operation, config) {
|
|
await coll.findOneAndUpdate({ _id }, operation, config);
|
|
return get(_id);
|
|
}
|
|
|
|
async function set(_id, value) {
|
|
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 });
|
|
}
|
|
|
|
global.Hydro.model.system = module.exports = {
|
|
get,
|
|
getMany,
|
|
update,
|
|
inc,
|
|
set,
|
|
};
|