Skip to content

Instantly share code, notes, and snippets.

@umutyerebakmaz
Created January 11, 2024 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umutyerebakmaz/d49442f0aa70a43878131972fc764944 to your computer and use it in GitHub Desktop.
Save umutyerebakmaz/d49442f0aa70a43878131972fc764944 to your computer and use it in GitHub Desktop.
TypeORM @manytomany, establish a two-sided `CASCADE` structure for the ManyToMany relationship.
// You can set up the structure without creating this entity, but when the project grows,
// you will need the entity for leftJoin.
import { BaseEntity, Entity, PrimaryColumn } from 'typeorm';
@Entity()
export class RequestUser extends BaseEntity {
@PrimaryColumn('uuid')
requestId: string;
@PrimaryColumn('uuid')
userId: string;
}
@ManyToMany(() => User, user => user.requests, { onDelete: 'CASCADE' })
@JoinTable({ name: 'request_user' })
@Field(() => [User], { nullable: true })
users: Promise<User[]>;
@ManyToMany(() => Request, request => request.users, { onDelete: 'CASCADE' })
@Field(() => [Request], { nullable: true })
requests: Promise<Request[]>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment