Skip to content

Commit 428f0e9

Browse files
author
hirsch
committed
Merge branch 'release/3.1.0'
2 parents ea30ebc + dd45b84 commit 428f0e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2742
-2640
lines changed

.env.example

+34-12
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,47 @@ APP_BANNER=true
1212
# LOGGING
1313
#
1414
LOG_LEVEL=debug
15-
LOG_JSON=false
1615
LOG_OUTPUT=dev
1716

1817
#
19-
# AUTHORIZATION
18+
# PostgreSQL DATABASE
2019
#
21-
AUTH_ROUTE=http://localhost:3333/tokeninfo
20+
TYPEORM_CONNECTION=postgres
21+
TYPEORM_HOST=localhost
22+
TYPEORM_PORT=5432
23+
TYPEORM_USERNAME=username
24+
TYPEORM_PASSWORD=
25+
TYPEORM_DATABASE=my_database
26+
TYPEORM_SYNCHRONIZE=false
27+
TYPEORM_LOGGING=error
28+
TYPEORM_LOGGER=advanced-console
2229

2330
#
24-
# DATABASE
31+
# MySQL DATABASE
2532
#
26-
DB_TYPE=mysql
27-
DB_HOST=localhost
28-
DB_PORT=3306
29-
DB_USERNAME=root
30-
DB_PASSWORD=
31-
DB_DATABASE=my_database
32-
DB_SYNCHRONIZE=false
33-
DB_LOGGING=false
33+
# TYPEORM_CONNECTION=mysql
34+
# TYPEORM_HOST=localhost
35+
# TYPEORM_PORT=3306
36+
# TYPEORM_USERNAME=root
37+
# TYPEORM_PASSWORD=root
38+
# TYPEORM_DATABASE=my_database
39+
# TYPEORM_SYNCHRONIZE=false
40+
# TYPEORM_LOGGING=error
41+
# TYPEORM_LOGGER=advanced-console
42+
43+
#
44+
# PATH STRUCTRUE
45+
#
46+
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
47+
TYPEORM_MIGRATIONS_DIR=src/database/migrations
48+
TYPEORM_ENTITIES=src/api/models/**/*.ts
49+
TYPEORM_ENTITIES_DIR=src/api/models
50+
CONTROLLERS=src/api/controllers/**/*Controller.ts
51+
MIDDLEWARES=src/api/middlewares/**/*Middleware.ts
52+
INTERCEPTORS=src/api/interceptors/**/*Interceptor.ts
53+
SUBSCRIBERS=src/api/subscribers/**/*Subscriber.ts
54+
QUERIES=src/api/queries/**/*Query.ts
55+
MUTATIONS=src/api/mutations/**/*Mutation.ts
3456

3557
#
3658
# GraphQL

.env.test

+18-8
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,36 @@ APP_BANNER=false
1212
# LOGGING
1313
#
1414
LOG_LEVEL=none
15-
LOG_JSON=false
1615
LOG_OUTPUT=dev
1716

1817
#
19-
# AUTHORIZATION
18+
# DATABASE
2019
#
21-
AUTH_ROUTE=http://localhost:3333/tokeninfo
20+
TYPEORM_CONNECTION=sqlite
21+
TYPEORM_DATABASE=./mydb.sql
22+
TYPEORM_LOGGING=error
23+
TYPEORM_LOGGER=advanced-console
2224

2325
#
24-
# DATABASE
25-
#
26-
DB_TYPE=sqlite
27-
DB_DATABASE=./mydb.sql
28-
DB_LOGGING=false
26+
# PATH STRUCTRUE
27+
#
28+
TYPEORM_MIGRATIONS=src/database/migrations/**/*.ts
29+
TYPEORM_MIGRATIONS_DIR=src/database/migrations
30+
TYPEORM_ENTITIES=src/api/models/**/*.ts
31+
TYPEORM_ENTITIES_DIR=src/api/models
32+
CONTROLLERS=src/api/controllers/**/*Controller.ts
33+
MIDDLEWARES=src/api/middlewares/**/*Middleware.ts
34+
INTERCEPTORS=src/api/interceptors/**/*Interceptor.ts
35+
SUBSCRIBERS=src/api/subscribers/**/*Subscriber.ts
36+
QUERIES=src/api/queries/**/*Query.ts
37+
MUTATIONS=src/api/mutations/**/*Mutation.ts
2938

