typeorm#DeleteResult TypeScript Examples

The following examples show how to use typeorm#DeleteResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CashBudgetTransactionRepository.ts    From cashcash-desktop with MIT License 6 votes vote down vote up
async deleteCustom(
        parameters: TransactionParameters = simpleTransactionParameters,
    ): Promise<DeleteResult> {
        let selectQb = this.createQueryBuilder('t').select('id');
        selectQb = this.createWhereClause(selectQb, parameters);

        const qb = this.createQueryBuilder().delete().andWhere(`id IN ( ${selectQb.getQuery()} )`);

        return await qb.execute();
    }
Example #2
Source File: CashTransactionRepository.ts    From cashcash-desktop with MIT License 6 votes vote down vote up
async deleteCustom(
        parameters: TransactionParameters = simpleTransactionParameters,
    ): Promise<DeleteResult> {
        // There is a Bug in TypeORM: the alias doesn't work with delete
        let qb = this.createQueryBuilder().delete();

        qb = this.createWhereClause(qb, parameters, 'cash_transaction');

        return await qb.execute();
    }
Example #3
Source File: pledge.controller.ts    From The-TypeScript-Workshop with MIT License 5 votes vote down vote up
@Delete(':id')
  deletePledge(@Param('id') id: number): Promise<DeleteResult> {
    return this.pledgeService.delete(id);
  }
Example #4
Source File: pledge.service.ts    From The-TypeScript-Workshop with MIT License 5 votes vote down vote up
delete(id: number): Promise<DeleteResult> {
    return this.pledgeRepository.delete(id);
  }
Example #5
Source File: CashPreferencesRepository.ts    From cashcash-desktop with MIT License 5 votes vote down vote up
async deleteAll(): Promise<DeleteResult> {
        return await this.createQueryBuilder().delete().execute();
    }
Example #6
Source File: CashSplitSumRepository.ts    From cashcash-desktop with MIT License 5 votes vote down vote up
async deleteAll(): Promise<DeleteResult> {
        return await this.createQueryBuilder().delete().execute();
    }
Example #7
Source File: access-token.repository.ts    From nestjs-oauth2-server-module with MIT License 5 votes vote down vote up
async delete(accessToken: AccessTokenEntity): Promise<DeleteResult> {
        return await this.repository.delete(accessToken.id);
    }
Example #8
Source File: access-token.repository.ts    From nestjs-oauth2-server-module with MIT License 5 votes vote down vote up
async deleteById(id: string): Promise<DeleteResult> {
        return await this.repository.delete(id);
    }
