本文介绍了带变压器的 TypeORM FindOperators的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用我自己的自定义转换器方法从数据库中编组/解组数据结构.

I want to use my own custom transformer methods for marshalling / unmarshalling a data structure out of the database.

所以我的实体看起来像这样:

So my entity looks something like this:

class EntityName {

  @Column({ type: `text`, transformer: { from: fromPostgres, to: toPostgres } })
  colName: CustomClass;

}

然后我显然有一个自定义类,如下所示:

Then I obviously have a custom class, along these lines:

class CustomClass {
  propertyA: string;
  propertyB: number;
}

还有我的映射函数:

fromPostgres(value: any) : any {
  let c = new CustomClass();
  c.propertyA = value.split(" ")[0];
  c.propertyB = parseInt(value.split(" ")[1]);
  return c;
}

toPostres(value: any) : any {
  return `${value.propertyA} ${value.propertyB}`
}

等等.显然这不是用例,但您明白了.

and so forth. Obviously this is not the use-case but you get the idea.

所以这一切都很好,没问题.除非您使用 FindOperators 进行查询.你知道,像这样:

So this all works great, no problem. Except when you do a query with FindOperators. You know, something like this:

let c = new CustomClass();
c.propertyA = "a";
c.propertyB = 12;
getRepository(EntityName).find({where: { colName: LessThan(c) } });

当我检查 toPostgres 方法时,我传入的是 FindOperator 对象.这具有存储在 _value 属性中的底层 CustomClass 实例(可通过 get value() getter 访问).这一切都很好,但我不确定如何将其解析为 SQL,因为无论我为 toPostgres 方法编写什么,例如提取 .value 并在其上调用 toPostgres,生成的 SQL 查询都不会做一个小于查询,它寻找一个正常的相等.

When I inspect the toPostgres method, what I'm getting passed in there is the FindOperator object. This has the underlying CustomClass instance stored in the _value property (accessible through the get value() getter). This is all well and good, but I'm not sure how this gets parsed into the SQL, since whatever I write for the toPostgres method, like for example extracting out the .value and calling toPostgres on that, the SQL query generated does not do a less than query, it looks for a normal equal.

你知道,你会得到这样的结果:

You know, you get something like this:

WHERE "EntityName"."colName" = $1 -- PARAMETERS: ["a 12"]

而不是我想要的:

WHERE "EntityName"."colName" < $1 -- PARAMETERS: ["a 12"]

那么,我是否必须为我的自定义类实现整个 FindOperators 范围?(我真的希望不会!).如果我这样做了,我如何强制可能是更高级别的函数将 SQL 查询修改为小于而不是相等.

So, am I going to have to implement the entire range of FindOperators for my custom class? (I really hope not!). And if I do, how do I force what is presumably a higher-level function to modify the SQL query to be less than instead of equal.

或者,我是否只是通过递归地将 toPostgres 应用于值来修改 FindOperator,然后让查询构建器完成剩下的工作.这是我的偏好,但据我所知 _value 是私有财产,所以我不确定如何继续这种思路.

OR, do I just modify the FindOperator somehow, by applying toPostgres to the value recursively and let the query builder do the rest. This is my preference, but as far as I can tell _value is a private property so I'm unsure how to proceed with that line of thinking.

我真的希望 TypeORM 足够聪明,可以继续做它已经用 FindOperator 做的任何事情,并且只是将转换器函数应用于我的基础值,但情况似乎并非如此,除非有其他一些我错过的配置?

I was really hoping that TypeORM would be smart enough to continue to do whatever it already does with FindOperator and just apply the transformer function to the underlying value for me, but it doesn't seem to be the case unless there's some other configuration that I've missed?

推荐答案

好的,在阅读了几个小时的代码以及如何在 TypeORM 中组装查询之后,我想出了以下解决方案.

Ok so after several hours of reading through the code and how queries are assembled in TypeORM, I've come up with the following solution.

toPostgres(value : any) : any {
    let returnValue = null;
    if(! value) {
      return null;
    } else if(value instanceof CustomClass) {
      returnValue = `${value.propertyA} ${value.propertyB}`;
    } else {
      let findValue = value as FindOperator<CustomClass>;
      returnValue = new FindOperator<CustomClass>( findValue[`_type`] as FindOperatorType, toPostgres(findValue.value), findValue.useParameter, findValue.multipleParameters)
    }
    return returnValue;
  }

换句话说,我必须递归地将转换器函数应用于所有 FindOperators 的 _value 属性.

In other words, I had to recursively apply the transformer function to the _value property of all FindOperators.

这篇关于带变压器的 TypeORM FindOperators的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 15:00