本次学习使用的是nestjs+sequelize管理mysql
先记录本次遇到的坑
按道理应该使用sequelize-typescript
但是听说要什么版本匹配 就没折腾
教程有说可以使用原始sql语句,我为了学习sequelize的api 一直在折腾sequelize的模型搭建
首先我用到了 sequelize-auto
自动创建模型(蛮好用)
按道理是可以使用ts创建模型的 但是国内中文文档较少,翻译不懂 没太懂
语句
安装
npm install -g sequelize-auto mysql
使用
sequelize-auto -h localhost -d test -u night -p 3306 -t admin_user-t admin_user -x
遇到的一个坑:-x 后面接数据库密码 如果在一条指令中输入 会提示
Warning: using a password on the command line interface can be insecure.
将密码空行不写,然后运行上述指令会要求你输入密码,这时的密码是不可见的
因为项目整体结构是TS 生成的models文件是js 导入是无法导入成功的 将文件名改成 ts后缀导入就成功了(不知道是不是这个原因)
生成model
import { Sequelize as _Sequelize } from 'sequelize';
export default function (sequelize, DataTypes) {
return sequelize.define(
'admin_user',
{
user_id: {
autoIncrement: true,
type: DataTypes.SMALLINT,
allowNull: false,
primaryKey: true,
comment: '用户ID',
},
account_name: {
type: DataTypes.STRING(24),
allowNull: false,
comment: '用户账号',
},
real_name: {
type: DataTypes.STRING(20),
allowNull: false,
comment: '真实姓名',
},
passwd: {
type: DataTypes.CHAR(32),
allowNull: false,
comment: '密码',
},
passwd_salt: {
type: DataTypes.CHAR(6),
allowNull: false,
comment: '密码盐',
},
mobile: {
type: DataTypes.STRING(15),
allowNull: false,
defaultValue: '0',
comment: '手机号码',
},
role: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 3,
comment:
'用户角色:0-超级管理员|1-管理员|2-开发&测试&运营|3-普通用户(只能查看)',
},
user_status: {
type: DataTypes.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '状态:0-失效|1-有效|2-删除',
},
create_by: {
type: DataTypes.SMALLINT,
allowNull: false,
comment: '创建人ID',
},
create_time: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: _Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
update_by: {
type: DataTypes.SMALLINT,
allowNull: false,
defaultValue: 0,
comment: '修改人ID',
},
update_time: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: _Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '修改时间',
},
},
{
sequelize,
tableName: 'admin_user',
timestamps: false,
indexes: [
{
name: 'PRIMARY',
unique: true,
using: 'BTREE',
fields: [{ name: 'user_id' }],
},
{
name: 'idx_m',
using: 'BTREE',
fields: [{ name: 'mobile' }],
},
],
},
);
}
使用:
import user from '../../database/model/admin_user';
const users = user(sequelize,Sequelize)
然后就可以使用api啦!!!
查询实例:
try {
const res = await users.findAll( {
where:{
account_name: username
},
raw:true
});
const user = res[0]; // 查出来的结果是一个数组,我们只取第一个。
console.log(res);
if (user) {
return {
code: 200, // 返回状态码,可自定义
data: {
user,
},
msg: 'Success',
};
} else {
return {
code: 600,
msg: '查无此人',
};
}
} catch (error) {
return {
code: 503,
msg: `Service error: ${error}`,
};
}
}`
Executing (default): SELECT `user_id`, `account_name`, `real_name`, `passwd`, `passwd_salt`, `mobile`, `role`, `user_status`, `create_by`, `create_time`, `update_by`, `update_time` FROM `admin_user` AS `admin_user` WHERE `admin_user`.`account_name` = 'javascript';
执行的sql语句!
返回结果
[
{
user_id: 1,
account_name: 'JavaScript',
real_name: 'script',
passwd: '0',
passwd_salt: '0',
mobile: '0',
role: 3,
user_status: 0,
create_by: 0,
create_time: 2021-06-27T11:11:09.000Z,
update_by: 0,
update_time: 2021-06-27T11:11:09.000Z
}
]
就我而言 模型的定义确实很麻烦。
目前所了解要记住的就是(不一定非要记住)
service里面写主要逻辑
async findOne(username: string): Promise<any|null> {
try {
const res = await users.findAll( {
where:{
account_name: username
},
raw:true
});
const user = res[0]; // 查出来的结果是一个数组,我们只取第一个。
console.log(res);
if (user) {
return {
code: 200, // 返回状态码,可自定义
data: {
user,
},
msg: 'Success',
};
} else {
return {
code: 600,
msg: '查无此人',
};
}
} catch (error) {
return {
code: 503,
msg: `Service error: ${error}`,
};
}
}
controller里只写路由:
@Post('find-one')
getuset(@Body() body:any): Promise<any|null> {
console.log(body);
return this.UserService.findOne(body.username);
}
module是将两个组合起来:
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
@Module( {
imports: [],
controllers: [UserController],
providers: [UserService]
})
export class UserModule {
}
数据库语法要知道大概,毕竟面向百度编程
使用sequelize模型建立要准确
模型建立务必记住 timestamps: false,
这个报错坑了我好久
目前就想到这么多了
最后一次更新于2021-06-30
0 条评论