当前位置:网站首页>Compiled by egg serialize TS
Compiled by egg serialize TS
2022-07-26 04:22:00 【YY little monster】
How to be in requlize Use in TS Programming
Details visible
1. install sequelize-typescript
npm install sequelize-typescript
2. install egg-sequelize-ts take egg-sequelize Medium sequelize Replace with sequelize-typescript,
npm i --save egg-sequelize-ts
3. stay config/plugin.ts Introduce in the file egg-sequelize-ts Components
sequelize: {
enable: true,
package: 'egg-sequelize-ts',
},
4. stay config/config.local.ts The configuration requelize
config.sequelize = {
dialect: 'mysql',
host: '127.0.0.1',
username: 'root',
password: '',
port: 3306,
database: 'it666',
};
5. To write app/module/user.ts
/** * @description User table */
import {
AutoIncrement, Column, DataType, Model, PrimaryKey, Table } from 'sequelize-typescript';
@Table({
modelName: 'user',
})
class User extends Model<User> {
@PrimaryKey
@AutoIncrement
@Column({
type: DataType.INTEGER,
comment: ' user ID',
})
id: number;
@Column({
type: DataType.STRING,
comment: ' User name ',
})
name: string;
@Column({
type: DataType.INTEGER,
comment: ' Age ',
})
age: number;
@Column({
type: DataType.DATE,
field: 'created_at',
})
createdAt: Date;
@Column({
type: DataType.DATE,
field: 'updated_at',
})
updatedAt: Date;
}
export default () => User;
6. To write app/control/home.ts
import {
Controller } from 'egg';
export default class HomeController extends Controller {
public async index() {
const {
ctx } = this;
// ctx.body = await ctx.service.test.sayHi('egg');
ctx.body = await ctx.model.User.findAll();
}
}
7. function
npm run dev
8. Be careful
stay user.ts The table name we use in is user, But our table in the database is users, This is because
sequelize take ⾃ Automatically transfer all the model names (define Of the ⼀ Parameters ) Convert to plural , It will automatically change to users
define: {
// Note that you need to add this , egg-sequelize Simply make ⽤Object.assign The configuration and default configuration are merge, If you don't add this update_at Will be transformed into updateAt So report a mistake
underscored: true,
// // Forbid ⽌ Modify the name of the table , By default ,sequelize take ⾃ Automatically transfer all the model names (define Of the ⼀ Parameters ) Convert to plural
// // But for the sake of safety , The conversion of plural numbers may send ⽣ change , So no ⽌ The ⾏ by
freezeTableName: true,
},
边栏推荐
- Wu Enda's machine learning after class exercises - logical regression
- Graph theory: topological sorting
- 1. If function of Excel
- How to transfer English documents to Chinese?
- 【学习笔记】AGC041
- Getting started with mongodb Basics
- Design and implementation of smart campus applet based on cloud development
- Firewall command simple operation
- 匿名函数的作用
- MySQL only checks the reasons for the slow execution of one line statements
猜你喜欢

1. Excel的IF函数

How engineers treat open source -- the heartfelt words of an old engineer

当你尝试删除程序中所有烂代码时 | 每日趣闻

How to download the supplementary literature?

1. Mx6u system migration-6-uboot graphical configuration

青少年创客教育的创意设计原理

支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布

Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release

PathMatchingResourcePatternResolver解析配置文件 资源文件
![Acwing game 61 [End]](/img/e3/191f7d888fc8cced608d41ef837a92.png)
Acwing game 61 [End]
随机推荐
动态规划 爬楼梯
AcWing. 102 best cattle fence
青少年创客教育的创意设计原理
LeetCode:1184. 公交站间的距离————简单
生活相关——减少期待,更快乐
Soft simulation rasterization renderer
Day24 job
解析Steam教育的课程设计测评体系
Steam科学教育赋予课堂教学的创造力
Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
吴恩达机器学习课后习题——逻辑回归
Postman 导入curl 、导出成curl、导出成对应语言代码
Share | 2022 big data white paper of digital security industry (PDF attached)
This article takes you to graph transformers
荐书 |《学者的术与道》:写论文是门手艺
Day26 job
APISIX 在 API 和微服务领域的探索
Method of test case design -- move combination, causal judgment
1. Mx6u system migration-6-uboot graphical configuration
VM虚拟机 没有未桥接的主机网络适配器 无法还原默认配置