本文整理汇总了TypeScript中typeorm.Repository.save方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Repository.save方法的具体用法?TypeScript Repository.save怎么用?TypeScript Repository.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类typeorm.Repository
的用法示例。
在下文中一共展示了Repository.save方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createUser
public static async createUser (ctx: BaseContext) {
// get a user repository to perform operations with user
const userRepository: Repository<User> = getManager().getRepository(User);
// build up entity user to be saved
const userToBeSaved: User = new User();
userToBeSaved.name = ctx.request.body.name;
userToBeSaved.email = ctx.request.body.email;
// validate user entity
const errors: ValidationError[] = await validate(userToBeSaved); // 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 POST body
const user = await userRepository.save(userToBeSaved);
// return created status code and updated user
ctx.status = 201;
ctx.body = user;
}
}
示例2: 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';
}
}