当前位置:网站首页>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,
},
边栏推荐
- Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
- TIA botu WinCC Pro controls the display and hiding of layers through scripts
- This article takes you to graph transformers
- Day26 job
- 测试用例设计方法之:入门试招,等价边界初探
- Segger embedded studio cannot find xxx.c or xxx.h file
- How to write the introduction and conclusion of an overview paper?
- Recommendation Book Educational Psychology: a book for tomorrow's teachers~
- Makefile knowledge rearrangement (super detailed)
- 【学习笔记】AGC041
猜你喜欢

华为高层谈 35 岁危机,程序员如何破年龄之忧?

Recommendation Book Educational Psychology: a book for tomorrow's teachers~
![[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](/img/e8/b115b85e2e0547545e85b2058a9bb0.png)
[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

荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因

Huawei issued another global convening order of "genius youth", and some people once gave up their annual salary of 3.6 million to join

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation

Recommendation | scholar's art and Tao: writing papers is a skill

Keil v5安装和使用

理性认知教育机器人寓教于乐的辅助作用

Leetcode:1184. Distance between bus stops -- simple
随机推荐
dijikstra(先预处理)+dfs,relocation truncated to fit
建设面向青少年的创客教育实验室
Yuansaka Lin wallpaper
APISIX 在 API 和微服务领域的探索
荐书 |《学者的术与道》:写论文是门手艺
Apisex's exploration in the field of API and microservices
Soft simulation rasterization renderer
生活相关——十年的职业历程(转)
How to make your language academic when writing a thesis? Just remember four sentences!
荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
Recommendation Book Educational Psychology: a book for tomorrow's teachers~
Recommendation | scholar's art and Tao: writing papers is a skill
Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理
Unable to find sygwin.s file during vscode debugging
Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
MySQL - multi table query - Cartesian product sum, correct multi table query, equivalent connection and unequal connection, inner connection and outer connection
Solution: runtimeerror: expected object of scalar type int but got scalar type double
How engineers treat open source -- the heartfelt words of an old engineer
Retail chain store cashier system source code management commodity classification function logic sharing
Under the high debt of Red Star Macalline, is it eyeing new energy?