在线QQ客服:1922638
专业的SQL Server、MySQL数据库同步软件
nest.js使用内置的数据库处理模块,该模块不仅支持MySQL,还支持许多不同的数据库。
我们选择TypeORM是因为它肯定是迄今为止最成熟的对象关系映射器(ORM)。
使用时:
1
npm install--save @ nestjs/typeorm typeorm mysql
安装完成后,在app.module.ts中添加数据库配置:
1
2
3
4
5
6
7
8
> 9
10
11
12
13
14
15
16
17
18
import {Module} from " @ nestjs/common";
导入 {TypeOrmModule} 来自 " @ nestjs/typeorm";
@模块({
进口:[
TypeOrmModule.forRoot({
类型:" mysql",
主机:" localhost",
端口:3306,
用户名:" root",
密码:"根",
数据库:" test",
实体:[__dirname + " *。 {.ts,.js}"],
同步:true,
}),
],
})
导出 class ApplicationModule {}
另外,您还可以在项目根目录下的文件中写入mysql配置参数。建议项目使用此方法来配置数据库连接。
我尝试了一下,发现它对配置文件没有用。我可能错过了一些配置。可以正确连接当前直接在app.module中进行写入的方式。
上面已经描述了mysql数据库的连接方法,在此不再赘述。
设置数据库中已经存在的消息表,您需要在程序中定义一个对应的消息表。
在PS中。 nest.js,数据库实体以名词单数命名,服务,控制器和模块以名词复数命名。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//消息/message.entity.ts
import {列,实体,PrimaryGeneratedColumn ,BaseEntity} from " typeorm";
//表名可以在此处修改
//@Entity("消息")
@实体()
导出 class 消息 扩展 BaseEntity {
@PrimaryGeneratedColumn()
id:数字;
@Column(" int",{ 名称:" user_id"})
userId:数字;
@Column(" text",{ 名称:" content"})
content:string;
@Column(" int",{ 名称:" to_user_id"})
toUserId:数字;
@Column(" datetime",{ 名称:" created_time"}))
createdTime:日期;
@Column(" int")
creator:number;
@Column(" datetime",{ 名称:" Updated_time"})
UpdatedTime:日期;
@Column(" int")
更新:数字;
}
我发现了一个问题在项目实践期间定义实体字段,然后,在程序启动时,它将自动同步到数据库表结构,其中字段命名将更改。因此,在定义数据库实体时,注意不要忽略字段。
此同步选项应该是可定义的,并且应该是将原来定义的数据库连接参数中的该字段设置为的原因。
在其中,添加与ORM相关的处理逻辑:
1
2
3
4
5
6
7
8
9
10
11
12
14
15
16
17
18
19
20
重要t {Injectable} from " @ nestjs/common";
导入 {消息} 来自 " http://zhuyuwei.cn/2019/interfaces/message.interface";
//ORM
import {InjectRepository} from " @ nestjs/typeorm";
导入 {存储库} from " typeorm"; /span>
导入 {消息} from " http://zhuyuwei.cn/2019/message.entity";
@Injectable()
导出 class MessagesService {
const ructor(
@InjectRepository(消息)
私人只读消息存储库:存储库\ lt;消息\ gt;,
){}
私人只读消息:Message [] = [];
异步 findAll():承诺 \ lt;消息[] \\ u> {
返回 等待 这个.messagesRepository.find();
}
}
在其中,添加与服务相关的处理逻辑并定义路由:
1
2
3
4
5
6
7
8
9
10
11
12
13
import {Controller,Get} from " @ nestjs/common" ;
导入 {MessagesService} from " http://zhuyuwei.cn/2019/messages.service";
导入 {消息} 来自 " http://zhuyuwei.cn/2019/message.entity";
@Controller("消息")
导出 class MessagesController {
构造函数(私有只读messagesService:MessagesService){}
@Get()
findAll():Promise \ lt;消息[] \\ u> {
return this.messagesService.findAll();
}
}
修改文件,添加服务,控制器和其他与ORM相关的处理模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
导入 {模块} from " @ nestjs/common";
导入 {MessagesService} 来自 " http://zhuyuwei.cn/2019/messages.service';
导入 {MessagesController} 来自 " http://zhuyuwei.cn/2019/messages.controller";
import {TypeOrmModule} 来自 " @ nestjs/typeorm";
导入 {消息} 来自 " http://zhuyuwei.cn/2019/message.entity";
@ Module({
进口:[TypeOrmModule.forFeature([Message])],
提供程序:[MessagesService],
控制器:[MessagesController]
})
导出 class MessagesModule {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
导入 {Module} from " @ nestjs/common";
导入 {TypeOrmModule} from " @ nestjs/typeorm";
导入 {连接} from " typeorm";
导入 {AppController} 来自 " http://zhuyuwei.cn/2019/app.controller";
导入 {AppService} from " http://zhuyuwei.cn/2019/app.service";
导入 {MessagesModule} from " http://zhuyuwei.cn/2019/messages/messages.module";
@Module({
进口:[
TypeOrmModule.forRoot({...}),
MessagesModule,
],
控制器:[
AppController,
],
提供者:[AppService],
})
导出 class AppModule {
构造函数(私有只读连接:连接){}
}
p>定义完上述所有步骤后,因为由于uter已添加到路由中,因此请访问:http://localhost:3000/messages您将看到数据表中的所有数据均已作为列表输出JSON。
使用ActiveRecord模式的数据模型进行编写,代码可以更加简洁。
继承基类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
导入 {Column,Entity,PrimaryGeneratedColumn,BaseEntity} from " typeorm";
@ Entity()
导出 class 消息 扩展 BaseEntity {
@PrimaryGeneratedColumn()
id:数字;
@Column(" int")
user_id:数字;
@Column(" text")
content:字符串;
@Column(" int")
to_user_id:数字;
@Column(" datetime")
created_time:Date;
@Column(" int")
创建者:数字;
@Column(" datetime")
更新时间:日期;
@Column(" int")
更新:数字;
}
图>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import {可注射} 来自 " @ nestjs/common";
导入 {Message} from " http://zhuyuwei.cn/2019/message.entity";
@可注入()
导出 class MessagesService {
异步 save():Promise \ lt;消息> {
const message = new Message();
message.user_id = Math.round(Math.random ()* 1000);
message.to_user_id = 数学.round(数学.random ()* 1000); message.content = "邮件内容";
return 等待 message.save();
}
异步 findAll():Promise \ lt;消息[] \\ u> {
return await /span> Message.find();
}
}
可以直接在浏览器中打开GET请求以查看效果。 POST请求将使用该工具来实现模拟调用。
1
curl-X POST--data "" http://本地主机:3000/条消息
默认情况下,curl发送数据。如果要以JSON格式发送数据,则可以通过-H参数修改Content-Type。
1
curl-H " Content-Type:application/json"-X POST--data " {" user_id":3000," to_user_id":4000," content":" xxxyyyzzz"}" http://本地主机:3000/消息
图>