当前位置:网站首页>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,
},
边栏推荐
- HelloWorld case analysis
- 荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
- VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
- ROS2的launch有何不同?
- Design and implementation of smart campus applet based on cloud development
- 1. Mx6u-alpha development board (GPIO interrupt experiment)
- dijikstra(先预处理)+dfs,relocation truncated to fit
- Apisex's exploration in the field of API and microservices
- The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
- Keil v5安装和使用
猜你喜欢

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

机器学习之桑基图(用于用户行为分析)

Scroll view pull-down refresh and pull-up load (bottom)

How mantium uses deepspeed to implement low latency gpt-j reasoning on Amazon sagemaker

解析Steam教育的课程设计测评体系

firewall 命令简单操作

MATLAB绘图

荐书丨《教育心理学》:送给明日教师的一本书~

Life related - ten years of career experience (turn)

Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
随机推荐
荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
Recommendation | scholar's art and Tao: writing papers is a skill
1. Mx6u-alpha development board (main frequency and clock configuration experiment)
How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
I.MX6U-系统移植-6-uboot图形化配置
MySQL的优化分析及效率执行
生活相关——减少期待,更快乐
Wu Enda's machine learning after class exercises - linear regression
VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep
智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候
Acwing brush questions
解析Steam教育的课程设计测评体系
Small ball and box model, arrangement and combination
What are the duplicate check rules for English papers?
Life related - ten years of career experience (turn)
Apisex's exploration in the field of API and microservices
Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
荐书丨《教育心理学》:送给明日教师的一本书~
青少年创客教育的创意设计原理