TypeORM postgresql 按枚举文本值排序

问题描述 投票:0回答:1

尝试按 Postgresql 上的文本值对枚举字段(大陆)进行排序。这很好用;

order by
    "Country"."continent"::TEXT DESC

当我设置 orderBy 时,需要在 TypeORM 上创建相同的结果;

orderBy = { ...orderBy, continent: value.sort };

如何在TypeOrm中使用它?

postgresql nestjs typeorm
1个回答
0
投票

我知道通过该属性订购的两种方法。

第一个是使用常规方式:

const countries = await CountryEntity.find({
    order: {
        continent: "DESC"
    }
})

另一种方式(如果您发现显式转换有用)是这样的:

const orderBy: OrderByCondition = {
  continent: `CAST("Country"."continent" AS TEXT) ${value.sort}`,
};

const countries = await Country.find({
  order: orderBy,
});
© www.soinside.com 2019 - 2024. All rights reserved.