The database

Nest is database agnostic and can easily integrate with any SQL or NoSQL database:

  1. Load an appropriate for the databaseNode.jsDriver.
  2. Just use whatever genericNode.jsDatabase integration library orORM, such as:Sequelize (recipe),knexjs(tutorial) andTypeORMTo operate at a higher level of abstraction.
  3. With a ready-madeTypeORM(@nestjs/typeorm) tight integrations: These integrations provide additional specificity tonestjsFeatures such as model/repository injection, testability, and asynchronous configuration to make it easier to access your chosen database.

TypeORM integration

TypeORM is TypeScript’s most mature object relational mapper (ORM) and provides support for many relational databases, such as: PostgreSQL, Oracle, Microsoft SQL Server, SQLite, and even NoSQL databases like MongoDB.

Nest provides the @NestJS/Typeorm package. Simply install the relevant client API libraries for the selected database.

  1. Dependencies required to install.

    npm install --save @nestjs/typeorm typeorm mysql2
    Copy the code
  2. Import TypeOrmModule into AppModule.

    // app.module.ts
    
    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    
    @Module({
      imports: [
        TypeOrmModule.forRoot({
          type: 'mysql'.host: 'localhost'.port: 3306.username: 'root'.password: 'root'.database: 'test'.entities: [].synchronize: true,})]})export class AppModule {}
    Copy the code

    The forRoot() method supports configuration attributes exposed by the createConnection() function in all TypeORM packages, as detailed.

    parameter instructions
    retryAttempts Number of database retries (default: 10)
    retryDelay The interval between two retry connections (ms) (Default: 3000)
    autoLoadEntities Automatic loading of entity identifiers (default:false, automatic loading)
    keepConnectionAlive Whether the connection remains after the application is closed (default:false, closed)

    Json file and then call forRoot() without any options:

    // ormconfig.json
    {
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,}Copy the code
    // app.module.ts
    
    import { Module } from '@nestjs/common';
    import { TypeOrmModule } from '@nestjs/typeorm';
    
    @Module({
      imports: [
        TypeOrmModule.forRoot(),
      ],
    })
    export class AppModule {}
    Copy the code

    However, the ormconfig.json file is loaded by the TypeOrm library, and any attributes other than the above parameters are not applied. Such as autoLoadEntities and retryDelay(), which are internally supported by the forRoot() method.

Repository pattern

TypeORM supports the repository design pattern, so each entity has its own repository. These repositories can be obtained from the database connection (dependency package: Typeorm-Model-generator).

entity

  1. Define an entity.

    / * * *@description: real PK user list entity file *@update: the 2021-09-11 18:33:41 *@author: Ada.H
     */
    
    // pk-user.entity.ts
     
    import { Entity, Column } from 'typeorm';
    
    @Entity('user_is_kp_info', { schema: 'operator_data' })
    export class PkUser {
      @Column('varchar', {
        primary: true.name: 'phone'.comment: 'phone'.length: 16,})phone: string
      
      @Column('varchar', {
        name: 'user_name'.nullable: true.comment: 'User name'.length: 64,})userName: string | null
    
      @Column('varchar', {
        name: 'post_name'.nullable: true.comment: The 'role'.length: 512,})postName: string | null
    
      @Column('varchar', {
        name: 'is_kp'.comment: KP user or not?.length: 2.default: () = > "' 1 '",})isKp: string
      
      @Column('datetime', {
        name: 'update_time'.nullable: true.comment: 'Update Time'.default: () = > 'CURRENT_TIMESTAMP',})updateTime: Date | null
    
      @Column('datetime', {
        name: 'create_time'.nullable: true.comment: 'Creation time',})createTime: Date | null
    }
    Copy the code

    The pk-user entity is in the pk-users directory, which contains all files related to the pk-users.module module. You can customize where to save the model files, but it is recommended to save the model files in the appropriate module directory.

  2. Insert it into the entities array under the option of the module’s forRoot() method to let TypeORM know of its presence.

    // app.module.ts
    
    import { PkUser } from './pk-users/pk-user.entity'
    
    // Non-static global force, manually inserted separately
    entities: [PkUser],
    
    // Static global path, no need to insert separately
    entities: ['dist/**/*.entity{.ts,.js}'].Copy the code
  3. The forFeature() method is used in the business module to define which repositories are registered in the current scope.

    // pk-users.module.ts
    
    import { Module } from '@nestjs/common'
    import { TypeOrmModule } from '@nestjs/typeorm'
    import { PkUsersService } from './pk-users.service'
    import { PkUsersController } from './pk-users.controller'
    import { PkUser } from './entities/pk-user.entity'
    
    @Module({
      imports: [TypeOrmModule.forFeature([PkUser])],
      controllers: [PkUsersController],
      providers: [PkUsersService],
    })
    export class PkUsersModule {}
    Copy the code
  4. Inject PkUsersRepository into PkUsersService using the @Injectrepository () decorator.

    // pk-users.service.ts
    
    import { Injectable } from '@nestjs/common';
    import { InjectRepository } from '@nestjs/typeorm';
    import { Repository } from 'typeorm';
    import { PkUser } from './pk-user.entity';
    
    @Injectable(a)export class PkUsersService {
      constructor(
        @InjectRepository(PkUser)
        private pkUsersRepository: Repository<PkUser>
      ) {}
    
      findAll(): Promise<PkUser[]> {
        return this.pkUsersRepository.find();
      }
    
      findOne(id: string) :Promise<PkUser> {
        return this.pkUsersRepository.findOne(id);
      }
    
      async remove(id: string) :Promise<void> {
        await this.pkUsersRepository.delete(id); }}Copy the code