反对在 jsonSchema 中使用 virtualAttributes

Use virtualAttributes with jsonSchema in objection

我想要一个附加列,其中包含来自另一列的修改值: 现在我有下一个这样的代码,但它不起作用:

const { Model } = require("objection");  
    class TestModel extends Model {
      static get tableName() {
        return "testTable";
      }
    
    
      static get virtualAttributes() {
        return ["someNewField"];
      }
    
      someNewField() {
        return this.someField+'testString';
      }
    
      static get jsonSchema() {
        return {
          type: "object",
          properties: {
            someField:{ type: "string" },
          },
        };
      }
    }
    
    module.exports = {
      TestModel,
    };

我遇到了这样的错误

"message": "Cannot query field \"someNewField\" on type \"testTable\".",

我的 grapql 查询:

{TestModels{
  someNewField,
}

从数据库中获取数据后,在 javascript 端创建虚拟属性。所以没有这样的名为 someNewField 的列生成到 DB.

所以现在您正在尝试创建一个查询,试图从根本不存在的数据库中查找数据。

没有通用的解决方法可以使这项工作正常进行。如果你能解释你的use-case你想在这里实现什么,我可能会建议在那个特定的情况下做些什么。