-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(many-to-many): Adds many to many decorators (#26)
- Loading branch information
1 parent
91f60d3
commit 938d602
Showing
3 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Field } from 'type-graphql'; | ||
import { ManyToMany as TypeORMManyToMany } from 'typeorm'; | ||
|
||
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; | ||
|
||
export function ManyToMany(parentType: any, joinFunc: any, options: any = {}): any { | ||
const factories = [ | ||
Field(() => [parentType()], { nullable: true, ...options }) as MethodDecoratorFactory, | ||
TypeORMManyToMany(parentType, joinFunc, options) as MethodDecoratorFactory | ||
]; | ||
|
||
return composeMethodDecorators(...factories); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Field } from 'type-graphql'; | ||
import { JoinTable, ManyToMany as TypeORMManyToMany } from 'typeorm'; | ||
|
||
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; | ||
|
||
// Note: for many to many relationships, you need to set one item as the "JoinTable" | ||
// therefore, we have 2 separate decorators. Just make sure to add one to one table and | ||
// One to the other in the relationship | ||
export function ManyToManyJoin(parentType: any, joinFunc: any, options: any = {}): any { | ||
const factories = [ | ||
JoinTable() as MethodDecoratorFactory, | ||
Field(() => [parentType()], { nullable: true, ...options }) as MethodDecoratorFactory, | ||
TypeORMManyToMany(parentType, joinFunc, options) as MethodDecoratorFactory | ||
]; | ||
|
||
return composeMethodDecorators(...factories); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters