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.
80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
4 years ago
|
import * as builtin from './builtin';
|
||
|
import * as document from './document';
|
||
|
import * as db from '../service/db';
|
||
|
import * as validator from '../lib/validator';
|
||
4 years ago
|
|
||
|
const coll = db.collection('domain');
|
||
|
|
||
4 years ago
|
export function add(domainId: string, owner: number, name: string, isEnsure = false) {
|
||
4 years ago
|
const tasks = [];
|
||
|
if (isEnsure) {
|
||
|
tasks.push(
|
||
|
coll.updateOne(
|
||
|
{ _id: domainId },
|
||
|
{ $set: { owner }, $setOnInsert: { name, bulletin: '' } },
|
||
|
{ upsert: true },
|
||
|
),
|
||
|
);
|
||
|
} else {
|
||
|
tasks.push(
|
||
|
coll.insertOne({
|
||
|
_id: domainId, owner, name, bulletin: '',
|
||
|
}),
|
||
|
);
|
||
|
}
|
||
4 years ago
|
for (const id in builtin.BUILTIN_ROLES) {
|
||
|
tasks.push(
|
||
|
document.add(
|
||
|
domainId, builtin.BUILTIN_ROLES[id].perm, owner,
|
||
4 years ago
|
document.TYPE_DOMAIN_USER, id,
|
||
4 years ago
|
),
|
||
|
);
|
||
|
}
|
||
|
return Promise.all(tasks);
|
||
|
}
|
||
|
|
||
4 years ago
|
export async function get(domainId: string) {
|
||
4 years ago
|
return coll.findOne({ _id: domainId });
|
||
|
}
|
||
|
|
||
4 years ago
|
export function getMany(query: any, sort: any, page: number, limit: number) {
|
||
4 years ago
|
return coll.find(query).sort(sort).skip((page - 1) * limit).limit(limit)
|
||
|
.toArray();
|
||
|
}
|
||
|
|
||
4 years ago
|
export function getMulti(query: any) {
|
||
4 years ago
|
return coll.find(query);
|
||
|
}
|
||
|
|
||
4 years ago
|
export function edit(domainId: string, $set: any) {
|
||
4 years ago
|
if ($set.title) validator.checkTitle($set.title);
|
||
|
if ($set.content) validator.checkContent($set.content);
|
||
|
return coll.updateOne({ _id: domainId }, { $set });
|
||
|
}
|
||
|
|
||
4 years ago
|
export async function inc(domainId: string, field: string, n: number) {
|
||
4 years ago
|
const res = await coll.findOneAndUpdate(
|
||
|
{ _id: domainId },
|
||
|
{ $inc: { [field]: n } },
|
||
|
{ returnOriginal: false },
|
||
|
);
|
||
|
return res.value;
|
||
|
}
|
||
|
|
||
4 years ago
|
export async function getList(domainIds: string[]) {
|
||
4 years ago
|
const r = {};
|
||
|
// eslint-disable-next-line no-await-in-loop
|
||
|
for (const domainId of domainIds) r[domainId] = await get(domainId);
|
||
|
return r;
|
||
|
}
|
||
|
|
||
4 years ago
|
global.Hydro.model.domain = {
|
||
4 years ago
|
add,
|
||
|
inc,
|
||
|
get,
|
||
|
getMany,
|
||
|
edit,
|
||
|
getMulti,
|
||
|
getList,
|
||
|
};
|