我正试图从我的数据库中获取不同类型问题的翻译。问题是,有些问题是有选择的,有些则没有。
const where: FindOptionsWhere<QuestionTranslation> = {
question: {
// some other props
options: { // this needs to be optional
translations: {
lang: In([localeCode, baseLocaleCode, LocaleCode.en]),
},
},
},
};
const questionTranslations = await this.questionTranslationRepository.find({
where,
});如果我删除options属性,我会得到所有的翻译,但是在有选项的问题上,选项的翻译显然是缺失的。如果我把它包括在内,没有选择的问题就会被排除在外。
我希望避免两次调用数据库(分别包含或排除options属性的FindOptionsWhere对象)。像这样的东西会很好:
options: {
[if exists]: {
translations: {
lang: In([localeCode, baseLocaleCode, LocaleCode.en]),
},
},
},这个是可能的吗?
发布于 2022-06-09 16:16:28
尝试使用以下方法。
Wrapping the where-clause in an array, will translate to an OR condition in SQL。
import { IsNull } from "typeorm";
const questionTranslations = await this.questionTranslationRepository.find({
where: [
{ question: { options: IsNull() } },
{ question: {
options: {
translations: {
lang: In([localeCode, baseLocaleCode, LocaleCode.en]),
},
},
}}
],
});https://stackoverflow.com/questions/72562311
复制相似问题