Skip to content
Permalink
Browse files

feat(Model): Ability to pass TypeORM Entity options (#282)

* feat: Ability to pass TypeORM Entity options

* Add ability to pass TypeGraphQL options

* Update README

* Update README
  • Loading branch information
jlemm45 authored and goldcaddy77 committed Dec 18, 2019
1 parent d2ed3e6 commit 493c1b2f98bc7e47078d70af52fc2156773d3ce8
Showing with 17 additions and 4 deletions.
  1. +6 −0 README.md
  2. +11 −4 src/decorators/Model.ts
@@ -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`
@@ -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
];

0 comments on commit 493c1b2

Please sign in to comment.
You can’t perform that action at this time.