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.
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
4 years ago
|
import { FindOneAndUpdateOption } from 'mongodb';
|
||
|
import * as db from '../service/db';
|
||
5 years ago
|
|
||
|
const coll = db.collection('system');
|
||
5 years ago
|
|
||
4 years ago
|
export async function get(_id: string) {
|
||
5 years ago
|
const doc = await coll.findOne({ _id });
|
||
5 years ago
|
if (doc) return doc.value;
|
||
5 years ago
|
return null;
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
export async function getMany(keys: string[]) {
|
||
4 years ago
|
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;
|
||
|
}
|
||
|
|
||
4 years ago
|
export async function update(_id: string, operation: any, config: FindOneAndUpdateOption) {
|
||
5 years ago
|
await coll.findOneAndUpdate({ _id }, operation, config);
|
||
5 years ago
|
return get(_id);
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
export async function set(_id: string, value: any) {
|
||
5 years ago
|
await coll.findOneAndUpdate({ _id }, { $set: { value } }, { upsert: true });
|
||
5 years ago
|
return get(_id);
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
|
export function inc(field: string) {
|
||
5 years ago
|
return update(field, { $inc: { value: 1 } }, { upsert: true });
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
global.Hydro.model.system = {
|
||
5 years ago
|
get,
|
||
4 years ago
|
getMany,
|
||
5 years ago
|
update,
|
||
5 years ago
|
inc,
|
||
|
set,
|
||
5 years ago
|
};
|