为了账号安全,请及时绑定邮箱和手机立即绑定

【备战春招】第18天 nest typeorm

标签:
Node.js

课程名称:NestJS 入门到实战 前端必学服务端新趋势


课程章节: 第1章


课程讲师:Brian


课程内容


    

热加载

https://docs.nestjs.com/recipes/hot-reload

npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack

创建 webpack-hmr.config.js

const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');

module.exports = function (options, webpack) {
  return {
    ...options,
    entry: ['webpack/hot/poll?100', options.entry],
    externals: [
      nodeExternals({
        allowlist: ['webpack/hot/poll?100'],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin({
        paths: [/\.js$/, /\.d\.ts$/],
      }),
      new RunScriptWebpackPlugin({ name: options.output.filename, autoRestart: false }),
    ],
  };
};

在 main.ts 添加

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
	// 下面这个
  if (module.hot) {
    module.hot.accept();
    module.hot.dispose(() => app.close());
  }
}
bootstrap();

此时 hot会报错 需要安装

npm i -D @types/webpack-env

然后设置

"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch"

执行 npm run start:debug 可以进行断点调试

https://img1.sycdn.imooc.com//63f961ae0001291e15260702.jpg


typeorm

TypeOrm

typeorm是个mysql的对象关系映射器,是用typescript编写的,在nest下运行非常好。

使用typeorm,必须先安装必须的依赖关系:

npm install  @nestjs/typeorm typeorm mysql -S

forRoot() 方法接受来自 TypeOrm 包的 createConnection() 的相同配置对象,也可以创建 ormconfig.json 配置文件配置连接参数。

https://img1.sycdn.imooc.com//63f961d40001265a08050481.jpg


https://img1.sycdn.imooc.com//63f961d9000100a814030688.jpg

@Entity() 定义一个新类来创建一个实体

@PrimaryGeneratedColumn() 自动递增的主键

@Column() 列类型

 // typescript -> 数据库 关联关系 Mapping

  @OneToMany(() => Logs, (logs) => logs.user)

  logs: Logs[];

https://img1.sycdn.imooc.com//63f961e600011cfe05610107.jpg


@Injectable

将类定义为提供者

@InjectRepository() 另一方面是 @Inject() 的扩展,它采用当前传递的实体/存储库并使用一些逻辑来创建新的注入令牌.通常,此逻辑是 Repository 有时会添加 connection.此令牌与从 TypeormModule.forFeature() 为同一实体创建的令牌匹配.(旁注:如果传递的实体是存储库,则令牌只是 带有可选连接(如果不是默认连接).



@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User) private readonly userRepository:
    Repository<User>
  ){}
  findAll(){
    return this.userRepository.find()
  }
  find(username: string) {
    return this.userRepository.findOne({where: {username}})
  }
  async create(user: User) {
    const userTmp = await this.userRepository.create(user)
    return this.userRepository.save(userTmp)
  }
  update(id: number, user:Partial<User>) {
    return this.userRepository.update(id, user)
  }
  remove(id:number){
    return this.userRepository.delete(id)
  }
}

https://img1.sycdn.imooc.com//63f9622300011dec07880427.jpg







点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
25
获赞与收藏
19

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消