# egg-sequelize-typescript

> Egg Sequelize typescript plugin, updated the latest dependencies

Latest version **2.0.0** (published 2024-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install egg-sequelize-typescript
pnpm add egg-sequelize-typescript
yarn add egg-sequelize-typescript
bun add egg-sequelize-typescript
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2024-03-10 |
| First published | 2024-03-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 4 |
| Unpacked size | 10.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | jtyjty99999, stone-lyl, ZeekoZhu |
| Maintainers | devilyouwei |
| Keywords | egg, sequelize, egg-plugin, eggPlugin, orm, typescript |

## Links

- npm: https://www.npmjs.com/package/egg-sequelize-typescript
- Repository: https://github.com/devilyouwei/egg-sequelize-ts
- Issues: https://github.com/stone-lyl/egg-sequelize-ts/issues
- npm.io page: https://npm.io/package/egg-sequelize-typescript

## Dependencies (4)

- [sequelize](https://npm.io/package/sequelize.md) ^6.37.1
- [mz-modules](https://npm.io/package/mz-modules.md) ^2.1.0
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.2.1
- [sequelize-typescript](https://npm.io/package/sequelize-typescript.md) ^2.1.6

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2024-03-10

## README

# egg-sequelize-typescript

## 更改

npmjs 上的的 egg-typescript 基本都停止维护了

本项目已更新了所有的 dependencies 包，适配了最新的 typescript 版 egg.js

此插件已在生产项目中得到实践：[https://github.com/uni-openai/uniai-maas](https://github.com/uni-openai/uniai-maas。

## 目的

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

## 安装

```bash
npm i --save egg-sequelize-typescript
# or
yarn add egg-sequelize-typescript
```

## 配置

- Enable plugin in `config/plugin.js`
- 在 `config/plugin.js` 文件中引入 `egg-sequelize-typescript` 组件

```js
exports.sequelize = {
  enable: true,
  package: "egg-sequelize-typescript",
};
```

- Edit your own configurations in `conif/config.{env}.js`
  在 `conif/config.{env}.js` 中编写 sequelize 配置

```js
config.sequelize = {
  dialect: "mysql",
  host: "127.0.0.1",
  port: 3306,
  database: "database",
};
```

## 例子

分别以 `model/user.ts` 和 `service/user.js` 举例说明

> note 注意我们都是从 `sequelize-typescript` 中`import`类名，方法，属性等。

```ts
// app/model/user.ts

/**
 * @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;
```

```ts
// app/service/user.ts
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;
  }
}
```

刷新下`types`

```bash
yarn ets
```

---
_Source: https://npm.io/package/egg-sequelize-typescript · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
