Skip to content

Commit

Permalink
feat(Model): Ability to pass TypeORM Entity options (goldcaddy77#282)
Browse files Browse the repository at this point in the history
* feat: Ability to pass TypeORM Entity options

* Add ability to pass TypeGraphQL options

* Update README

* Update README
  • Loading branch information
jadenlemmon authored and goldcaddy77 committed Dec 18, 2019
1 parent d2ed3e6 commit 493c1b2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
15 changes: 11 additions & 4 deletions src/decorators/Model.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
];

Expand Down

0 comments on commit 493c1b2

Please sign in to comment.