Skip to content

Commit

Permalink
feat(ormconfig): generate ormconfig (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Jan 26, 2019
1 parent 89eb4b8 commit a470b2d
Show file tree
Hide file tree
Showing 33 changed files with 6,682 additions and 215 deletions.
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ yarn add warthog

## Usage

Check out the [warthog-starter](https://github.com/goldcaddy77/warthog-starter) repo to see how to use Warthog in a project. There are also a bunch of examples in the [examples](./examples/README.md) folder. Note that these use relative import paths to call into Warthog instead of pulling from NPM.
Check out the [warthog-starter](https://github.com/goldcaddy77/warthog-starter) repo to see how to use Warthog in a project. There are also a bunch of examples in the [examples](./examples/README.md) folder.

Note that the examples in the [examples](./examples/README.md) folder use relative import paths to call into Warthog. In your projects, you won't need to set this config value. See [warthog-starter](https://github.com/goldcaddy77/warthog-starter/blob/master/src/app.ts) for how you should set this up.

### 1. Create a Model

Expand Down Expand Up @@ -73,16 +75,27 @@ export class UserResolver extends BaseResolver<User> {
}
```

### 3. Run your server
### 3. Add config to .env file



```env
APP_HOST=localhost
APP_PORT=4100
TYPEORM_DATABASE=example2
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=
```

### 4. Run your server

```typescript

import 'reflect-metadata';
import { Container } from 'typedi';
import { Server } from 'warthog';

async function bootstrap() {
const server = new Server({ container: Container });
const server = new Server();
return server.start();
}

Expand Down Expand Up @@ -161,11 +174,15 @@ Notice how we've only added a single field on the model and you get pagination,

## Config

| value | ENV var | option name | default |
All config is driven by environment variables. Most options can also be set by setting the value when creating your `Server` instance.

| variable | value | config option name | default |
| --- | --- | --- | --- |
| host | APP_HOST | appOptions.host | no default |
| app port | APP_PORT | appOptions.port | 4000 |
| generated folder | _none_ | appOptions.generatedFolder | _current-dir_ + `generated` |
| APP_HOST | App server host | appOptions.host | _none_ |
| APP_PORT | App server port | appOptions.port | 4000 |
| TYPEORM_DATABASE | DB name | _none_ | _none_ |
| TYPEORM_USERNAME | DB username | _none_ | _none_ |
| TYPEORM_PASSWORD | DB password | _none_ | _none_ |

## Intentionally Opinionated

Expand Down
2 changes: 0 additions & 2 deletions examples/1-simple-model/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'reflect-metadata';
import { Container } from 'typedi';

import { Server } from '../../../src/';

async function bootstrap() {
const server = new Server({
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
});

Expand Down
3 changes: 1 addition & 2 deletions examples/2-complex-example/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example1",
"name": "example2",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
Expand All @@ -13,7 +13,6 @@
"start": "yarn start:ts",
"start:debug": "yarn start:ts --inspect",
"start:ts": "ts-node --type-check src/index.ts",
"typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -f ./ormconfig",
"test": "dotenv -- jest --detectOpenHandles --verbose --coverage",
"test:watch": "dotenv -- jest --watch",
"watch:ts": "nodemon -e ts,graphql -x ts-node --type-check src/index.ts"
Expand Down
5 changes: 0 additions & 5 deletions examples/2-complex-example/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'reflect-metadata';

import { Container } from 'typedi';

import { BaseContext, Server } from '../../../src/';

// import { User } from './modules/user/user.model';

interface Context extends BaseContext {
user: {
email: string;
Expand All @@ -17,7 +13,6 @@ interface Context extends BaseContext {
export function getServer(AppOptions = {}, dbOptions = {}) {
return new Server<Context>(
{
container: Container,
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: request => {
return {
Expand Down
2 changes: 1 addition & 1 deletion examples/3-one-to-many-relationship/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example1",
"name": "example3",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
Expand Down
2 changes: 0 additions & 2 deletions examples/3-one-to-many-relationship/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'reflect-metadata';
import { Container } from 'typedi';

import { Server } from '../../../src/';

async function bootstrap() {
const server = new Server({
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
});

Expand Down
2 changes: 1 addition & 1 deletion examples/4-many-to-many-relationship/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "example1",
"name": "example4",
"version": "0.0.0",
"scripts": {
"//": "make sure to install top-level packages, too",
Expand Down
2 changes: 0 additions & 2 deletions examples/4-many-to-many-relationship/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'reflect-metadata';
import { Container } from 'typedi';

import { Server } from '../../../src/';

async function bootstrap() {
const server = new Server({
container: Container,
warthogImportPath: '../../../src' // Path written in generated classes
});

Expand Down
5 changes: 5 additions & 0 deletions examples/5-migrations/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
APP_HOST=localhost
APP_PORT=4100
TYPEORM_DATABASE=example5
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=
1 change: 1 addition & 0 deletions examples/5-migrations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
generated/*
21 changes: 21 additions & 0 deletions examples/5-migrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Example 1 - User Model

## Setup

Run `yarn bootstrap && yarn start`

## Bootstrapping the App

Running `yarn bootstrap` will do the following:

- Install packages
- Create the example DB
- Generate code in `generated` folder

## Generate DB schema migration

To automatically generate a schema migration file, run `yarn db:migration:generate`. The migration will be put in the `db/migrations` folder

## Run the DB migration

To run the DB migration, run `yarn db:migration:run`
16 changes: 16 additions & 0 deletions examples/5-migrations/examples.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query {
users(orderBy: createdAt_DESC) {
id
lastName
createdAt
}
}

mutation {
createUser(data: { firstName: "Test", email: "test@fakeemail.com" }) {
id
firstName
lastName
createdAt
}
}
138 changes: 0 additions & 138 deletions examples/5-migrations/generated/classes.ts

This file was deleted.

1 change: 0 additions & 1 deletion examples/5-migrations/generated/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions examples/5-migrations/generated/ormconfig.ts

This file was deleted.

4 changes: 4 additions & 0 deletions examples/5-migrations/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"verbose": true,
"ignore": ["dist/*", "generated/*"]
}
Loading

0 comments on commit a470b2d

Please sign in to comment.