Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ DB_DATABASE="my_database"
DB_SYNCHRONIZE=false
DB_LOGGING=false

#
# GraphQL
#
GRAPHQL_ENABLED=true
GRAPHQL_ROUTE="/graphql"
GRAPHQL_EDITOR=true

#
# Swagger
#
Expand Down
15 changes: 8 additions & 7 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ AUTH_ROUTE="http://localhost:3333/tokeninfo"
#
# DATABASE
#
DB_TYPE="mysql"
DB_HOST="localhost"
DB_PORT=3306
DB_USERNAME="root"
DB_PASSWORD=""
DB_DATABASE="my_database"
DB_SYNCHRONIZE=false
DB_TYPE="sqlite"
DB_DATABASE="./mydb.sql"
DB_LOGGING=false

#
# GraphQL
#
GRAPHQL_ENABLED=true
GRAPHQL_ROUTE="/graphql"

#
# Swagger
#
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ test/**/*.js
test/**/*.js.map
coverage/
!test/preprocessor.js
mydb.sql
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
[Jest](https://facebook.github.io/jest/),
[Swagger](http://swagger.io/),
[validatejs](https://validatejs.org/),
[GraphQL](http://graphql.org/),
[DataLoaders](https://github.com/facebook/dataloader),
by [w3tech](https://github.com/w3tecch)

## Why
Expand All @@ -44,6 +46,8 @@ Try it!! We are happy to hear your feedback or any kind of new features.
- **Easy event dispatching** thanks to [event-dispatch](https://github.com/pleerock/event-dispatch).
- **Fast Database Building** with simple migration from [TypeORM](https://github.com/typeorm/typeorm).
- **Easy Data Seeding** with our own factories.
- **GraphQL** provides as a awesome query language for our api [GraphQL](http://graphql.org/).
- **DataLoaders** helps with performance thanks to caching and batching [DataLoaders](https://github.com/facebook/dataloader).

### Comming soon

Expand Down Expand Up @@ -128,6 +132,7 @@ All script are defined in the package.json file, but the most important ones are
### Tests

- Run the unit tests using `npm start test` (There is also a vscode task for this called `test`).
- Run the integration tests using `npm start test:integration`.
- Run the e2e tests using `npm start test:e2e` and don't forget to start your application and your [Auth0 Mock Server](https://github.com/hirsch88/auth0-mock-server).

### Running in dev mode
Expand Down Expand Up @@ -165,9 +170,11 @@ The swagger and the monitor route can be altered in the `.env` file.
| Route | Description |
| -------------- | ----------- |
| **/api** | Shows us the name, description and the version of the package.json |
| **/graphql** | Route to the graphql editor or your query/mutations requests |
| **/swagger** | This is the Swagger UI with our API documentation |
| **/monitor** | Shows a small monitor page for the server |
| **/api/users** | Example entity endpoint |
| **/api/pets** | Example entity endpoint |

## Project Structure

Expand All @@ -187,6 +194,9 @@ The swagger and the monitor route can be altered in the `.env` file.
| **src/api/services/** | Service layer |
| **src/api/subscribers/** | Event subscribers |
| **src/api/validators/** | Custom validators, which can be used in the request classes |
| **src/api/queries/** | GraphQL queries |
| **src/api/mutations/** | GraphQL mutations |
| **src/api/types/** | GraphQL types |
| **src/api/** swagger.json | Swagger documentation |
| **src/auth/** | Authentication checkers and services |
| **src/core/** | The core features like logger and env variables |
Expand All @@ -199,9 +209,12 @@ The swagger and the monitor route can be altered in the `.env` file.
| **src/types/** *.d.ts | Custom type definitions and files that aren't on DefinitelyTyped |
| **test** | Tests |
| **test/e2e/** *.test.ts | End-2-End tests (like e2e) |
| **test/integration/** *.test.ts | Integration test with SQLite3 |
| **test/unit/** *.test.ts | Unit tests |
| .env.example | Environment configurations |
| .env.test | Test environment configurations |
| ormconfig.json | TypeORM configuration for the database. Used by seeds and the migration. (generated file) |
| mydb.sql | SQLite database for integration tests. Ignored by git and only available after integration tests |

## Logging

Expand Down Expand Up @@ -378,6 +391,9 @@ npm start db.seed
| [Auth0 API Documentation](https://auth0.com/docs/api/management/v2) | Authentification service |
| [Jest](http://facebook.github.io/jest/) | Delightful JavaScript Testing Library for unit and e2e tests |
| [swagger Documentation](http://swagger.io/) | API Tool to describe and document your api. |
| [SQLite Documentation](https://www.sitepoint.com/getting-started-sqlite3-basic-commands/) | Getting Started with SQLite3 – Basic Commands. |
| [GraphQL Documentation](http://graphql.org/graphql-js/) | A query language for your API. |
| [DataLoader Documentation](https://github.com/facebook/dataloader) | DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching. |

## Related Projects

Expand Down
180 changes: 73 additions & 107 deletions package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,83 @@ const { series, crossEnv, concurrent, rimraf, runInNewWindow } = require('nps-ut

module.exports = {
scripts: {
default: {
script: 'nps start'
},
default: 'nps start',
/**
* Starts the builded app from the dist directory
*/
start: {
script: 'node dist/app.js'
},
start: 'node dist/app.js',
/**
* Serves the current app and watches for changes to restart it
*/
serve: {
script: series(
'nps banner.serve',
'nodemon --watch src --watch .env'
)
},
serve: series(
'nps banner.serve',
'nodemon --watch src --watch .env'
),
/**
* Setup's the development environment and the database
*/
setup: {
script: series(
'yarn install',
'nps db.migrate',
'nps db.seed'
)
},
setup: series(
'yarn install',
'nps db.migrate',
'nps db.seed'
),
/**
* Builds the app into the dist directory
*/
build: {
script: series(
'nps banner.build',
'nps lint',
'nps clean.dist',
'nps transpile',
'nps copy'
)
},
build: series(
'nps banner.build',
'nps lint',
'nps clean.dist',
'nps transpile',
'nps copy'
),
/**
* Database scripts
*/
db: {
migrate: {
script: series(
'nps banner.migrate',
'nps db.config',
runFast('./node_modules/typeorm/cli.js migrations:run')
)
},
revert: {
script: series(
'nps banner.revert',
'nps db.config',
runFast('./node_modules/typeorm/cli.js migrations:revert')
)
},
seed: {
script: series(
'nps banner.seed',
'nps db.config',
runFast('./src/lib/seeds/')
)
},
config: {
script: runFast('./src/lib/ormconfig.ts')
},
drop: {
script: runFast('./node_modules/typeorm/cli.js schema:drop')
}
migrate: series(
'nps banner.migrate',
'nps db.config',
runFast('./node_modules/typeorm/cli.js migrations:run')
),
revert: series(
'nps banner.revert',
'nps db.config',
runFast('./node_modules/typeorm/cli.js migrations:revert')
),
seed: series(
'nps banner.seed',
'nps db.config',
runFast('./src/lib/seeds/')
),
config: runFast('./src/lib/ormconfig.ts'),
drop: runFast('./node_modules/typeorm/cli.js schema:drop')
},
/**
* These run various kinds of tests. Default is unit.
*/
test: {
default: 'nps test.unit',
unit: {
default: {
script: series(
'nps banner.test',
'nps test.unit.pretest',
'nps test.unit.run'
)
},
pretest: {
script: 'tslint -c ./tslint.json -t stylish ./test/unit/**/*.ts'
},
run: {
script: 'cross-env NODE_ENV=test jest --testPathPattern=unit'
},
verbose: {
script: 'nps "test --verbose"'
},
coverage: {
script: 'nps "test --coverage"'
}
default: series(
'nps banner.test',
'nps test.unit.pretest',
'nps test.unit.run'
),
pretest: 'tslint -c ./tslint.json -t stylish ./test/unit/**/*.ts',
run: 'cross-env NODE_ENV=test jest --testPathPattern=unit',
verbose: 'nps "test --verbose"',
coverage: 'nps "test --coverage"'
},
integration: {
default: series(
'nps banner.test',
'nps test.integration.pretest',
'nps test.integration.run'
),
pretest: 'tslint -c ./tslint.json -t stylish ./test/integration/**/*.ts',
verbose: 'nps "test.integration --verbose"',
run: 'cross-env NODE_ENV=test jest --testPathPattern=integration -i',
},
e2e: {
default: {
Expand All @@ -128,51 +108,37 @@ module.exports = {
/**
* Runs TSLint over your project
*/
lint: {
script: `tslint -c ./tslint.json -p tsconfig.json src/**/*.ts --format stylish`
},
lint: `tslint -c ./tslint.json -p tsconfig.json src/**/*.ts --format stylish`,
/**
* Transpile your app into javascript
*/
transpile: {
script: `tsc`
},
transpile: `tsc`,
/**
* Clean files and folders
*/
clean: {
default: {
script: series(
`nps banner.clean`,
`nps clean.dist`
)
},
dist: {
script: rimraf('./dist')
}
default: series(
`nps banner.clean`,
`nps clean.dist`
),
dist: rimraf('./dist')
},
/**
* Copies static files to the build folder
*/
copy: {
default: {
script: series(
`nps copy.swagger`,
`nps copy.public`
)
},
swagger: {
script: copy(
'./src/api/swagger.json',
'./dist'
)
},
public: {
script: copy(
'./src/public/*',
'./dist'
)
}
default: series(
`nps copy.swagger`,
`nps copy.public`
),
swagger: copy(
'./src/api/swagger.json',
'./dist'
),
public: copy(
'./src/public/*',
'./dist'
)
},
/**
* This creates pretty banner to the terminal
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,17 @@
"compression": "^1.7.1",
"copyfiles": "^1.2.0",
"cors": "^2.8.4",
"dataloader": "^1.3.0",
"dotenv": "^4.0.0",
"event-dispatch": "^0.4.1",
"express": "^4.16.2",
"express-basic-auth": "^1.1.3",
"express-graphql": "^0.6.11",
"express-status-monitor": "^1.0.1",
"faker": "^4.1.0",
"figlet": "^1.2.0",
"glob": "^7.1.2",
"graphql": "^0.11.7",
"helmet": "^3.9.0",
"jsonfile": "^4.0.0",
"lodash": "^4.17.4",
Expand Down Expand Up @@ -113,6 +116,7 @@
"mock-express-request": "^0.2.0",
"mock-express-response": "^0.2.1",
"nock": "^9.1.0",
"sqlite3": "^3.1.13",
"ts-jest": "^21.1.4"
}
}
2 changes: 1 addition & 1 deletion src/api/middlewares/CompressionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';


@Middleware({ type: 'before' })
export class SecurityMiddleware implements ExpressMiddlewareInterface {
export class CompressionMiddleware implements ExpressMiddlewareInterface {

public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
return compression()(req, res, next);
Expand Down
8 changes: 7 additions & 1 deletion src/api/models/Pet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { IsNotEmpty } from 'class-validator';
import { User } from './User';

Expand All @@ -17,7 +17,13 @@ export class Pet {
@Column()
public age: number;

@Column({
nullable: true,
})
public userId: number;

@ManyToOne(type => User, user => user.pets)
@JoinColumn({ name: 'userId' })
public user: User;

public toString(): string {
Expand Down
Loading