Skip to content

Commit

Permalink
Merge branch 'release/3.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
hirsch committed Nov 10, 2018
2 parents ea30ebc + dd45b84 commit 428f0e9
Show file tree
Hide file tree
Showing 65 changed files with 2,742 additions and 2,640 deletions.
46 changes: 34 additions & 12 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,47 @@ APP_BANNER=true
# LOGGING
#
LOG_LEVEL=debug
LOG_JSON=false
LOG_OUTPUT=dev

#
# AUTHORIZATION
# PostgreSQL DATABASE
#
AUTH_ROUTE=http://localhost:3333/tokeninfo
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_PORT=5432
TYPEORM_USERNAME=username
TYPEORM_PASSWORD=
TYPEORM_DATABASE=my_database
TYPEORM_SYNCHRONIZE=false
TYPEORM_LOGGING=error
TYPEORM_LOGGER=advanced-console

#
# DATABASE
# MySQL DATABASE
#
DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE=my_database
DB_SYNCHRONIZE=false
DB_LOGGING=false
# TYPEORM_CONNECTION=mysql
# TYPEORM_HOST=localhost
# TYPEORM_PORT=3306
# TYPEORM_USERNAME=root
# TYPEORM_PASSWORD=root
# TYPEORM_DATABASE=my_database
# TYPEORM_SYNCHRONIZE=false
# TYPEORM_LOGGING=error
# TYPEORM_LOGGER=advanced-console

#
# PATH STRUCTRUE
#
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
TYPEORM_MIGRATIONS_DIR=src/database/migrations
TYPEORM_ENTITIES=src/api/models/**/*.ts
TYPEORM_ENTITIES_DIR=src/api/models
CONTROLLERS=src/api/controllers/**/*Controller.ts
MIDDLEWARES=src/api/middlewares/**/*Middleware.ts
INTERCEPTORS=src/api/interceptors/**/*Interceptor.ts
SUBSCRIBERS=src/api/subscribers/**/*Subscriber.ts
QUERIES=src/api/queries/**/*Query.ts
MUTATIONS=src/api/mutations/**/*Mutation.ts

#
# GraphQL
Expand Down
26 changes: 18 additions & 8 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ APP_BANNER=false
# LOGGING
#
LOG_LEVEL=none
LOG_JSON=false
LOG_OUTPUT=dev

#
# AUTHORIZATION
# DATABASE
#
AUTH_ROUTE=http://localhost:3333/tokeninfo
TYPEORM_CONNECTION=sqlite
TYPEORM_DATABASE=./mydb.sql
TYPEORM_LOGGING=error
TYPEORM_LOGGER=advanced-console

#
# DATABASE
#
DB_TYPE=sqlite
DB_DATABASE=./mydb.sql
DB_LOGGING=false
# PATH STRUCTRUE
#
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
TYPEORM_MIGRATIONS_DIR=src/database/migrations
TYPEORM_ENTITIES=src/api/models/**/*.ts
TYPEORM_ENTITIES_DIR=src/api/models
CONTROLLERS=src/api/controllers/**/*Controller.ts
MIDDLEWARES=src/api/middlewares/**/*Middleware.ts
INTERCEPTORS=src/api/interceptors/**/*Interceptor.ts
SUBSCRIBERS=src/api/subscribers/**/*Subscriber.ts
QUERIES=src/api/queries/**/*Query.ts
MUTATIONS=src/api/mutations/**/*Mutation.ts

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

#
# Swagger
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ yarn-error.log
# OS generated files #
.DS_Store
Thumbs.db
.tmp/

# Typing #
typings/

# Dist #
dist/
ormconfig.json
tsconfig.build.json

