本文整理汇总了TypeScript中mongoose.Model类的典型用法代码示例。如果您正苦于以下问题:TypeScript Model类的具体用法?TypeScript Model怎么用?TypeScript Model使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Model类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: update
export function update(req: Request, res: Response): void {
const [creator, creatorRef] = UTIL.getLoginedUser(req)
let log = {
creator,
creatorRef,
action: CONST.USER_ACTIONS.COMMON.UPDATE,
ua: req.body.ua || req.ua
},
update = UTIL.sanitizeObject(req.body,
CONST.USER_UNUPDATABLE_FIELDS,
true
)
const UserModel: Model<IUser> = UTIL.getModelFromName(creatorRef)
UserModel
.findByIdAndUpdate(creator, update, {new: true})
.then((user: IUser) => {
if (user) {
res.status(200).json(UTIL.getSignedUser(user))
new Logger(log)
} else {
res.status(404).send()
}
})
.catch((err: Error) => {
new Err(res, err, log)
})
}
示例2: retract
export function retract(req: Request, res: Response): void {
const [creator, creatorRef] = UTIL.getLoginedUser(req),
target: Schema.Types.ObjectId = req.body.id,
targetRef: string = req.body.type,
DataModel: Model<IContent> = UTIL.getModelFromName(targetRef)
let log: any = {
creator,
creatorRef,
target,
targetRef,
action: CONST.USER_ACTIONS.CONSUMER.RETRACT,
ua: req.body.ua || req.ua
}
DataModel
.findOne({
creator,
_id: target
})
.then((data: IContent) => {
if (data) {
if (data.status === CONST.STATUSES.CONTENT.EDITING) {
res.status(422).json({ code: ERRORS.CONTENT.CONTENT_CANNOT_BE_RETRACTED })
return null
} else {
data.status = CONST.STATUSES.CONTENT.EDITING
return data.save()
}
}
res.status(404).send({ code: ERRORS.CONTENT.NO_ELIGIBLE_CONTENT_FOUND })
return null
})
.then((data: IContent) => {
res.status(200).json(data)
new Logger(log)
// retract submission/approval process if required
if (CONFIG.POST_REQUIRES_APPROVAL) {
return Process
.findOneAndUpdate({
creator: log.creator,
creatorRef: log.creatorRef,
target: log.target,
targetRef: log.targetRef,
type: CONST.PROCESS_TYPES.APPROVAL
}, {
status: CONST.STATUSES.PROCESS.CANCELLED,
completed: UTIL.getTimestamp()
})
.sort({_id: -1})
} else {
return null
}
})
.catch((err: Error) => {
new Err(res, err, log)
})
}
示例3: list
export function list(req: Request, res: Response): void {
let params = UTIL.assembleSearchParams(req, {
status: CONST.STATUSES.CONSUMER.ACTIVE
}, 'handle')
const UserModel: Model<IUser> = UTIL.getModelFromName(req.routeVar.userType)
UserModel
.find(params.query)
.skip(params.skip)
.limit(params.limit)
.sort(params.sort)
.lean()
.exec()
.then((data: IUser[]) => {
if (data) {
res.status(200).json(data)
} else {
res.status(404).send()
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
}
示例4: remove
export function remove(req: Request, res: Response): void {
const [creator, creatorRef] = UTIL.getLoginedUser(req)
let log = {
creator,
creatorRef,
action: CONST.USER_ACTIONS.COMMON.DELETE,
ua: req.body.ua || req.ua
}
const UserModel: Model<IUser> = UTIL.getModelFromName(creatorRef)
UserModel
.findByIdAndRemove(creator)
.then((user: IUser) => {
if (user) {
res.status(204).end()
new Logger(log)
}
res.status(404).send()
})
.catch((err: Error) => {
new Err(res, err, log)
})
}
示例5: list
export function list(req: Request, res: Response): void {
let handle: string = UTIL.getRequestParam(req, req.routeVar.userHandleKey)
if (handle && handle.length > 0) {
const UserModel: Model<IUser> = UTIL.getModelFromName(req.routeVar.creatorType)
UserModel
.findOne({handle})
.select('_id')
.lean()
.then((user: IUser) => {
if (user) {
search(req, res, user._id)
} else {
res.status(404).json({ code: ERRORS.LOGIN.USER_NOT_FOUND })
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
} else {
search(req, res)
}
}
示例6:
const search = (req: Request, res: Response, creator?: Schema.Types.ObjectId) => {
let params = UTIL.assembleSearchParams(req, {
status: CONST.STATUSES.CONTENT.APPROVED
}, req.routeVar.keywordFields)
if (creator) {
params.query.creator = creator
}
const DataModel: Model<IContent> = UTIL.getModelFromName(req.routeVar.contentType)
DataModel
.find(params.query)
.skip(params.skip)
.limit(params.limit)
.sort(params.sort)
.populate({
path: 'creator',
model: CONST.USER_TYPES.CONSUMER,
select: CONST.PUBLIC_CONSUMER_INFO_LIST
})
.lean()
.exec()
.then((data: IContent[]) => {
if (data) {
res.status(200).json(data)
} else {
res.status(404).send()
}
})
.catch((err: Error) => {
res.status(res.statusCode).send()
console.log(err)
})
}
示例7: reject
return new Promise<T>((resolve: Function, reject: Function) => {
const txModelOptions = this.obtainTransactionModelOptions(newOptions);
const authorizationResponse = this.isSearchAuthorized(txModelOptions);
if (!authorizationResponse.isAuthorized) {
return reject(new Error(authorizationResponse.errorMessage));
}
this.Model.findById(id, txModelOptions.projection, { lean: true }).populate(txModelOptions.population)
.exec((err: Error, foundObj: T) => {
if (err) {
return reject(err);
}
if (ObjectUtil.isBlank(foundObj)) {
return reject(new Error('Object not found'));
}
if (txModelOptions.validatePostSearchAuthData) {
const authorizationResponse = this.validateAuthDataPostSearch(txModelOptions, foundObj);
if (!authorizationResponse.isAuthorized) {
return reject(new Error(authorizationResponse.errorMessage));
}
}
resolve(foundObj);
});
});
示例8: view
public async view(id) : Promise<Document> {
if (!Types.ObjectId.isValid(id)) {
throw new Error('Unknown Record');
}
let paths = this.getPathsToPopulate();
return await this.Model.findById(id).populate(paths.join(', '));
}