Skip to content

Instantly share code, notes, and snippets.

@umar-muneer
Last active December 5, 2022 06:11
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 umar-muneer/628f2ee30093f1ba8d7427ed2b5788f6 to your computer and use it in GitHub Desktop.
Save umar-muneer/628f2ee30093f1ba8d7427ed2b5788f6 to your computer and use it in GitHub Desktop.
Mikro-orm Many to Many
@Entity({
tableName: 'folders',
})
export class FolderEntity {
@ManyToMany({
entity: () => Project,
pivotEntity: () => VisionProjectsFolderEntity,
joinColumn: 'folder__id', // this property is important to provide, it is the column where we will go and query the pivot table
})
projects = new Collection<Project>(this);
}
@Entity({
tableName: 'projects',
})
export class ProjectEntity {
// defining this side of the relationship is optional, only if we need to retrieve folders through projects
@ManyToMany({
entity: () => Folder,
pivotEntity: () => VisionProjectsFolderEntity,
joinColumn: 'project__id', // this property is important to provide, it is the column where we will go and query the pivot table
})
projects = new Collection<Project>(this);
}
@Entity({
tableName: 'projects_folders',
})
export class ProjectsFoldersEntity extends TransectBaseEntity {
@ManyToOne({ entity: () => FolderEntity, joinColumn: 'folder__id' })
folder!: FolderEntity;
@ManyToOne({ entity: () => Project, joinColumn: 'project__id' })
project!: Project;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment