当前位置:网站首页>egg-sequelize JS编写
egg-sequelize JS编写
2022-07-26 04:20:00 【YY小怪兽】
1.安装Egg脚手架
npm i egg-init -g
2.通过脚手架创建Egg TS项目
egg-init xxx(项目名称) --type=ts
cd xxx
npm install
3.安装egg-requelize(当前安装的为js版本)
npm install --save egg-sequelize mysql2
4.在 config/plugin.js中引入 egg-sequelize 插件
exports.sequelize = {
enable: true,
package: 'egg-sequelize',
};
ts项目为config/plugin.ts
sequelize: {
enable: true,
package: 'egg-sequelize',
}
4.在 config/config.local.ts 中编写 sequelize 配置
config.sequelize = {
dialect: 'mysql',
host: '127.0.0.1',
username: 'root',
password: '',
port: 3306,
database: 'it666',
};
ts项目为config/config.local.ts
config.sequelize = {
dialect: 'mysql',
host: '127.0.0.1',
username: 'root',
password: '',
port: 3306,
database: 'it666',
};
5.在app目录下创建model目录,在model目录下创建user.ts
'use strict';
module.exports = app => {
const {
STRING, INTEGER, DATE } = app.Sequelize;
const User = app.model.define('user', {
id: {
type: INTEGER, primaryKey: true, autoIncrement: true },
name: STRING(30),
age: INTEGER,
created_at: DATE,
updated_at: DATE,
});
return User;
};
6.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.终端运得到数据库中users表的全部数据
npm run dev
边栏推荐
- 解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
- [SVN] please execute the 'cleanup' command appears all the time. The solution is that there is no response after cleanup
- How to choose the key words of the thesis?
- How to write the abbreviation of the thesis?
- RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
- HelloWorld case analysis
- Web Test Method Encyclopedia
- AcWing. 102 best cattle fence
- Dynamic planning for stair climbing
- MySQL usage
猜你喜欢

This article takes you to graph transformers

Unable to find sygwin.s file during vscode debugging

dijango学习

Recommendation Book Educational Psychology: a book for tomorrow's teachers~

Luoda Development -- the context of sidetone configuration

Makefile knowledge rearrangement (super detailed)

Share | 2022 big data white paper of digital security industry (PDF attached)

零售连锁门店收银系统源码管理商品分类的功能逻辑分享

Apisex's exploration in the field of API and microservices

How mantium uses deepspeed to implement low latency gpt-j reasoning on Amazon sagemaker
随机推荐
Under the high debt of Red Star Macalline, is it eyeing new energy?
使用百度飞桨EasyDL完成垃圾分类
机器学习之桑基图(用于用户行为分析)
Recommendation | DBT skills training manual: baby, you are the reason why you live
生活相关——十年的职业历程(转)
How to choose the key words of the thesis?
Scroll view pull-down refresh and pull-up load (bottom)
Maximum average value of continuous interval
【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
1. Mx6u-alpha development board (GPIO interrupt experiment)
How to write abstract in English thesis?
VM virtual machine has no un bridged host network adapter, unable to restore the default configuration
低成本、快速、高效搭建数字藏品APP、H5系统,扇贝科技专业开发更放心!
How to write the abbreviation of the thesis?
图互译模型
Dynamic planning for stair climbing
Acwing brush questions
Low cost, fast and efficient construction of digital collection app and H5 system, professional development of scallop technology is more assured!
生活相关——一个华科研究生导师的肺腑之言(主要适用于理工科)