添加数据库类型别名
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop } from '@typegoose/typegoose';
|
||||
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
|
||||
|
||||
@index({ roleId: 1, id: 1 })
|
||||
@index({ seqId: 1 })
|
||||
@@ -22,36 +22,36 @@ export default class Item extends BaseModel {
|
||||
|
||||
|
||||
public static async findbyRole(roleId: string, lean = true) {
|
||||
const items = await ItemModel.find({ roleId }).select('id count type').lean(lean);
|
||||
const items: ItemType[] = await ItemModel.find({ roleId }).select('id count type').lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async findbyRoleAndIds(roleId: string, ids: Array<number>, lean = true) {
|
||||
const items = await ItemModel.find({ roleId, id: {$in: ids} }).select('id count type').lean(lean);
|
||||
const items: ItemType[] = await ItemModel.find({ roleId, id: {$in: ids} }).select('id count type').lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async findByRoleAndType(roleId: string, type: number, lean = true) {
|
||||
const items = await ItemModel.find({ roleId, type }).select('id count type').sort({id: 1}).lean(lean);
|
||||
const items: ItemType[] = await ItemModel.find({ roleId, type }).select('id count type').sort({id: 1}).lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async findbyRoleAndGidAndCount(roleId: string, id: number, count: number, lean = true) {
|
||||
const items = await ItemModel.findOne({ roleId, id, count: {$gte: count} }).select('id count type').lean(lean);
|
||||
const items: ItemType = await ItemModel.findOne({ roleId, id, count: {$gte: count} }).select('id count type').lean(lean);
|
||||
return items;
|
||||
}
|
||||
|
||||
public static async increaseItem(roleId: string, id: number, count: number, itemInfo: {roleId: string, roleName: string, id: number, itemName: string, type: number, hid?: number }, lean = true) {
|
||||
const doc = new ItemModel();
|
||||
const setOnInsert = Object.assign(doc.toJSON(), itemInfo);
|
||||
const equips = await ItemModel.findOneAndUpdate({roleId, id },{$setOnInsert: setOnInsert, $inc: { count }}, {new: true, upsert: true}).lean(lean);
|
||||
const equips: ItemType = await ItemModel.findOneAndUpdate({roleId, id },{$setOnInsert: setOnInsert, $inc: { count }}, {new: true, upsert: true}).lean(lean);
|
||||
return equips;
|
||||
}
|
||||
|
||||
public static async decreaseItems(roleId: string, items: Array<{id: number, count: number}>, lean = true) {
|
||||
let updateItems = new Array<{id: number, count: number}>(), hasError: boolean = false, result = new Array();
|
||||
for(let {id, count} of items) {
|
||||
const rec = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||
const rec: ItemType = await ItemModel.findOneAndUpdate({roleId, id, count: {$gte: count} },{ $inc: { count: -1 * count }}, {new: true}).lean(lean);
|
||||
if(rec) {
|
||||
result.push(rec);
|
||||
updateItems.push({id, count});
|
||||
@@ -69,10 +69,12 @@ export default class Item extends BaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
public static async deleteAccount(roleId: string, lean = true) {
|
||||
let result = await ItemModel.deleteMany({roleId}).lean(lean);
|
||||
return result||{};
|
||||
public static async deleteAccount(roleId: string) {
|
||||
let result = await ItemModel.deleteMany({roleId});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export const ItemModel = getModelForClass(Item);
|
||||
|
||||
export interface ItemType extends Pick<DocumentType<Item>, keyof Item>{};
|
||||
Reference in New Issue
Block a user