 由于我们实现了一套插件机制，我们可以轻松的自定义自己需要的orm，作为插件导入。

 在这里，使用我自己开发的burn-sequelize插件来操作mysql数据库。


准备
===
```
npm install --save burn-sequelize
```



第一步，config目录下新增plugin.ts
===
```ts
export default {
    sequelize: {
        enable: true,
        package: "burn-sequelize"
    }
}
```

第二步，config.default.ts中配置sequelize各项数据

```ts
export default {
    default: 'default config string',
    sequelize: { // sequelize 配置
        dialect: "mysql", // db type
        database: 'blog',
        host: "localhost",
        port: "3306",
        username: "你的账号",
        password: "你的密码",
        dialectOptions: {
            charset: "utf8mb4",//为了支持emoji和中文
            collate: "utf8mb4_unicode_ci",//为了支持emoji和中文
            supportBigNumbers: true,
            bigNumberStrings: true
        },
        define: {
            'underscored': true,
            charset: 'utf8mb4'//为了支持emoji和中文
        },
    }
}
```

第三步，新增一个model文件夹，新建article.ts文件
====


```
.
├── README.md
├── app
├── nodemon.json
├── package-lock.json
├── package.json
├── src
│   ├── config
│   │   ├── config.default.ts
│   │   ├── config.dev.ts
│   │   └── plugin.ts
│   ├── controller
│   │   └── index.ts
│   ├── model
│   │   └── article.ts   ///这里新增
│   ├── service
│   │   └── svs.ts
│   └── start.ts
└── tsconfig.json


```


```ts
//article.ts
import { Burn } from 'burnjs';
import { Sequelize, BIGINT, TEXT, STRING, Model } from 'sequelize';

export interface ArticleModel {
    title: string
    content: string
    articleID: string
}

declare module 'burnjs' {
    export interface Burn {
        Sequelize: Sequelize;
    }
}
declare module "koa" {
    export interface BaseContext {
        model: {
            article: Model<{}, {}>,
            [key: string]: any
        }
    }
}

export default (app: Burn) => {
    const article = app.Sequelize.define('article', {
        id: {
            type: BIGINT,
            primaryKey: true,
            unique: true,
            autoIncrement: true
        },
        title: STRING(64),
        content: TEXT,
        articleID: STRING(64)
    });
    app.Sequelize.sync();
    return article
}

```

主要看``export default``这里，倒出一个函数，函数的参数是app.
注意最后要把模型返回，然后在controller或者service中
```ts
await this.ctx.model.article.findAll(....)
```
这样使用了。


特别提醒1:增加typescript支持
===
```ts
declare module 'burnjs' {
    export interface Burn {
        Sequelize: Sequelize;
    }
}
declare module "koa" {
    export interface BaseContext {
        model: {
            article: Model<{}, {}>,
            [key: string]: any
        }
    }
}
```
这个是typescript的类型拓展，不仅使得我们的ts能编译正确，还使得我们在使用vscode的时候，能够获得智能提示，非常方便。



特别提醒2
=====

代码这里
```ts
await this.ctx.model.article.findAll(....)
```
其中的article是文件名，也就是``article.ts``的名字，而不是我们导出的类名。

这样的好处就是，我们看到文件名，就知道键值了，不用再跑到文件里看。
