Skip to content

Commit 4954f61

Browse files
committed
init
1 parent 077f60f commit 4954f61

Some content is hidden

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

59 files changed

+9588
-4
lines changed

.gitattributes

100644100755
+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# Auto detect text files and perform LF normalization
22
* text=auto
3+
4+
# JS and TS files must always use LF for tools to work
5+
*.js eol=lf
6+
*.ts eol=lf

.gitignore

100644100755
+6-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.vscode
2+
.DS_Store
3+
dist
4+
build
5+
6+
17
# Logs
28
logs
39
*.log
@@ -41,9 +47,6 @@ build/Release
4147
node_modules/
4248
jspm_packages/
4349

44-
# TypeScript v1 declaration files
45-
typings/
46-
4750
# TypeScript cache
4851
*.tsbuildinfo
4952

.prettierrc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"tabWidth": 2,
3+
"useTabs": false,
4+
"semi": true,
5+
"singleQuote": true,
6+
"quoteProps": "as-needed",
7+
"trailingComma": "none",
8+
"bracketSpacing": true,
9+
"arrowParens": "always"
10+
}

README.md

100644100755
+71-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,72 @@
11
# node-nestjs-structure
2-
Node.js framework NestJS project structure
2+
Node.js framework NestJS project structure
3+
4+
### Installation
5+
```sh
6+
# 1. node_modules
7+
$ npm i
8+
# 2. edit env config
9+
$ vim ./config/*.json
10+
# 3. database model entity generate
11+
$ npm run entity dbname
12+
# OR
13+
$ npm run entity dbname skip
14+
$ npm run build:entity
15+
```
16+
17+
### Development
18+
```sh
19+
$ npm run start:dev
20+
```
21+
22+
### Production
23+
```sh
24+
# define NODE_ENV yourself
25+
$ npm run build
26+
# OR
27+
$ npm start
28+
```
29+
30+
### Folders
31+
```js
32+
+-- bin // Custom tasks
33+
+-- config // Env config files for `config` module
34+
+-- dist // Source build
35+
+-- src
36+
| +-- common // Nest Module, global
37+
| | +-- controllers // Nest Controllers
38+
| | +-- decorators // Nest Decorators
39+
| | +-- dto // DTO (Data Transfer Object) Schema, Validation
40+
| | +-- filters // Nest Filters
41+
| | +-- guards // Nest Guards
42+
| | +-- interceptors // Nest Interceptors
43+
| | +-- interfaces // TypeScript Interfaces
44+
| | +-- middleware // Nest Middleware
45+
| | +-- pipes // Nest Pipes
46+
| | +-- providers // Nest Providers
47+
| | | +-- models // or repository
48+
| +-- entity // TypeORM Entities generated by `typeorm-model-generator` module
49+
| +-- * // Other Nest Modules, non-global, same as common structure above
50+
+-- test // Jest testing
51+
+-- typings // Modules or global type definitions
52+
```
53+
54+
### Implementation
55+
* See [app](src/app.ts), [app.module](src/app.module.ts)
56+
- Database
57+
- Static Files
58+
- Module Router
59+
* [Global Authenticated Guard](src/common/guards/authenticated.guard.ts)
60+
* [Global Exception Filter](src/common/filters/exceptions.filter.ts)
61+
* [Global Logging Middleware](src/common/middleware/logger.middleware.ts)
62+
* [Session Login with Passport](src/base/providers/local.strategy.ts)
63+
* [AWS SDK Example](src/aws)
64+
* Controller Routes
65+
- [Login](src/base/controllers/login.controller.ts)
66+
- [Sample](src/sample/controllers/sample.controller.ts) Parameter, [DTO](src/sample/dto/sample.dto.ts)
67+
* [Database Query](src/sample/providers/database.service.ts) Sample
68+
69+
#### Links.
70+
* [NestJS](https://docs.nestjs.com)
71+
* [Nest Sample](https://github.com/nestjs/nest/tree/master/sample)
72+
* [Awesome Nest](https://github.com/juliandavidmr/awesome-nestjs)

bin/entity.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const config = require('config');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const rimraf = require('rimraf');
5+
const shell = require('shelljs');
6+
7+
/**
8+
* npm run entity dbname [skip]
9+
*/
10+
process.env.PATH += (path.delimiter + path.join(process.cwd(), 'node_modules', '.bin'));
11+
if (process.argv.length < 3) {
12+
throw new Error('RequiredDatabase');
13+
};
14+
15+
const db = process.argv[2];
16+
const isBuild = process.argv[3] !== 'skip';
17+
18+
/* const DATABASE = [];
19+
if (!DATABASE.includes(db)) {
20+
throw new Error('InvalidDatabase');
21+
} */
22+
23+
const MODEL_DIR = path.join(__dirname, '../src/entity', db);
24+
rimraf.sync(`${MODEL_DIR}/*`);
25+
26+
const dbConfig = config.get('db');
27+
shell.exec('typeorm-model-generator --noConfig --ce pascal '
28+
+ `-h ${dbConfig.replication.master.host} `
29+
+ `-p ${dbConfig.replication.master.port} `
30+
+ `-d ${db} `
31+
+ `-u ${dbConfig.replication.master.username} `
32+
+ `-x ${dbConfig.replication.master.password} `
33+
+ `-e ${dbConfig.type} `
34+
+ `-o ${MODEL_DIR}`);
35+
36+
const files = [];
37+
fs.readdirSync(MODEL_DIR).forEach((file) => {
38+
files.push(`export * from './${file.replace('.ts', '')}';`);
39+
});
40+
files.push('');
41+
// export entity db tables
42+
// AS-IS import { Tablename } from './entity/dbname/tablename';
43+
// TO-BE import { Tablename } from './entity/dbname';
44+
fs.writeFileSync(path.join(MODEL_DIR, 'index.ts'), files.join('\n'));
45+
46+
// Build skips and builds manually when errors occur
47+
if (isBuild) {
48+
shell.exec('npm run build:entity');
49+
}

config/default.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"aws": {
3+
"access_key_id": "access_key_id",
4+
"secret_access_key": "secret_access_key",
5+
"region": "region"
6+
},
7+
"hello": "world"
8+
}

config/development.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"db": {
3+
"type": "mysql",
4+
"synchronize": false,
5+
"logging": true,
6+
"database": "dbname",
7+
"replication": {
8+
"master": {
9+
"host": "127.0.0.1",
10+
"port": 3306,
11+
"username": "username",
12+
"password": "password"
13+
},
14+
"slaves": [{
15+
"host": "127.0.0.1",
16+
"port": 3306,
17+
"username": "username",
18+
"password": "password"
19+
}]
20+
},
21+
"extra": {
22+
"connectionLimit": 10
23+
}
24+
},
25+
"foo": "bar"
26+
}

config/production.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"db": {
3+
"type": "mysql",
4+
"synchronize": false,
5+
"logging": false,
6+
"database": "dbname",
7+
"replication": {
8+
"master": {
9+
"host": "hostMaster",
10+
"port": 3306,
11+
"username": "username",
12+
"password": "password"
13+
},
14+
"slaves": [{
15+
"host": "hostSlave",
16+
"port": 3306,
17+
"username": "username",
18+
"password": "password"
19+
}]
20+
},
21+
"extra": {
22+
"connectionLimit": 30
23+
}
24+
},
25+
"foo": "bar"
26+
}

0 commit comments

Comments
 (0)