Skip to content

Instantly share code, notes, and snippets.

@victorcrbt
Last active August 8, 2025 06:36
Show Gist options
  • Select an option

  • Save victorcrbt/d6f376635bfb65484936d75e7e6959ac to your computer and use it in GitHub Desktop.

Select an option

Save victorcrbt/d6f376635bfb65484936d75e7e6959ac to your computer and use it in GitHub Desktop.

Revisions

  1. victorcrbt revised this gist May 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Client.ts
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ export default class Client {
    @PrimaryGeneratedColumn()
    id: number;

    @Colum()
    @Column()
    name: string;

    @Column()
  2. victorcrbt created this gist May 13, 2020.
    35 changes: 35 additions & 0 deletions Client.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    import { Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable } from 'typeorm';

    import Company from './Company';

    @Entity('clients')
    export default class Client {
    @PrimaryGeneratedColumn()
    id: number;

    @Colum()
    name: string;

    @Column()
    email: string;

    @Column()
    document: string;

    @Column()
    password: string;

    @ManyToMany(() => Company, company => company.clients)
    @JoinTable({
    name: 'clients_companies_relation',
    joinColumn: {
    name: 'client_id',
    referencedColumnName: 'id',
    },
    inverseJoinColumn: {
    name: 'company_id',
    referencedColumnName: 'id',
    }
    })
    companies: Company[];
    }
    21 changes: 21 additions & 0 deletions ClientsRepository.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import { getRepository, Repository } from 'typeorm';

    import Client from '../entities/Client';

    interface IClientsRepository {
    findById(id: number): Promise<Client>;
    }

    export default class ClientsRepositoryimplements IClientsRepository {
    private ormRepository: Repository<Client>;

    constructor() {
    this.ormRepository = getRepository(Client);
    }

    public async findById (id: number): Promise<Client> {
    const client = await this.ormRepository.findOne(id, { relations: ['companies'] });

    return client;
    }
    }
    21 changes: 21 additions & 0 deletions Company.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import { Entity, PrimaryGeneratedColumn, Column, ManyToMany } from 'typeorm';

    import Client from './Client';

    @Entity('companies')
    export default class Company {
    @PrimaryGeneratedColumn()
    id: number;

    @Colum()
    company_name: string;

    @Column()
    document: string;

    @Column()
    phone: string;

    @ManyToMany(() => Client, client => client.companies)
    clients: Client[];
    }