-
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(relationships): adds OneToMany and ManyToOne (#19)
- Loading branch information
1 parent
bf09179
commit 3fb97f1
Showing
11 changed files
with
110 additions
and
20 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
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
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
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,35 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { Field } from 'type-graphql'; | ||
import { JoinColumn, ManyToOne as TypeORMManyToOne } from 'typeorm'; | ||
|
||
import { StringField } from '../decorators'; | ||
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; | ||
|
||
export function ManyToOne(parentType: any, joinFunc: any, options: any = {}): any { | ||
// Need to grab the class name from within a decorator | ||
let klass: string; | ||
const extractClassName = (target: any): any => { | ||
klass = target.constructor.name; | ||
}; | ||
|
||
// This Decorator creates the foreign key field for the association so that the consumer | ||
// Doesn't need to hand roll this each time by doing somethign like: | ||
// @StringField() | ||
// userId?: ID; | ||
const createForeignKeyField = (target: any, propertyKey: string, descriptor: PropertyDescriptor): any => { | ||
klass = target.constructor.name; | ||
Reflect.defineProperty(target, `${klass}Id`, {}); | ||
StringField()(target, `${propertyKey}Id`, descriptor); | ||
}; | ||
|
||
const factories: MethodDecoratorFactory[] = [ | ||
Field(parentType, { nullable: true, ...options }) as MethodDecoratorFactory, | ||
TypeORMManyToOne(parentType, joinFunc, options) as MethodDecoratorFactory, | ||
JoinColumn() as MethodDecoratorFactory, | ||
createForeignKeyField, | ||
extractClassName // Note: this is actually run first because of now composeMethodDecorators works | ||
]; | ||
|
||
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 |
---|---|---|
@@ -1,14 +1,27 @@ | ||
const caller = require('caller'); | ||
import { ObjectType } from 'type-graphql'; | ||
import { Entity } from 'typeorm'; | ||
|
||
import { getMetadataStorage } from '../metadata'; | ||
import { composeClassDecorators, ClassDecoratorFactory } from '../utils/'; | ||
|
||
interface ModelOptions { | ||
auditTableName?: string; | ||
} | ||
|
||
export function Model(this: any, args: ModelOptions = {}): any { | ||
const factories = [Entity() as ClassDecoratorFactory, ObjectType() as ClassDecoratorFactory]; | ||
const modelFileName = caller(); | ||
|
||
const registerModelWithWarthog = (target: any): any => { | ||
// Save off where the model is located so that we can import it in the generated classes | ||
getMetadataStorage().addModel(target.name, target, modelFileName); | ||
}; | ||
|
||
const factories = [ | ||
Entity() as ClassDecoratorFactory, | ||
ObjectType() as ClassDecoratorFactory, | ||
registerModelWithWarthog as ClassDecoratorFactory | ||
]; | ||
|
||
return composeClassDecorators(...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,13 @@ | ||
import { Field } from 'type-graphql'; | ||
import { OneToMany as TypeORMOneToMany } from 'typeorm'; | ||
|
||
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils'; | ||
|
||
export function OneToMany(parentType: any, joinFunc: any, options: any = {}): any { | ||
const factories = [ | ||
Field(parentType, { nullable: true, ...options }) as MethodDecoratorFactory, | ||
TypeORMOneToMany(parentType, joinFunc) 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
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
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
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
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
I think this change is causing TS6133 errors (declared/unused).