本文整理匯總了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(', '));
}