diff --git a/README.md b/README.md index 2dfcb6aa..c0a68536 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,12 @@ Note that the examples in the [examples](./examples/README.md) folder use relati A model represents both a GraphQL type and a DB table. Warthog exposes a [BaseModel](https://github.com/goldcaddy77/warthog/blob/master/src/core/BaseModel.ts) class that provides the following columns for free: `id`, `createdAt`, `createdById`, `updatedAt`, `updatedById`, `deletedAt`, `deletedById`, `version`. If you use BaseModel in conjunction with BaseService (see below), all of these columns will be updated as you'd expect. The Warthog server will find all models that match the following glob - `'/**/*.model.ts'`. Ex: `user.model.ts` +Custom [TypeORM](https://github.com/typeorm/typeorm/blob/master/docs/decorator-reference.md#entity) and [TypeGraphQL](https://typegraphql.ml/docs/types-and-fields.html) options may be passed into the `Model` decorator using the following signature. + +```javascript +@Model({ api: { description: 'Custom description' }, db: { name: 'customtablename' } }) +``` + #### Resolvers A Warthog resolver exposes queries (reading data) and mutations (writing data). They interact with the DB through `services` (described below) and typically make use of a bunch of auto-generated TypeScript types in the `generated` folder for things like sorting and filtering. Warthog will find all resolvers that match the following glob - `'/**/*.resolver.ts'`. Ex: `user.resolver.ts` diff --git a/src/decorators/Model.ts b/src/decorators/Model.ts index 286a4e0f..6467a04b 100644 --- a/src/decorators/Model.ts +++ b/src/decorators/Model.ts @@ -1,13 +1,20 @@ const caller = require('caller'); // eslint-disable-line @typescript-eslint/no-var-requires import * as path from 'path'; import { ObjectType } from 'type-graphql'; -import { Entity } from 'typeorm'; +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/'; -export function Model() { +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(); @@ -22,8 +29,8 @@ export function Model() { }; const factories = [ - Entity() as ClassDecoratorFactory, - ObjectType() as ClassDecoratorFactory, + Entity(db) as ClassDecoratorFactory, + ObjectType(api) as ClassDecoratorFactory, registerModelWithWarthog as ClassDecoratorFactory ];