Example #9
Source File: user.service.stub.spec.ts    From nestjs-api-example with MIT License 4 votes vote down vote up
describe('UserService (Stub)', () => {
  let userService: UserService;
  let userRepository: UserRepository;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [UserService, UserRepository],
    }).compile();

    userService = module.get<UserService>(UserService);
    userRepository = module.get<UserRepository>(UserRepository);
  });

  describe('createUser', () => {
    it('유저를 생성하고, 생성한 유저를 반환한다', async () => {
      const firstName = '재혁';
      const lastName = '김';
      const requestDto: UserCreateRequestDto = {
        firstName: firstName,
        lastName: lastName,
      };
      const createdUserEntity = User.of(requestDto);
      const savedUser = User.of({
        id: 1,
        firstName: firstName,
        lastName: lastName,
        isActive: true,
      });
      const userRepositoryCreateSpy = jest
        .spyOn(userRepository, 'create')
        .mockReturnValue(createdUserEntity);
      const userRepositorySaveSpy = jest
        .spyOn(userRepository, 'save')
        .mockResolvedValue(savedUser);

      const result = await userService.createUser(requestDto);

      expect(userRepositoryCreateSpy).toBeCalledWith(requestDto);
      expect(userRepositorySaveSpy).toBeCalledWith(createdUserEntity);
      expect(result).toBe(savedUser);
    });
  });

  describe('findAll', () => {
    it('생성된 모든 유저 목록을 반환한다', async () => {
      const existingUserList = [
        User.of({
          id: 1,
          firstName: '재혁',
          lastName: '김',
          isActive: true,
        }),
        User.of({
          id: 2,
          firstName: '길동',
          lastName: '홍',
          isActive: true,
        }),
      ];
      const userRepositoryFindSpy = jest
        .spyOn(userRepository, 'find')
        .mockResolvedValue(existingUserList);

      const result = await userService.findAll();

      expect(userRepositoryFindSpy).toBeCalled();
      expect(result).toStrictEqual(existingUserList);
    });
  });

  describe('findById', () => {
    it('생성되지 않은 유저의 id가 주어진다면 유저를 찾을 수 없다는 예외를 던진다', async () => {
      const userId = 1;
      jest.spyOn(userRepository, 'findOne').mockResolvedValue(undefined);

      const result = async () => {
        await userService.findById(userId);
      };

      await expect(result).rejects.toThrowError(
        new NotFoundException('유저 정보를 찾을 수 없습니다.'),
      );
    });

    it('생성된 유저의 id가 주어진다면 해당 id의 유저를 반환한다', async () => {
      const userId = 1;
      const existingUser = User.of({
        id: userId,
        firstName: '재혁',
        lastName: '김',
        isActive: true,
      });
      const userRepositoryFindOneSpy = jest
        .spyOn(userRepository, 'findOne')
        .mockResolvedValue(existingUser);

      const result = await userService.findById(userId);

      expect(userRepositoryFindOneSpy).toHaveBeenCalledWith({
        where: { id: userId },
      });
      expect(result.firstName).toBe('재혁');
      expect(result.lastName).toBe('김');
    });
  });

  describe('updateUser', () => {
    it('생성되지 않은 유저의 id가 주어진다면 유저를 찾을 수 없다는 예외를 던진다', async () => {
      const userId = 1;
      const requestDto: UserUpdateRequestDto = {
        firstName: '길동',
        lastName: '김',
        isActive: false,
      };
      jest.spyOn(userRepository, 'findOne').mockResolvedValue(undefined);

      const result = async () => {
        await userService.updateUser(userId, requestDto);
      };

      await expect(result).rejects.toThrowError(
        new NotFoundException('유저 정보를 찾을 수 없습니다.'),
      );
    });

    it('생성된 유저의 id가 주어진다면 해당 id의 유저를 수정하고 수정된 유저를 반환한다', async () => {
      const userId = 1;
      const requestDto: UserUpdateRequestDto = {
        firstName: '길동',
        lastName: '김',
        isActive: false,
      };
      const existingUser = User.of({
        id: userId,
        firstName: '재혁',
        lastName: '김',
        isActive: true,
      });
      const savedUser = User.of({
        id: userId,
        ...requestDto,
      });
      const userRepositoryFindOneSpy = jest
        .spyOn(userRepository, 'findOne')
        .mockResolvedValue(existingUser);
      const userRepositorySaveSpy = jest
        .spyOn(userRepository, 'save')
        .mockResolvedValue(savedUser);

      const result = await userService.updateUser(userId, requestDto);

      expect(userRepositoryFindOneSpy).toHaveBeenCalledWith({
        where: { id: userId },
      });
      expect(userRepositorySaveSpy).toHaveBeenCalledWith(savedUser);
      expect(result).toBe(savedUser);
    });
  });

  describe('deleteUser', () => {
    it('생성된 유저의 id가 주어진다면 생성된 유저를 삭제한다', async () => {
      const userId = 1;
      const userRepositoryDeleteSpy = jest
        .spyOn(userRepository, 'delete')
        .mockResolvedValue({} as DeleteResult);

      const result = await userService.deleteUser(userId);

      expect(userRepositoryDeleteSpy).toHaveBeenCalledWith(userId);
      expect(result).toBeUndefined();
    });
  });
});