3039
#
3140
# GraphQL
3241
#
3342
GRAPHQL_ENABLED=true
3443
GRAPHQL_ROUTE=/graphql
44+
GRAPHQL_EDITOR=false
3545

3646
#
3747
# Swagger

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ yarn-error.log
1414
# OS generated files #
1515
.DS_Store
1616
Thumbs.db
17+
.tmp/
1718

1819
# Typing #
1920
typings/
2021

2122
# Dist #
2223
dist/
23-
ormconfig.json
2424
tsconfig.build.json
2525

2626
# IDE #

.vscode/launch.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
],
1616
"protocol": "inspector",
1717
"env": {
18-
"NODE_ENV": "development"
18+
"NODE_ENV": "production"
1919
}
20+
},
21+
{
22+
"type": "node",
23+
"request": "attach",
24+
"name": "Nodemon Debug",
25+
"port": 9229,
26+
"restart": true
2027
}
2128
]
2229
}

.vscode/settings.json

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"cSpell.enabled": true,
44
"files.exclude": {
55
"tsconfig.build.json": true,
6-
"ormconfig.json": true
76
},
87
"importSorter.generalConfiguration.sortOnBeforeSave": true,
98
"files.trimTrailingWhitespace": true,

README.md

+25-27
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Install [Node.js and NPM](https://nodejs.org/en/download/)
8989
Install yarn globally
9090

9191
```bash
92-
npm install yarn -g
92+
yarn install yarn -g
9393
```
9494

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

109109
```bash
110-
npm run setup
110+
yarn run setup
111111
```
112112

113113
> 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.
114114
115115
### Step 3: Serve your App
116116

117-
Go to the project dir and start your app with this npm script.
117+
Go to the project dir and start your app with this yarn script.
118118

119119
```bash
120-
npm start serve
120+
yarn start serve
121121
```
122122

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

136136
### Linting
137137

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

141141
### Tests
142142

143-
- Run the unit tests using `npm start test` (There is also a vscode task for this called `test`).
144-
- Run the integration tests using `npm start test.integration`.
145-
- Run the e2e tests using `npm start test.e2e`.
143+
- Run the unit tests using `yarn start test` (There is also a vscode task for this called `test`).
144+
- Run the integration tests using `yarn start test.integration`.
145+
- Run the e2e tests using `yarn start test.e2e`.
146146

147147
### Running in dev mode
148148

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

152152
### Building the project and run it
153153

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

157157
### Database Migration
158158

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

165165
### Database Seeding
166166

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

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

171171
## ❯ Debugger in VSCode
172172

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

176176
![divider](./w3tec-divider.png)
@@ -228,7 +228,6 @@ The swagger and the monitor route can be altered in the `.env` file.
228228
| **test/unit/** *.test.ts | Unit tests |
229229
| .env.example | Environment configurations |
230230
| .env.test | Test environment configurations |
231-
| ormconfig.json | TypeORM configuration for the database. Used by seeds and the migration. (generated file) |
232231
| mydb.sql | SQLite database for integration tests. Ignored by git and only available after integration tests |
233232

234233
![divider](./w3tec-divider.png)
@@ -378,19 +377,18 @@ export class CreatePets implements SeedsInterface {
378377
The last step is the easiest, just hit the following command in your terminal, but be sure you are in the projects root folder.
379378
380379
```bash
381-
npm start db.seed
380+
yarn start db.seed
382381
```
383382
384383
#### CLI Interface
385384
386-
| Command | Description |
387-
| --------------------------------------------------- | ----------- |
388-
| `npm start "db.seed"` | Run all seeds |
389-
| `npm start "db.seed --run CreateBruce,CreatePets"` | Run specific seeds (file names without extension) |
390-
| `npm start "db.seed -L"` | Log database queries to the terminal |
391-
| `npm start "db.seed --factories <path>"` | Add a different path to your factories (Default: `src/database/`) |
392-
| `npm start "db.seed --seeds <path>"` | Add a different path to your seeds (Default: `src/database/seeds/`) |
393-
| `npm start "db.seed --config <file>"` | Path to your ormconfig.json file |
385+
| Command | Description |
386+
| ---------------------------------------------------- | ----------- |
387+
| `yarn start "db.seed"` | Run all seeds |
388+
| `yarn start "db.seed --run CreateBruce,CreatePets"` | Run specific seeds (file names without extension) |
389+
| `yarn start "db.seed -L"` | Log database queries to the terminal |
390+
| `yarn start "db.seed --factories <path>"` | Add a different path to your factories (Default: `src/database/`) |
391+
| `yarn start "db.seed --seeds <path>"` | Add a different path to your seeds (Default: `src/database/seeds/`) |
394392
395393
![divider](./w3tec-divider.png)
396394

commands/banner.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from 'chalk';
22
import * as figlet from 'figlet';
33

4-
figlet(process.argv[2], (error: any, data: any) => {
4+
figlet.text(process.argv[2], (error: any, data: any) => {
55
if (error) {
66
return process.exit(1);
77
}

commands/ormconfig.ts

-31
This file was deleted.

commands/seed.ts

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import chalk from 'chalk';
2+
import commander from 'commander';
3+
import * as path from 'path';
4+
import {
5+
loadConnection, loadEntityFactories, loadSeeds, runSeed, setConnection
6+
} from 'typeorm-seeding';
7+
8+
// Cli helper
9+
commander
10+
.version('1.0.0')
11+
.description('Run database seeds of your project')
12+
.option('-L, --logging', 'enable sql query logging')
13+
.option('--factories <path>', 'add filepath for your factories')
14+
.option('--seeds <path>', 'add filepath for your seeds')
15+
.option('--run <seeds>', 'run specific seeds (file names without extension)', (val) => val.split(','))
16+
.option('--config <file>', 'path to your ormconfig.json file (must be a json)')
17+
.parse(process.argv);
18+
19+
// Get cli parameter for a different factory path
20+
const factoryPath = (commander.factories)
21+
? commander.factories
22+
: 'src/database/factories';
23+
24+
// Get cli parameter for a different seeds path
25+
const seedsPath = (commander.seeds)
26+
? commander.seeds
27+
: 'src/database/seeds/';
28+
29+
// Get a list of seeds
30+
const listOfSeeds = (commander.run)
31+
? commander.run.map(l => l.trim()).filter(l => l.length > 0)
32+
: [];
33+
34+
// Search for seeds and factories
35+
const run = async () => {
36+
const log = console.log;
37+
38+
let factoryFiles;
39+
let seedFiles;
40+
try {
41+
factoryFiles = await loadEntityFactories(factoryPath);
42+
seedFiles = await loadSeeds(seedsPath);
43+
} catch (error) {
44+
return handleError(error);
45+
}
46+
47+
// Filter seeds
48+
if (listOfSeeds.length > 0) {
49+
seedFiles = seedFiles.filter(sf => listOfSeeds.indexOf(path.basename(sf).replace('.ts', '')) >= 0);
50+
}
51+
52+
// Status logging to print out the amount of factories and seeds.
53+
log(chalk.bold('seeds'));
54+
log('🔎 ', chalk.gray.underline(`found:`),
55+
chalk.blue.bold(`${factoryFiles.length} factories`, chalk.gray('&'), chalk.blue.bold(`${seedFiles.length} seeds`)));
56+
57+
// Get database connection and pass it to the seeder
58+
try {
59+
const connection = await loadConnection();
60+
setConnection(connection);
61+
} catch (error) {
62+
return handleError(error);
63+
}
64+
65+
// Show seeds in the console
66+
for (const seedFile of seedFiles) {
67+
try {
68+
let className = seedFile.split('/')[seedFile.split('/').length - 1];
69+
className = className.replace('.ts', '').replace('.js', '');
70+
className = className.split('-')[className.split('-').length - 1];
71+
log('\n' + chalk.gray.underline(`executing seed: `), chalk.green.bold(`${className}`));
72+
const seedFileObject: any = require(seedFile);
73+
await runSeed(seedFileObject[className]);
74+
} catch (error) {
75+
console.error('Could not run seed ', error);
76+
process.exit(1);
77+
}
78+
}
79+
80+
log('\n👍 ', chalk.gray.underline(`finished seeding`));
81+
process.exit(0);
82+
};
83+
84+
const handleError = (error) => {
85+
console.error(error);
86+
process.exit(1);
87+
};
88+
89+
run();

0 commit comments

Comments
 (0)