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)

  1. 替换 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;
}
}

了解详情,请点击commit

  1. 替换loader.js中的内容
  • 使用('sequelize-typescript').Sequelize替换sequelize

  • filter(model) 中使用findAll 替换sequelize,因为seq-ts在执行addModels才会有 sequelize

  • seq-ts需要手动添加models

    sequelize.addModels(models);

    了解详情,请点击commit

    如果对您有帮助,欢迎 star 和推荐 (✿◡‿◡)

egg-sequelize-ts 插件的更多相关文章

  1. 搭建项目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 ...

  2. Vue/Egg大型项目开发(一)搭建项目

    项目Github地址:前端(https://github.com/14glwu/stuer)后端(https://github.com/14glwu/stuer-server) 项目线上预览:http ...

  3. sequelize 学习笔记

    使用 eggjs 和 sequelize 进行开发,一些要注意的地方 1.egg 的 egg-sequelize 插件是 sequelize 的V4版本,目前已经更新到V5版本,API有一些变化,比如 ...

  4. egg源码浅析一npm init egg --type=simple

    要egg文档最开始的时候,有这样的几条命令: 我们推荐直接使用脚手架,只需几条简单指令,即可快速生成项目: $ mkdir egg-example && cd egg-example ...

  5. Serverless + Egg.js 后台管理系统实战

    本文将介绍如何基于 Egg.js 和 Serverless 实现一个后台管理系统 作为一名前端开发者,在选择 Nodejs 后端服务框架时,第一时间会想到 Egg.js,不得不说 Egg.js 是一个 ...

  6. egg.js-基于koa2的node.js入门

    一.Egg.JS 简介 Egg.JS是阿里开发的一套node.JS的框架,主要以下几个特点: Egg 的插件机制有很高的可扩展性,一个插件只做一件事,Egg 通过框架聚合这些插件,并根据自己的业务场景 ...

  7. 使用pkg打包node.js项目(egg框架)为可执行包

    问题: 公司有个工具型项目使用node.js 开发,需要部署到客户的服务器中,遇到的问题: 1.客户的服务器没有外网.环境配置,依赖安装等都比较麻烦,只能手工上传,最好能一个文件直接搞定: 2.直接包 ...

  8. egg.js 相关

      egg sequelize 建表规范 CREATE TABLE `wx_member` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT 'primary key' ...

  9. 使用 vite 构建一个表情选择插件

    初始化 Vite 基于原生 ES 模块提供了丰富的内建功能,开箱即用.同时,插件足够简单,它不需要任何运行时依赖,只需要安装 vite (用于开发与构建)和 sass (用于开发环境编译 .scss ...

  10. 基 vue-element-admin升级的Vue3 +TS +Element-Plus 版本的后端管理前端解决方案 vue3-element-admin 正式对外发布,有来开源组织又一精心力作,毫无保留开放从0到1构建过程

    项目简介 vue3-element-admin 是基于 vue-element-admin 升级的 Vue3 + Element Plus 版本的后台管理前端解决方案,是 有来技术团队 继 youla ...

随机推荐

  1. 西门子S7-1200与 G120系列变频器USS通信

    西门子S7-1200 紧凑型PLC在当前的市场中有着广泛的应用,作为经常与SINAMICS G120系列变频器共同使用的PLC,其USS通信协议的使用一直在市场上有着非常广泛的应用.本文将主要介绍如何 ...

  2. Elasticsearch(一)开启外网访问

    1. 设置Elasticsearch对外访问的Host 修改Elasticsearch配置文件 elasticsearch.yml : network.host: 128.24.108.84  //在 ...

  3. GPS常识-B版(简)

    第一章 绪论 1.简述GPS系统的特点有哪些? 在测绘工程中有如下优点:(1)定位精度高(2)观测时间短(3)测站间无需通视(4)可提供地心坐标(5)操作简便(6)全天候作业(7)功能多.应用广 GP ...

  4. Java编程思想:File类list()方法

    import java.util.regex.Pattern; import java.io.File; import java.io.FilenameFilter; public class Tes ...

  5. mybatis学习(五)(动态mybatis(多条件查询))

    有时候要查询条件是多条件的,尤其是使用mybatis的时候如何创建sql语句呢? 这里mybatis有自己的办法,如下: 案例:通过传入map,根据map里面的数据来查询 mapper配置如下: &l ...

  6. flash put_movie loadmovie 区别

    put_Movie 应该是c++的函数用来往程序加载一个swf用的,as3里没有loadmovie是flash用的,用来加载另一个swf或jpeg文件 不过这个loadmovie这个函数是在as2中用 ...

  7. c++小游戏——扫雷

    #include<cstdio> #include<cstring> #include<algorithm> #include<conio.h> #in ...

  8. Excel催化剂开源第9波-VSTO开发图片插入功能,图片带事件

    图片插入功能,这个是Excel插件的一大刚需,但目前在VBA接口里开发,如果用Shapes.AddPicture方法插入的图片,没法对其添加事件,且图片插入后需等比例调整纵横比例特别麻烦,特别是对于插 ...

  9. sql注入------基于时间延迟benchmark函数注入脚本

    #author:windy_2import requests urlx = 'http://127.0.0.1/?id= 1 and if((substr((select database()),' ...

  10. python-if条件判断与while/for循环

    条件判断if 让计算机像人一样,能判断是非对错,根据条件做一些事情. if ''' ------ if代码结构:------- if 条件: 代码体 tips:同一缩进范围内的代码被视作同一代码体,p ...