Skip to content

Commit 8d11ca5

Browse files
feat: merge pull request #143 from vndevteam/develop
feat: merge develop into main
2 parents 0e5f153 + d632122 commit 8d11ca5

39 files changed

+1718
-657
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ $ pnpm start:prod
5959
## Features
6060

6161
- [x] Database. Support [TypeORM](https://www.npmjs.com/package/typeorm)
62-
- [ ] Seeding.
62+
- [x] Seeding.
6363
- [x] Config Service ([@nestjs/config](https://www.npmjs.com/package/@nestjs/config)).
6464
- [ ] Mailing ([nodemailer](https://www.npmjs.com/package/nodemailer)).
6565
- [ ] Sign in and sign up via email.
6666
- [ ] Social sign in (Apple, Facebook, Google, Twitter).
6767
- [ ] Admin and User roles.
68+
- [x] Pagination: Offset and Cursor (Clone from [typeorm-cursor-pagination](https://github.com/benjamin658/typeorm-cursor-pagination) and add more features).
6869
- [x] Internationalization/Translations (I18N) ([nestjs-i18n](https://www.npmjs.com/package/nestjs-i18n)).
6970
- [ ] File uploads. Support local and Amazon S3 drivers.
7071
- [x] Swagger.

docs/.vuepress/config.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { vi as viThemeConfig } from './config/theme/vi.config.mjs';
88
export default defineUserConfig({
99
lang: 'en-US',
1010
title: 'NestJS boilerplate',
11-
description: 'NestJS boilerplate',
11+
description: 'NestJS boilerplate with best practices',
1212
base: '/nestjs-boilerplate/',
1313
bundler: viteBundler(),
1414
markdown: {
@@ -19,11 +19,11 @@ export default defineUserConfig({
1919
locales: {
2020
'/': {
2121
lang: 'en-US',
22-
title: 'NestJS boilerplate',
22+
title: 'NestJS boilerplate 🎉',
2323
},
2424
'/vi/': {
2525
lang: 'vi-VN',
26-
title: 'NestJS boilerplate',
26+
title: 'NestJS boilerplate 🎉',
2727
},
2828
},
2929
theme: defaultTheme({

docs/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,18 @@ This is a boilerplate for NestJS projects. It is a starting point for building a
2626

2727
## References
2828

29-
// TODO: Add references
29+
- [TypeScript](https://www.typescriptlang.org/)
30+
- [NestJS](https://docs.nestjs.com/)
31+
- [TypeORM](https://typeorm.io/)
32+
- [TypeORM Extension](https://typeorm-extension.tada5hi.net/)
33+
- [Jest](https://jestjs.io/)
34+
- [PNPM](https://pnpm.io/)
35+
- [ESLint](https://eslint.org/)
36+
- [TypeScript ESLint](https://typescript-eslint.io/)
37+
- [Prettier](https://prettier.io/)
38+
- [Husky](https://typicode.github.io/husky/)
39+
- [Lint Staged](https://github.com/lint-staged/lint-staged)
40+
- [Commitlint](https://commitlint.js.org/)
41+
- [Conventional Commits](https://www.conventionalcommits.org/)
42+
- [Commitizen](https://commitizen-tools.github.io/commitizen/)
43+
- [Renovate](https://docs.renovatebot.com/)

docs/database.md

+81
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
We use [TypeORM](https://typeorm.io/) as an ORM for working with databases. It supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL databases.
44

5+
For seeding data, we use [typeorm-extension](https://github.com/tada5hi/typeorm-extension) package.
6+
57
---
68

79
[[toc]]
@@ -57,3 +59,82 @@ pnpm migration:up
5759
```bash
5860
pnpm migration:down
5961
```
62+
63+
## Seeding (TypeORM)
64+
65+
### Creating seed file
66+
67+
1. Create seed file with `pnpm seed:create` command:
68+
69+
```bash
70+
pnpm seed:create src/database/seeds/post-seeder
71+
```
72+
73+
2. Go to `src/database/seeds/xxxtimestampxxx-post-seeder.ts` and write your seed data:
74+
75+
```typescript
76+
// /src/database/seeds/xxxtimestampxxx-post-seeder.ts
77+
import { PostEntity } from '@/api/post/entities/post.entity';
78+
import { DataSource } from 'typeorm';
79+
import { Seeder, SeederFactoryManager } from 'typeorm-extension';
80+
81+
export class PostSeederxxxtimestampxxx implements Seeder {
82+
track = false;
83+
84+
public async run(
85+
dataSource: DataSource,
86+
factoryManager: SeederFactoryManager,
87+
): Promise<any> {
88+
// Creating post by using repository
89+
const repository = dataSource.getRepository(PostEntity);
90+
await repository.insert(
91+
new PostEntity({
92+
title: 'Post 1',
93+
content: 'Content 1',
94+
}),
95+
);
96+
97+
// Creating post by using factory
98+
const postFactory = factoryManager.get(PostEntity);
99+
await postFactory.saveMany(5);
100+
}
101+
}
102+
```
103+
104+
In `run` method extend your logic, you can use repository or factory to create data.
105+
106+
3. Apply this seed to database via `pnpm seed:run`.
107+
108+
### Running seed
109+
110+
```bash
111+
pnpm seed:run
112+
```
113+
114+
### Factory and Faker
115+
116+
To create entities with random data, create a factory for each desired entity. The definition of a factory is **optional**. If you don't define a factory, the seeder will use the repository to create entities.
117+
118+
The factory callback provides an instance of the [faker](https://fakerjs.dev/guide/) library as function argument, to populate the entity with random data.
119+
120+
1. Create factory file at `src/database/factories/post.factory.ts`:
121+
122+
```typescript
123+
import { PostEntity } from '@/api/post/entities/post.entity';
124+
import { setSeederFactory } from 'typeorm-extension';
125+
126+
export default setSeederFactory(PostEntity, (fake) => {
127+
const post = new PostEntity();
128+
post.title = fake.lorem.sentence();
129+
post.content = fake.lorem.paragraph();
130+
131+
return post;
132+
});
133+
```
134+
135+
2. Use factory in `src/database/seeds/xxxtimestampxxx-post-seeder.ts`
136+
3. Run seed:
137+
138+
```bash
139+
pnpm seed:run
140+
```

package.json

+17-12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
"migration:show": "pnpm typeorm migration:show",
2525
"migration:create": "typeorm migration:create",
2626
"migration:generate": "pnpm typeorm migration:generate --pretty",
27+
"db:create": "env-cmd ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:create",
28+
"db:drop": "env-cmd ts-node ./node_modules/typeorm-extension/bin/cli.cjs db:drop",
29+
"seed:run": "env-cmd ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:run",
30+
"seed:create": "env-cmd ts-node ./node_modules/typeorm-extension/bin/cli.cjs seed:create",
2731
"prepare": "husky",
2832
"docs:dev": "vuepress dev docs",
2933
"docs:build": "vuepress build docs"
@@ -52,38 +56,39 @@
5256
"nodemailer": "6.9.14",
5357
"pg": "8.12.0",
5458
"pino-http": "10.2.0",
55-
"pino-pretty": "11.2.1",
59+
"pino-pretty": "11.2.2",
5660
"reflect-metadata": "0.2.2",
5761
"rxjs": "7.8.1",
5862
"typeorm": "0.3.20",
63+
"typeorm-extension": "3.6.0",
5964
"uuid": "10.0.0"
6065
},
6166
"devDependencies": {
6267
"@commitlint/cli": "19.3.0",
6368
"@commitlint/config-conventional": "19.2.2",
64-
"@eslint/js": "9.7.0",
69+
"@eslint/js": "9.8.0",
6570
"@nestjs/cli": "10.4.2",
66-
"@nestjs/schematics": "10.1.2",
71+
"@nestjs/schematics": "10.1.3",
6772
"@nestjs/testing": "10.3.10",
6873
"@swc/cli": "0.4.0",
69-
"@swc/core": "1.7.0",
74+
"@swc/core": "1.7.3",
7075
"@types/compression": "1.7.5",
7176
"@types/eslint__js": "8.42.3",
7277
"@types/express": "4.17.21",
7378
"@types/jest": "29.5.12",
7479
"@types/ms": "0.7.34",
75-
"@types/node": "20.14.11",
80+
"@types/node": "20.14.13",
7681
"@types/nodemailer": "6.4.15",
7782
"@types/supertest": "6.0.2",
7883
"@types/uuid": "10.0.0",
7984
"@vuepress/bundler-vite": "2.0.0-rc.14",
80-
"@vuepress/plugin-search": "2.0.0-rc.39",
81-
"@vuepress/theme-default": "2.0.0-rc.39",
85+
"@vuepress/plugin-search": "2.0.0-rc.40",
86+
"@vuepress/theme-default": "2.0.0-rc.40",
8287
"env-cmd": "10.1.0",
83-
"eslint": "9.7.0",
88+
"eslint": "9.8.0",
8489
"eslint-config-prettier": "9.1.0",
8590
"eslint-plugin-prettier": "5.2.1",
86-
"husky": "9.1.1",
91+
"husky": "9.1.4",
8792
"jest": "29.7.0",
8893
"lint-staged": "15.2.7",
8994
"prettier": "3.3.3",
@@ -95,9 +100,9 @@
95100
"ts-node": "10.9.2",
96101
"tsconfig-paths": "4.2.0",
97102
"tslib": "2.6.3",
98-
"typescript": "5.5.3",
99-
"typescript-eslint": "7.16.1",
100-
"vue": "3.4.33",
103+
"typescript": "5.5.4",
104+
"typescript-eslint": "7.18.0",
105+
"vue": "3.4.34",
101106
"vuepress": "2.0.0-rc.14"
102107
}
103108
}

0 commit comments

Comments
 (0)