添加数据库类型别名

This commit is contained in:
luying
2020-12-15 18:08:29 +08:00
parent 1565dd206a
commit 2d774fc0e4
38 changed files with 393 additions and 321 deletions

View File

@@ -1,7 +1,7 @@
import { COUNTER } from './../consts';
import { CounterModel } from './Counter';
import BaseModel from './BaseModel';
import { index, getModelForClass, prop } from '@typegoose/typegoose';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
/**
* GM用户组接口
@@ -28,32 +28,32 @@ export default class GMGroup extends BaseModel {
public static async createGroup(group: string, name: string, desc: string, lean = true) {
const id = await CounterModel.getNewCounter(COUNTER.GM_GROUP);
const result = await GMGroupModel.findOneAndUpdate({id}, {group, desc, name, apis: []}, {upsert: true, new: true}).select('uid username name').lean(lean);
const result: GMGroupType = await GMGroupModel.findOneAndUpdate({id}, {group, desc, name, apis: []}, {upsert: true, new: true}).select('uid username name').lean(lean);
return result;
}
public static async editGroup(groupId: number, group: string, name: string, desc: string, lean = true) {
const result = await GMGroupModel.findOneAndUpdate({ id: groupId }, {$set: {group, desc, name}}).select('uid username name').lean(lean);
const result: GMGroupType = await GMGroupModel.findOneAndUpdate({ id: groupId }, {$set: {group, desc, name}}).select('uid username name').lean(lean);
return result;
}
public static async getAllGroup(lean = true) {
const list = await GMGroupModel.find().lean(lean);
const list: GMGroupType[] = await GMGroupModel.find().lean(lean);
return list;
}
public static async getGroup(group: string, lean = true) {
const result = await GMGroupModel.findOne({ group }).lean(lean);
const result: GMGroupType = await GMGroupModel.findOne({ group }).lean(lean);
return result;
}
public static async getGroupById(id: number, lean = true) {
const result = await GMGroupModel.findOne({ id }).lean(lean);
const result: GMGroupType = await GMGroupModel.findOne({ id }).lean(lean);
return result;
}
public static async saveApiToGroup(id: number, apis: Array<number>, lean = true) {
let group = await GMGroupModel.getGroupById(id);
let group: GMGroupType = await GMGroupModel.getGroupById(id);
if(group) {
let originApi = group.apis||[];
let addArr = new Array(), removeArr = new Array();
@@ -66,7 +66,7 @@ export default class GMGroup extends BaseModel {
if(apis.indexOf(api) == -1) removeArr.push(api);
}
await GMGroupModel.findOneAndUpdate({ id }, { $push: {apis: {$each: addArr}} }).lean(lean);
const result = await GMGroupModel.findOneAndUpdate({ id }, { $pull: {apis: {$in: removeArr}} }).lean(lean);
const result: GMGroupType = await GMGroupModel.findOneAndUpdate({ id }, { $pull: {apis: {$in: removeArr}} }).lean(lean);
return result;
}
}
@@ -75,3 +75,5 @@ export default class GMGroup extends BaseModel {
}
export const GMGroupModel = getModelForClass(GMGroup);
export interface GMGroupType extends Pick<DocumentType<GMGroup>, keyof GMGroup>{};