# IDE #
Expand Down
9 changes: 8 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@
],
"protocol": "inspector",
"env": {
"NODE_ENV": "development"
"NODE_ENV": "production"
}
},
{
"type": "node",
"request": "attach",
"name": "Nodemon Debug",
"port": 9229,
"restart": true
}
]
}
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"cSpell.enabled": true,
"files.exclude": {
"tsconfig.build.json": true,
"ormconfig.json": true
},
"importSorter.generalConfiguration.sortOnBeforeSave": true,
"files.trimTrailingWhitespace": true,
Expand Down
52 changes: 25 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Install [Node.js and NPM](https://nodejs.org/en/download/)
Install yarn globally

```bash
npm install yarn -g
yarn install yarn -g
```

Install a MySQL database.
Expand All @@ -107,17 +107,17 @@ Create a new database with the name you have in your `.env`-file.
Then setup your application environment.

```bash
npm run setup
yarn run setup
```

> This installs all dependencies with yarn. After that it migrates the database and seeds some test data into it. So after that your development environment is ready to use.
### Step 3: Serve your App

Go to the project dir and start your app with this npm script.
Go to the project dir and start your app with this yarn script.

```bash
npm start serve
yarn start serve
```

> This starts a local server using `nodemon`, which will watch for any file changes and will restart the sever according to these changes.
Expand All @@ -135,42 +135,42 @@ All script are defined in the `package-scripts.js` file, but the most important

### Linting

- Run code quality analysis using `npm start lint`. This runs tslint.
- Run code quality analysis using `yarn start lint`. This runs tslint.
- There is also a vscode task for this called `lint`.

### 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`.
- Run the unit tests using `yarn start test` (There is also a vscode task for this called `test`).
- Run the integration tests using `yarn start test.integration`.
- Run the e2e tests using `yarn start test.e2e`.

### Running in dev mode

- Run `npm start serve` to start nodemon with ts-node, to serve the app.
- Run `yarn start serve` to start nodemon with ts-node, to serve the app.
- The server address will be displayed to you as `http://0.0.0.0:3000`

### Building the project and run it

- Run `npm start build` to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called `build`).
- To start the builded app located in `dist` use `npm start`.
- Run `yarn start build` to generated all JavaScript files from the TypeScript sources (There is also a vscode task for this called `build`).
- To start the builded app located in `dist` use `yarn start`.

### Database Migration

- Run `typeorm migrations:create -n <migration-file-name>` to create a new migration file.
- Run `typeorm migration:create -n <migration-file-name>` to create a new migration file.
- Try `typeorm -h` to see more useful cli commands like generating migration out of your models.
- To migrate your database run `npm start db.migrate`.
- To revert your latest migration run `npm start db.revert`.
- Drops the complete database schema `npm start db.drop`.
- To migrate your database run `yarn start db.migrate`.
- To revert your latest migration run `yarn start db.revert`.
- Drops the complete database schema `yarn start db.drop`.

### Database Seeding

- Run `npm start db.seed` to seed your seeds into the database.
- Run `yarn start db.seed` to seed your seeds into the database.

![divider](./w3tec-divider.png)

## ❯ Debugger in VSCode

To debug your code run `npm start build` or hit <kbd>cmd</kbd> + <kbd>b</kbd> to build your app.
To debug your code run `yarn start build` or hit <kbd>cmd</kbd> + <kbd>b</kbd> to build your app.
Then, just set a breakpoint and hit <kbd>F5</kbd> in your Visual Studio Code.

![divider](./w3tec-divider.png)
Expand Down Expand Up @@ -228,7 +228,6 @@ The swagger and the monitor route can be altered in the `.env` file.
| **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 |

