Skip to content

Commit

Permalink
feat(many-to-many): Adds many to many decorators (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 15, 2019
1 parent 91f60d3 commit 938d602
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/decorators/ManyToMany.ts
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);
}
17 changes: 17 additions & 0 deletions src/decorators/ManyToManyJoin.ts
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);
}
2 changes: 2 additions & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './EmailField';
export * from './EnumField';
export * from './ManyToMany';
export * from './ManyToManyJoin';
export * from './Model';
export * from './StringField';
export * from './ForeignKeyField';
Expand Down

0 comments on commit 938d602

Please sign in to comment.