Socket
Socket
Sign inDemoInstall

egg-sequelize-typescript-test

Package Overview
Dependencies
41
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    egg-sequelize-typescript-test

egg Sequelize typescript plugin


Version published
Maintainers
1
Created

Readme

Source

egg-sequelize-ts

更改

插件只是将 egg-sequelize 中的 sequelize 替换为 sequelize-typescript, 同时保证用户在 egg.js 创建的项目中使用 egg-sequelize 的方法尽量一致,在使用时的不同,我将下面一一阐述。 其他内容部分请查看 egg-sequelize。 此插件已在生产项目中得到实践。

目的

能让使用 typescript 编写的 egg.js 项目中能够使用 sequelize方法,并同时得到egg.js所赋予的功能。

安装

$ npm i --save egg-sequelize-ts
$ yarn add egg-sequelize-ts

配置

  • 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}.jsconif/config.{env}.js 中编写 sequelize 配置
    config.sequelize = {
        dialect: 'mysql',
        host: '127.0.0.1',
        port: 3306,
        database: 'database'
    };

例子

分别以 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;
  }
}

Keywords

FAQs

Last updated on 09 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc