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.38k stars 574 forks source link

急急急!!!头大, 我把node-modules删除掉, 重新npm i 一下, InjectEntityModel注入就都失败了, 之前的版本应该是3.5.3, 重新安装依赖, 应该变成最新的了, 最新的是要怎么额外处理一下吗? #2880

Closed huangapple closed 1 year ago

huangapple commented 1 year ago

头大, 我把node-modules删除掉, 重新npm i 一下, InjectEntityModel注入就都失败了, 之前的版本应该是3.5.3, 重新安装依赖, 应该变成最新的了, 最新的是要怎么额外处理一下吗?

现在报错的 报 注解InjectEntityModel注入的属性为空, 就类似这样

  @InjectEntityModel(SysServlet)
  servletModel: ReturnModelType<typeof SysServlet>;

报 this.servletModel为空。

代码都没改过, 就只是重新安装了依赖。

huangapple commented 1 year ago

@czy88840616 求解决思路

huangapple commented 1 year ago

跟这个问题有点像 https://github.com/midwayjs/midway/issues/921

czy88840616 commented 1 year ago

lock删掉再装,或者用npx midway-version检查版本

huangapple commented 1 year ago

不行哦。但我发现给实体类加个 @EntityModel() 注解就能成功。

但这边的文档又说不需要 , 还说不要混用 http://www.midwayjs.org/docs/extensions/mongodb

和老写法的区别
如果想使用新版本的用法,请参考下面的流程,将老代码进行修改,新老代码请勿混用。

升级方法:

1、无需再使用 EntityModel 装饰器
3、在 src/config.default 的 mongoose 部分配置调整,参考下面的数据源配置部分
3.1 修改为数据源的形式 mongoose.dataSource
3.2 将实体模型在数据源的 entities 字段中声明

我mongo相关的依赖是这个版本, 使用的mongo数据库是 mongo5

    "@typegoose/typegoose": "^9.12.1",
    "mongoose": "^6.6.7",
huangapple commented 1 year ago

npx midway-version使用结果

✓ @midwayjs/faas-typings(not installed)
✓ @midwayjs/fc-starter(not installed)
✓ @midwayjs/serverless-http-parser(not installed)
✓ @midwayjs/async-hooks-context-manager(3.11.1)
✓ @midwayjs/axios(3.11.3)
✓ @midwayjs/bootstrap(3.11.1)
✓ @midwayjs/bull-board(not installed)
✓ @midwayjs/bull(not installed)
✓ @midwayjs/cache(3.11.3)
✓ @midwayjs/captcha(not installed)
✓ @midwayjs/casbin-redis-adapter(not installed)
✓ @midwayjs/casbin-typeorm-adapter(not installed)
✓ @midwayjs/casbin(not installed)
✓ @midwayjs/code-dye(not installed)
✓ @midwayjs/consul(not installed)
✓ @midwayjs/core(3.11.1)
✓ @midwayjs/cos(not installed)
✓ @midwayjs/cron(not installed)
✓ @midwayjs/cross-domain(3.11.3)
✓ @midwayjs/decorator(3.11.1)
✓ @midwayjs/etcd(not installed)
✓ @midwayjs/express-session(not installed)
✓ @midwayjs/faas(not installed)
✓ @midwayjs/grpc(not installed)
✓ @midwayjs/http-proxy(not installed)
✓ @midwayjs/i18n(3.11.3)
✓ @midwayjs/info(3.11.3)
✓ @midwayjs/jwt(3.11.3)
✓ @midwayjs/kafka(not installed)
✓ @midwayjs/mikro(not installed)
✓ @midwayjs/mock(3.11.3)
✓ @midwayjs/mongoose(3.11.3)
✓ @midwayjs/oss(3.11.3)
✓ @midwayjs/otel(not installed)
✓ @midwayjs/passport(not installed)
✓ @midwayjs/process-agent(not installed)
✓ @midwayjs/prometheus-socket-io(not installed)
✓ @midwayjs/prometheus(not installed)
✓ @midwayjs/rabbitmq(not installed)
✓ @midwayjs/redis(not installed)
✓ @midwayjs/security(not installed)
✓ @midwayjs/sequelize(not installed)
✓ @midwayjs/session(3.11.3)
✓ @midwayjs/socketio(not installed)
✓ @midwayjs/static-file(3.11.3)
✓ @midwayjs/swagger(not installed)
✓ @midwayjs/tablestore(not installed)
✓ @midwayjs/tags(not installed)
✓ @midwayjs/typegoose(3.11.3)
✓ @midwayjs/typeorm(not installed)
✓ @midwayjs/upload(3.11.3)
✓ @midwayjs/validate(3.11.3)
✓ @midwayjs/version(3.11.3)
✓ @midwayjs/view-ejs(not installed)
✓ @midwayjs/view-nunjucks(not installed)
✓ @midwayjs/view(not installed)
✓ @midwayjs/express(not installed)
✓ @midwayjs/koa(3.11.3)
✓ @midwayjs/web(not installed)
✓ @midwayjs/ws(not installed)
**************************************************
>> Check complete, all versions are healthy.
**************************************************
huangapple commented 1 year ago

