midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.4k stars 576 forks source link

能否提供2.0的sequelize-typescript组件或者例子 #1061

Closed mkanako closed 3 years ago

mkanako commented 3 years ago

如题

mkanako commented 3 years ago

项目从egg.js换成midway,原本用的是sequelize,结果这边没啥支持,例子都没有 [裂开]

czy88840616 commented 3 years ago

https://github.com/midwayjs/midway-examples/tree/master/v2/demo-sequelize-typescript

mkanako commented 3 years ago

感谢连夜添加,我也在摸索中,看了例子,那个模型好像没有用到依赖注入那些

我自己摸索到的

import { Model, Table, Column, HasMany}  from 'sequelize-typescript';
import { providerWrapper } from '@midwayjs/core'
import { Post } from './post';

export const factory = () => User

providerWrapper([
  {
    id: 'UserModel',
    provider: factory,
  },
])

@Table
export class User extends Model {

  @Column name!: string;
  @HasMany(() => Post) posts: Post[];

}

相比有官方组件支持的TypeORM麻烦了点

czy88840616 commented 3 years ago

是想在 Model 里写方法和逻辑么?

mkanako commented 3 years ago

目前也没想这样,不过以后也许需要

现在我只是在照着TypeORM的套路学着来,把模型加载进容器,然后在service注入模型这样用而已

czy88840616 commented 3 years ago

我看了下 sequelize 和typeorm稍微有点不同,typeorm 是用实例注册,所以可以将 ioc 创出来的实例再添加到 typeorm 逻辑中,sequelize 是类注册,方法是静态的(static method),本质上就算在上面定义了方法也没有实例调用,这就不太一样的了。

按这个思路,其实 sequlize 的 model 里应该不会放逻辑才对。

mkanako commented 3 years ago

好的,感谢指点!