nestjs monorepo模式

项目环境

  1. mac系统
  2. node v12.18.0
  3. mysql 假设已经创建了test数据库

初始化项目

nest new test

创建admin子应用

#子应用
nest g mo admin #自动生成apps/admin目录
#创建用户模块
nest g mo user #user.model
nest g s user #user.service
nest g co user #user.controller
#公用库
nest g lib db #自动生成libs目录

在libs/db目录下面创建实体目录,新建user.entitiy.ts

import { Column, Entity,PrimaryGeneratedColumn } from 'typeorm';

@Entity({ name: 'user' })
export class UserEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({ length: 20 ,comment:"用户名"})
    username: string;

    @Column({length:60, comment:"密码"})
    password: string;
}

最终目录结构如下

test
  | --- apps
     | --- admin //admin应用
        | --- src
           | --- main.ts
           | --- app.module.ts
           | --- user //用户模块
              | --- user.module.ts
              | --- user.service.ts
              | --- user.controller.ts
   | --- libs
      | --- db
         | --- src
           | --- db.module.ts //公用db模块
           | --- db.service.ts
           | --- entities //公用实体目录
              | --- user.entity.ts //用户实体

下载包

yarn add mysql @nestjs/typeorm typeorm

数据库配置

db.module.ts

@Module({
    imports: [
        TypeOrmModule.forRoot({
            type: 'mysql',
            host: '127.0.0.1',
            port: 3306,
            username: 'username',
            password: 'password',
            database: 'test',
            entities: [
              __dirname +  "/entities/*.ts" //这里有问题是 
            ],
            synchronize: true,
        })
    ],
    providers: [DbService ],
    exports: [DbService],
})
export class DbModule {
}

组合模块

app.module.ts

@Module({
    imports: [
        DbModule, //引入db公用模块,别忘记了!
        UserModule
    ],
    controllers: [AppController],
    providers: [AppService],
})

export class AppModule { }

业务代码

user.controller.ts

import { Controller, Post,Body, Param } from '@nestjs/common';
import { UserService } from './user.service';


@Controller('user')
export class UserController {

    constructor(private readonly usersService: UserService) {
    }

    @Post()
    create(@Body() data) {
        return this.usersService.create(data)
    }

}

user.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, getRepository } from 'typeorm';
import {UserEntity} from "@libs/db/entities/user.entity";


@Injectable()
export class UserService {

    constructor(
        @InjectRepository(UserEntity)
        private readonly usersRepository: Repository<UserEntity>
    ) { }

    public create(user) {
        return this.usersRepository.save(getRepository(UserEntity).create(user))
    }
}

运行测试

nest start -w admin

报错1:

No repository for “UserEntity” was found. Looks like this entity is not registered in current “default” connection

这是typeorm 找不到UserEntity实体,修改db.module.ts

db.module.ts

@Module({
    imports: [
        TypeOrmModule.forRoot({
           ...
            database: 'test',
            entities: [
             UserEntity //这里改成class
            ],
        })
...

报错2:

If UserEntityRepository is exported from a separate @Module, is that module imported within UserModule?

userService使用了UserEntity,但是在user.module中没有import

修改 user.module.ts

@Module({
    imports:[
        TypeOrmModule.forFeature([UserEntity]) //加入这一行
    ],
    providers: [UserService],
    controllers: [UserController]
})
export class UserModule { }

搞定

nestjs monorepo模式

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!