增加自定义的insertOrUpdate方法
This commit is contained in:
parent
2254c477bd
commit
cd26c1270f
@ -30,7 +30,7 @@ class GameUserController extends BaseController {
|
||||
obj.shop = shop.id
|
||||
Object.assign(obj, { $addToSet: { shops: shop.id } })
|
||||
}
|
||||
let user = await GameUser.findOneAndUpdate({ accountId }, obj, { upsert: true, new: true })
|
||||
let user = await GameUser.insertOrUpdate({ accountId }, obj)
|
||||
const token = await res.jwtSign({ id: user.id, accountId })
|
||||
return { token }
|
||||
}
|
||||
|
@ -95,11 +95,7 @@ class ShopController extends BaseController {
|
||||
if (!shop) {
|
||||
throw new ZError(11, '未找到对应的店铺')
|
||||
}
|
||||
await GameUser.findOneAndUpdate(
|
||||
{ accountId },
|
||||
{ shop: shop.id, loginParams: sid.split('_').slice(1) },
|
||||
{ upsert: true, new: true },
|
||||
)
|
||||
await GameUser.insertOrUpdate({ accountId }, { shop: shop.id, loginParams: sid.split('_').slice(1) })
|
||||
rspData.gameCfg = {}
|
||||
if (!!shop.gameInfo) {
|
||||
let gameInfo = shop.gameInfo
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { FindOrCreate } from '@typegoose/typegoose/lib/defaultClasses'
|
||||
import { checkJson } from '../decorators/nojson'
|
||||
import { plugin } from '@typegoose/typegoose'
|
||||
import { plugin, ReturnModelType } from '@typegoose/typegoose'
|
||||
|
||||
// @ts-ignore
|
||||
import findOrCreate from 'mongoose-findorcreate'
|
||||
import { Connection } from 'mongoose'
|
||||
import { ObjectId } from 'bson'
|
||||
import { isTrue } from '../utils/string.util'
|
||||
import { AnyParamConstructor } from '@typegoose/typegoose/lib/types'
|
||||
|
||||
const jsonExcludeKeys = ['updatedAt', '__v']
|
||||
const saveExcludeKeys = ['createdAt', 'updatedAt', '__v', '_id']
|
||||
@ -23,6 +24,15 @@ export abstract class BaseModule extends FindOrCreate {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入或更新
|
||||
* @param condition
|
||||
* @param data
|
||||
*/
|
||||
public static async insertOrUpdate<T>(this: ReturnModelType<AnyParamConstructor<T>>, condition: any, data: any) {
|
||||
return this.findOneAndUpdate(condition, data, { upsert: true, new: true })
|
||||
}
|
||||
|
||||
public toJson() {
|
||||
let result: any = {}
|
||||
// @ts-ignore
|
||||
|
Loading…
x
Reference in New Issue
Block a user