跳至主要內容

连接MySql(TypeORM)

刘春龙原创...小于 1 分钟NodejsNestjs教程文档

安装

npm install --save @nestjs/typeorm typeorm mysql2

配置

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'wwlj-yunfuwu.mysql.huhehaote.rds.aliyuncs.com',
      port: 3306,
      username: "j********9",
      password: "A**********0",
      database: "jiamei",
      retryDelay: 500,
      retryAttempts: 10,
      // entities: [__dirname + '/**/*.entity(.ts,.js'],
      autoLoadEntities: true,
      synchronize: true,
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }

模型注入

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

@Entity()
export class App {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    userName: string;

    @Column()
    passWord: string;

    @Column({ default: true })
    isActive: boolean;
}

使用

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { App } from './app.entity';
@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'wwlj-yunfuwu.mysql.huhehaote.rds.aliyuncs.com',
      port: 3306,
      username: "jiamei0429",
      password: "Aa1472583690",
      database: "jiamei",
      retryDelay: 500,
      retryAttempts: 10,
      autoLoadEntities: true,
      synchronize: true,
    }),
    TypeOrmModule.forFeature([App])
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule { }



















 





上次编辑于:
贡献者: 刘春龙
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.7