egg-sequelize-ts 插件
egg-sequelize-ts plugin
目的 (Purpose)
能让使用 typescript 编写的 egg.js 项目中能够使用 sequelize方法,并同时得到egg.js所赋予的功能。
说明 (Description)
this plugin use sequelize-typescript replace with sequelize in egg.js. at the same time, make sure of user
插件只是将 egg-sequelize 中的 sequelize 替换为 sequelize-typescript, 同时保证用户在 egg.js 创建的项目中使用 egg-sequelize 的方法尽量一致,在使用时的不同,我将下面一一阐述。 其他内容部分请查看 egg-sequelize。
此插件已在生产项目中得到实践。
安装 (Install)
$ npm i --save egg-sequelize-ts
$ yarn add egg-sequelize-ts
配置 (Config)
- Enable plugin in
config/plugin.js - 在
config/plugin.js文件中引入egg-sequelize-ts组件
exports.sequelize = {
enable: true,
package: 'egg-sequelize-ts'
}
- Edit your own configurations in
conif/config.{env}.js
在conif/config.{env}.js中编写 sequelize 配置
config.sequelize = {
dialect: 'mysql',
host: '127.0.0.1',
port: 3306,
database: 'database'
};
例子 (Example)
分别以 model/user.js 和 service/user.js 举例说明
note 注意我们都是从
sequelize-typescript中导出类名,方法,属性等。
// app/model/user.js
/**
* @desc 用户表
*/
import { AutoIncrement, Column, DataType, Model, PrimaryKey, Table } from 'sequelize-typescript';
@Table({
modelName: 'user'
})
export class User extends Model<User> {
@PrimaryKey
@AutoIncrement
@Column({
type: DataType.INTEGER(11),
comment: '用户ID',
comment: 'user id'
})
id: number;
@Column({
comment: '用户姓名',
})
name: string;
@Column({
comment: '用户邮箱'
})
email: string;
@Column({
comment: '用户手机号码'
})
phone: string;
@Column({
field: 'created_at'
})
createdAt: Date;
@Column({
field: 'updated_at'
})
updatedAt: Date;
};
export default () => User;
// app/service/user.js
import { Service } from 'egg';
import { Sequelize } from 'sequelize-typescript';
class UserService extends Service {
async index() {
const { or } = Sequelize.Op;
const users = await this.ctx.model.User.findOne({
where: {
[or]: [
{ name, phone },
{ id }
]
}
});
this.ctx.body = users;
}
}
更改内容(Major changes)
- 替换
index.d.ts文件中的类型
主要将 interface 中的Sequelize,model指向sequelize-typescript.
for example:
- import * as sequelize from "sequelize";
+ import { Sequelize, ISequelizeValidationOnlyConfig } from 'sequelize-typescript';
declare module 'egg' {
+ // 便于egg 将方法挂载到IModel上
+ interface IModel extends Sequelize { }
interface Application {
- Sequelize: sequelize.SequelizeStatic;
- model: sequelize.Sequelize;
+ Sequelize: Sequelize;
+ model: IModel;
}
}
- 替换
loader.js中的内容
使用('sequelize-typescript').Sequelize替换sequelize
在
filter(model)中使用findAll 替换sequelize,因为seq-ts在执行addModels才会有 sequelizeseq-ts需要手动添加models
sequelize.addModels(models);
如果对您有帮助,欢迎 star 和推荐 (✿◡‿◡)
egg-sequelize-ts 插件的更多相关文章
- 搭建项目vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize
vue + vue-router + vuex + vue-i18n + element-ui + egg + sequelize https://www.cnblogs.com/wuguanglin ...
- Vue/Egg大型项目开发(一)搭建项目
项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...
- sequelize 学习笔记
使用 eggjs 和 sequelize 进行开发,一些要注意的地方 1.egg 的 egg-sequelize 插件是 sequelize 的V4版本,目前已经更新到V5版本,API有一些变化,比如 ...
- egg源码浅析一npm init egg --type=simple
要egg文档最开始的时候,有这样的几条命令: 我们推荐直接使用脚手架,只需几条简单指令,即可快速生成项目: $ mkdir egg-example && cd egg-example ...
- Serverless + Egg.js 后台管理系统实战
本文将介绍如何基于 Egg.js 和 Serverless 实现一个后台管理系统 作为一名前端开发者,在选择 Nodejs 后端服务框架时,第一时间会想到 Egg.js,不得不说 Egg.js 是一个 ...
- egg.js-基于koa2的node.js入门
一.Egg.JS 简介 Egg.JS是阿里开发的一套node.JS的框架,主要以下几个特点: Egg 的插件机制有很高的可扩展性,一个插件只做一件事,Egg 通过框架聚合这些插件,并根据自己的业务场景 ...
- 使用pkg打包node.js项目(egg框架)为可执行包
问题: 公司有个工具型项目使用node.js 开发,需要部署到客户的服务器中,遇到的问题: 1.客户的服务器没有外网.环境配置,依赖安装等都比较麻烦,只能手工上传,最好能一个文件直接搞定: 2.直接包 ...
- egg.js 相关
egg sequelize 建表规范 CREATE TABLE `wx_member` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'primary key' ...
- 使用 vite 构建一个表情选择插件
初始化 Vite 基于原生 ES 模块提供了丰富的内建功能,开箱即用.同时,插件足够简单,它不需要任何运行时依赖,只需要安装 vite (用于开发与构建)和 sass (用于开发环境编译 .scss ...
- 基 vue-element-admin升级的Vue3 +TS +Element-Plus 版本的后端管理前端解决方案 vue3-element-admin 正式对外发布,有来开源组织又一精心力作,毫无保留开放从0到1构建过程
项目简介 vue3-element-admin 是基于 vue-element-admin 升级的 Vue3 + Element Plus 版本的后台管理前端解决方案,是 有来技术团队 继 youla ...
随机推荐
- 【Aizu - 0118】Property Distribution
-->Property Distribution 原文是日语,算了算了,直接上我大中华母语吧 Descriptions: 在H * W的矩形果园里有苹果.梨.蜜柑三种果树, 相邻(上下左右)的 ...
- Python Day_1
听说Python很6,然后我的偶像小甲鱼竟然在6年前(现在2019年)就开始制作Python的教程,而前不久(世界读书日前一个星期左右),京东有活动,小甲鱼的Python第一版本打折,顺便买了本(还凑 ...
- WPF 入门笔记之事件
一.事件路由 1. 直接路由事件 起源于一个元素,并且不能传递给其他元素 MouserEnter 和MouserLeave 就是直接事件路由 2. 冒泡路由事件 在包含层次中向上传递,首先由引发的元素 ...
- Mybatis__模糊查询
在一个Web工程中,查询功能几乎都要用到姓名模糊查询,,虽然学号,工号等可以最准确最快的定位,但如果清楚信息到连学号,工号都一个数不差,应该也没必要去查询了. 故需要用到一下语句实现模糊查询: sel ...
- android_layout_framelayout
framelayout最大的特点,个人认为就是N个元素都会重叠地堆在左上角,除非你手动指定位置.相对布局relative也是如此,但是他妈的人家不可以指定布局的垂直与水平,frame可以,但是指定垂直 ...
- SSAS Tabular 数据表关系与join的映射
才想起来总结这两天发现的一个有趣的现象: 在SSAS Tabular 模型中建立了关系之后,在excel中分析发现: 产品库龄作为量值:Aging Stock:=sum([DISTRIBUTOR_ST ...
- Oracle粗心大意总结篇
有时候写sql语句不细心的话,很容易犯大错误,导致你纠结好久,找不到原因,慢慢总结: 错误1: SELECT * FROM( SELECT USER.*, ROWNUM AS CON FROM USE ...
- Contos7 常用命令
```pythoncentos常用命令: 查看所有运行的单元:systemctl list-units 查看所有单元:systemctl list-units --all 查看所有启动的服务:syst ...
- TensorFlow高效读取数据的方法——TFRecord的学习
关于TensorFlow读取数据,官网给出了三种方法: 供给数据(Feeding):在TensorFlow程序运行的每一步,让python代码来供给数据. 从文件读取数据:在TensorFlow图的起 ...
- NOIp 2018 普及&提高组试题答案
你们考的咋样呢?在评论区说出自己的分数吧!