- Create and edit
.envfile base on.env.example - Run
npm installto install dependencies - Run
npm run migration:generateandnpm run migration:runto generate and run database migration - Run
npm run start:devto start in watch mode
@db: located atsrc/db, contains entities and other files relating to database.@modules: located atsrc/modules, contains modules that prepresents the features of the project.@utils: located atsrc/utils, contains utilities.@errors: located atsrc/errors, contains error definitions.@providers: located atsrc/providers, contains 3rd party services like AWS, Google, etc.
Inside @db module, we have the following files:
datasource.ts: the TypeORM datasource file, which is the connection driver that connect our code and database.database.module.ts: NestJS module that import TypeORM root module with datasource config.
Guide for creating entity (example: Sample entity):
- Create your
sample.entity.tsfile inside theentitiesfolder:
@Entity()
export class Sample {
// Your column definitions
}- Export entity in
index.ts:
export * from './sample.entity'- Re-run migration to apply change to database:
npm run migration:generate
npm run migration:runThe @modules module is the root folder of all the main feature modules in our project. Example for creating a new feature module (Sample module):
- Create a module folder name
sample
@modules
| sample
| Your files here...
- Create module file and service file:
sample.module.ts,sample.service.ts
// sample.service.ts
@Injectable()
export class SampleService {
// your code...
}// sample.module.ts
@Module({
providers: [SampleService],
})
export class SampleModule {}- Export them in
index.tsfile:
export * from './sample.module'
export * from './sample.service'- Other folders and files convention:
dto: contains request/response DTOserrors: contains module error- Other related files like
pipe,filter,guard, etc.
@utils module provides utilities to out project. These are some important files:
env.ts: providesEnvconstants, edit this file whenever an environment variable change.validation.pipe.ts: provides global data transform & validation.iaa-exception.filter.ts: provides global error catching and filtering.api-response.dto.ts: provides a standardizing response DTO for app. Usage example:
@Get()
getSomething() {
return new ApiResponseDto(something, pagination, mesasge);
}@errors module provides top-level error definition. Every error should inherit ApiError, which is located at api-error.ts. Usage example:
// sample.error.ts
export class SampleError extends ApiError<DataType> {
constructor(data: DataType) {
super({
code: 'sample_err',
message: 'Sample Error!',
detail: data,
})
}
}You can provide module scope error by creating an errors folder at your module and provide error definitions inside that folder.
@modules
| sample
| errors
| your-error-name.error.ts
| index.ts
| sample.module.ts
| ...
When throwing error, it is catched by the exception filter.
@providers module adds a buffer layer to 3rd party services which enable us to customize their SDKs or APIs for easy usage. The structure of our provider is almost the same as the @modules module, except we will have a provider module and provider's service modules inside of it.
@providers
| aws
| modules
| s3
| s3.module.ts
| s3.service.ts
| index.ts
| aws.module.ts
| aws.service.ts
| index.ts