第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

【備戰(zhàn)春招】第18天 nest typeorm

標(biāo)簽:
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







點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消