当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Repository.save方法代码示例

本文整理汇总了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;
        }
    }
开发者ID:ryanwild,项目名称:node-typescript-koa-rest,代码行数:25,代码来源:user.ts

示例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';
        }

    }
开发者ID:ryanwild,项目名称:node-typescript-koa-rest,代码行数:36,代码来源:user.ts


注:本文中的typeorm.Repository.save方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。