不过我还是蛮喜欢加 @EntityModel() 这个注解的, 之前在有标志的文件夹下有 export class就会被当成实体类了。有点不太方便

有这个注解就灵活了 @czy88840616

czy88840616 commented 1 year ago

那就是3.1/3.2的配置没做咯,entity没写进去。

huangapple commented 1 year ago

啥?是说midway新版本3.11.1的有点小问题, 还是说我代码之前忘了写?

czy88840616 commented 1 year ago

升级方法:

1、无需再使用 EntityModel 装饰器 3、在 src/config.default 的 mongoose 部分配置调整,参考下面的数据源配置部分 3.1 修改为数据源的形式 mongoose.dataSource 3.2 将实体模型在数据源的 entities 字段中声明

上面升级的方式写的很清楚啊,调整原有的配置到 dataSource,调整 entityModel 到 entities 字段。

改造 EntityModel 的原因是多连接池管理无法实现,每个 entity 都要关联到自己对应的 dataSource 上。

huangapple commented 1 year ago

可是我有设置扫描的目录啊

  mongoose: {
    dataSource: {
      default: {
        uri: process.env.CONFIG_MONGO_URL,
        options: {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          user: process.env.CONFIG_MONGO_USER ,
          pass: process.env.CONFIG_MONGO_PASS
        },
        // 关联实体
        entities: ['/entity']
      }
    }
  },

有设置 目录 entity

czy88840616 commented 1 year ago

目录、文件对么,理论上加上了装饰器能出现,entity 本身没有问题,只可能是路径或者文件不对导致没扫描到。

huangapple commented 1 year ago

目录文件没理由不对啊, 之前都好好的, 有git记录, 如果有不小心改到是看得到的。

只是早上删除了 node-modules 目录, 重新npm i 了一下。然后就不行了。

czy88840616 commented 1 year ago

理论上 3.5.3 也已经是调整完配置的版本了, @EntityModel 直到现在也还是会兼容性加载的,如果没有多连接需求,继续用着也没有问题。

huangapple commented 1 year ago

你之前说的 理论上加上了装饰器就能出现, 这个装饰器是指加在 entity上吗? 我之前都没加, 有加也只是加索引那些?但目前要在entity上加上 @entityModel, 别的地方需要这个实体, 才能被注入。比如

 @InjectEntityModel(SysServlet)
  servletModel: ReturnModelType<typeof SysServlet>;

有在 SysServlet 这个entity上加 @EntityModel() , 这里的 servletModel 才不会为空。之前没加@EntityModel() , servletModel 也能正常注入。

czy88840616 commented 1 year ago

加 @EntityModel 在 entity 上是很老的用法,和在配置中配置 entities 是一个作用,二选一,现在一般是后者。

huangapple commented 1 year ago

哦, 那就不懂了。那我能 不在配置中配置写 entities , 然后通过加 @entityModel 来标志实体吗? 我觉得这种更方便好用。

czy88840616 commented 1 year ago

能的。

huangapple commented 1 year ago

问题还是没解决, 我晚上弄个最小复现的demo给你们吧?

czy88840616 commented 1 year ago

huangapple commented 1 year ago

image

// 关联实体, 写成 /entity是不行的, 要写成 ./entity

但我之前就一直是写成/entity的啊

czy88840616 commented 1 year ago

我增加了 /entity 的单测,目前最新版本是没有的问题的(可能中间有小版本调整的时候有 bug)。

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.