forked from goldcaddy77/warthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.ts
38 lines (31 loc) · 1.49 KB
/
Model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const caller = require('caller'); // eslint-disable-line @typescript-eslint/no-var-requires
import * as path from 'path';
import { ObjectType } from 'type-graphql';
import { ObjectOptions } from 'type-graphql/dist/decorators/ObjectType.d';
import { Entity, EntityOptions } from 'typeorm';
import { ClassType } from '../core';
import { getMetadataStorage } from '../metadata';
import { ClassDecoratorFactory, composeClassDecorators, generatedFolderPath } from '../utils/';
interface ModelOptions {
api?: ObjectOptions;
db?: EntityOptions;
}
// Allow default TypeORM and TypeGraphQL options to be used
export function Model({ api = {}, db = {} }: ModelOptions = {}) {
// In order to use the enums in the generated classes file, we need to
// save their locations and import them in the generated file
const modelFileName = caller();
// Use relative paths when linking source files so that we can check the generated code in
// and it will work in any directory structure
const relativeFilePath = path.relative(generatedFolderPath(), modelFileName);
const registerModelWithWarthog = (target: ClassType): void => {
// Save off where the model is located so that we can import it in the generated classes
getMetadataStorage().addModel(target.name, target, relativeFilePath);
};
const factories = [
Entity(db) as ClassDecoratorFactory,
ObjectType(api) as ClassDecoratorFactory,
registerModelWithWarthog as ClassDecoratorFactory
];
return composeClassDecorators(...factories);
}