![divider](./w3tec-divider.png)
Expand Down Expand Up @@ -378,19 +377,18 @@ export class CreatePets implements SeedsInterface {
The last step is the easiest, just hit the following command in your terminal, but be sure you are in the projects root folder.
```bash
npm start db.seed
yarn start db.seed
```
#### CLI Interface
| Command | Description |
| --------------------------------------------------- | ----------- |
| `npm start "db.seed"` | Run all seeds |
| `npm start "db.seed --run CreateBruce,CreatePets"` | Run specific seeds (file names without extension) |
| `npm start "db.seed -L"` | Log database queries to the terminal |
| `npm start "db.seed --factories <path>"` | Add a different path to your factories (Default: `src/database/`) |
| `npm start "db.seed --seeds <path>"` | Add a different path to your seeds (Default: `src/database/seeds/`) |
| `npm start "db.seed --config <file>"` | Path to your ormconfig.json file |
| Command | Description |
| ---------------------------------------------------- | ----------- |
| `yarn start "db.seed"` | Run all seeds |
| `yarn start "db.seed --run CreateBruce,CreatePets"` | Run specific seeds (file names without extension) |
| `yarn start "db.seed -L"` | Log database queries to the terminal |
| `yarn start "db.seed --factories <path>"` | Add a different path to your factories (Default: `src/database/`) |
| `yarn start "db.seed --seeds <path>"` | Add a different path to your seeds (Default: `src/database/seeds/`) |
![divider](./w3tec-divider.png)
Expand Down
2 changes: 1 addition & 1 deletion commands/banner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import * as figlet from 'figlet';

figlet(process.argv[2], (error: any, data: any) => {
figlet.text(process.argv[2], (error: any, data: any) => {
if (error) {
return process.exit(1);
}
Expand Down
31 changes: 0 additions & 31 deletions commands/ormconfig.ts

This file was deleted.

89 changes: 89 additions & 0 deletions commands/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import chalk from 'chalk';
import commander from 'commander';
import * as path from 'path';
import {
loadConnection, loadEntityFactories, loadSeeds, runSeed, setConnection
} from 'typeorm-seeding';

// Cli helper
commander
.version('1.0.0')
.description('Run database seeds of your project')
.option('-L, --logging', 'enable sql query logging')
.option('--factories <path>', 'add filepath for your factories')
.option('--seeds <path>', 'add filepath for your seeds')
.option('--run <seeds>', 'run specific seeds (file names without extension)', (val) => val.split(','))
.option('--config <file>', 'path to your ormconfig.json file (must be a json)')
.parse(process.argv);

// Get cli parameter for a different factory path
const factoryPath = (commander.factories)
? commander.factories
: 'src/database/factories';

// Get cli parameter for a different seeds path
const seedsPath = (commander.seeds)
? commander.seeds
: 'src/database/seeds/';

// Get a list of seeds
const listOfSeeds = (commander.run)
? commander.run.map(l => l.trim()).filter(l => l.length > 0)
: [];

// Search for seeds and factories
const run = async () => {
const log = console.log;

let factoryFiles;
let seedFiles;
try {
factoryFiles = await loadEntityFactories(factoryPath);
seedFiles = await loadSeeds(seedsPath);
} catch (error) {
return handleError(error);
}

// Filter seeds
if (listOfSeeds.length > 0) {
seedFiles = seedFiles.filter(sf => listOfSeeds.indexOf(path.basename(sf).replace('.ts', '')) >= 0);
}

// Status logging to print out the amount of factories and seeds.
log(chalk.bold('seeds'));
log('🔎 ', chalk.gray.underline(`found:`),
chalk.blue.bold(`${factoryFiles.length} factories`, chalk.gray('&'), chalk.blue.bold(`${seedFiles.length} seeds`)));

// Get database connection and pass it to the seeder
try {
const connection = await loadConnection();
setConnection(connection);
} catch (error) {
return handleError(error);
}

// Show seeds in the console
for (const seedFile of seedFiles) {
try {
let className = seedFile.split('/')[seedFile.split('/').length - 1];
className = className.replace('.ts', '').replace('.js', '');
className = className.split('-')[className.split('-').length - 1];
log('\n' + chalk.gray.underline(`executing seed: `), chalk.green.bold(`${className}`));
const seedFileObject: any = require(seedFile);
await runSeed(seedFileObject[className]);
} catch (error) {
console.error('Could not run seed ', error);
process.exit(1);
}
}

log('\n👍 ', chalk.gray.underline(`finished seeding`));
process.exit(0);
};

const handleError = (error) => {
console.error(error);
process.exit(1);
};

run();

0 comments on commit 428f0e9

Please sign in to comment.