本文整理汇总了TypeScript中typeorm.Repository.findOne方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Repository.findOne方法的具体用法?TypeScript Repository.findOne怎么用?TypeScript Repository.findOne使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类typeorm.Repository
的用法示例。
在下文中一共展示了Repository.findOne方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: updateUser
public static async updateUser (ctx: BaseContext) {
// get a user repository to perform operations with user
const userRepository: Repository<User> = getManager().getRepository(User);
// check if a user with the specified id exists
if (await userRepository.findOne(ctx.params.id)) {
// update the user by specified id
// build up entity user to be updated
const userToBeUpdated: User = new User();
userToBeUpdated.id = +ctx.params.id;
userToBeUpdated.name = ctx.request.body.name;
userToBeUpdated.email = ctx.request.body.email;
// validate user entity
const errors: ValidationError[] = await validate(userToBeUpdated); // errors is an array of validation errors
if (errors.length > 0) {
// return bad request status code and errors array
ctx.status = 400;
ctx.body = errors;
} else {
// save the user contained in the PUT body
const user = await userRepository.save(userToBeUpdated);
// return created status code and updated user
ctx.status = 201;
ctx.body = user;
}
} else {
// return a BAD REQUEST status code and error message
ctx.status = 400;
ctx.body = 'The user you are trying to update doesn\'t exist in the db';
}
}
示例2: removeById
async removeById(req: { id: any }): Promise<T> {
let x = await this.repo.findOne(req.id)
if (x == null)
return null;
return await this.repo.remove(x);
}
示例3: findOneById
findOneById(req: { id: any, options?: FindOneOptions<T> }): Promise<T | undefined> {
return this.repo.findOne(req.id, req.options);
}