user.service.ts 807 B

1234567891011121314151617181920212223242526
  1. import { Injectable } from '@nestjs/common';
  2. import { InjectConnection } from '@nestjs/typeorm';
  3. import { Connection } from 'typeorm';
  4. import { User } from '../entities/User';
  5. import { Address } from '../entities/Address';
  6. @Injectable()
  7. export class UserService {
  8. constructor(@InjectConnection() private connection: Connection) {}
  9. findAll(): Promise<User[]> {
  10. return this.connection.manager.find(User);
  11. }
  12. findOne(userId: number): Promise<User> {
  13. return this.connection.manager.findOne(User, userId);
  14. }
  15. findAddressesByUserId(userId: number): Promise<Address[]> {
  16. return this.connection
  17. .getRepository(Address)
  18. .createQueryBuilder('address')
  19. .where('address.userId = :id', { id: userId })
  20. .getMany();
  21. }
  22. }