Property in TypeORM FindOptionsWhere that may be omitted

My current task involves retrieving translations for various types of questions stored in my database. One issue I am facing is that some questions come with options while others do not.

    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,
    });

If I exclude the options property, all translations are fetched but the ones related to options are missing for questions that have them. On the other hand, if I include it, questions without options are omitted.

I am looking for a solution that allows me to avoid making multiple calls to the database, with one call including and the other excluding the options property in the FindOptionsWhere object. It would be ideal to have something like:

        options: {
          [if exists]: {
            translations: {
              lang: In([localeCode, baseLocaleCode, LocaleCode.en]),
            },
          },
        },

Do you think such an approach is feasible?

Answer №1

Consider approaching it in the following way.

By enclosing the where-statement within an array, it will be interpreted as an OR condition in SQL.

import { IsNull } from "typeorm";

const productDetails = await this.productDetailRepository.find({
  where: [
    { product: { specs: IsNull() } },
    { product: {
      options: {
        translations: {
          language: In([selectedLanguage, defaultLanguage, LanguageCodes.en]),
        },
      },
    }}
  ],
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

strange complications with importing TypeScript

In my Typescript projects, I frequently use an npm module called common-types (repository: https://github.com/lifegadget/common-types). Recently, I added an enum for managing Firebase projects named FirebaseEvent. Here is how it is defined: export enum Fi ...

Combining different types and "currying" or partial application APIs in a unique way

Here is an interface that I am working with: interface IFactory<T> extends Function { (...args: any[]): (((...args: any[]) => T)|T); } After implementing the following code snippet, an error occurred: ts] Type '((...args: any[]) => ...

Utilize puppeteer and web-vitals in NextJS to retrieve the web performance metrics of a website

I'm currently working on a basic tool in NextJS that uses puppeteer to fetch web vitals data from a given URL. However, I'm facing an issue where the results are not being printed out. What could be causing this problem? const browser = await pup ...

What is the reasoning behind TypeScript's decision to permit implicit downcasting in method parameters?

Consider the following example: interface Vehicle{ mass:number } interface InspectorClass{ inspect(v:Vehicle):void } class Car implements Vehicle{ mass = 2000 wheels = 4 } class Boat implements Vehicle{ mass = 3000 sails = 2 } ...

Is there a way to adjust the width of a table cell in Material UI using React?

I encountered a problem where I am attempting to adjust the width of a table cell, specifically in Typescript. However, I am only able to choose between medium and small sizes for TableCellProps. Is there a workaround for this issue? I am looking to expand ...

Executing a method in an Angular 2 component through its template

I've been diving deep into Angular2 lately, but I've hit a snag. Here's the template code where I'm stuck: <div class="container" *ngFor="let group of groupList"> <div class="row"> <di ...

What causes a React Native wrapped component to re-render consistently?

I have a functional component in React native (expo) that is displaying a page using the stack navigator. On this page, there is a simple color picker (react-native-wheel-color-picker), which is a native component, and a button that updates the state. I&a ...

No TypeScript error in Angular app when assigning a string to a number data type

Today, I encountered some confusion when my app started acting strangely. It turns out that I mistakenly assigned a string to a number without receiving any error alerts. Any thoughts on why this happened? id:number; Later on: this.id = ActiveRoute.params ...

Steps for updating text within an object in Angular

details = [ { event: "02/01/2019 - [Juan] - D - [Leo]", point: 72 }, { event: "02/01/2019 - [Carlo] - N - [Trish]", point: 92 } ]; I am attempting to modify the text within the titles that contain - N - or - D - The desired outcom ...

Building a React Typescript service with axios functionality

When creating a service and calling it from the required functional component, there are two different approaches you can take. 1. export const userProfileService = { ResetPassword: async (userId: string) => { var response = await http.get ...

Troubleshooting the failure of the addEventListener mouseEvent in an Angular environment

Recently, I've been encountering an issue with adding addEventListener to dynamically created HTML canvas elements. Everything was working fine before, but now none of the events seem to be triggered. Below is the code snippet I am currently using: t ...

What is the method to invoke a login function in TestCafe using a Utility class?

As a beginner in TestCafe and TypeScript, I am looking to implement a login function within a utility class and then utilize it in the beforeEach method. ...

Navigating with header tags and using the navbar in react-router

Here is the code snippet I am working with App.tsx import React, { FC, Fragment } from "react"; import Nav from "./Components/Nav/Nav"; const App: FC = () => ( <Fragment> <Nav /> </Fragment> ); export default App; Nav.tsx ...

Encountering an issue while trying to integrate custom commands using the addCommand function in WebDriverIO

When using the addCommand function to add a new command, I encountered an error message that states: [ts] Property 'WaitForElementsAmount' does not exist on type 'Client<void>'. Here is an example of the code snippet I used: br ...

Updating Angular 5 Views Dynamically Using a While Loop

I am facing an issue in my app where I have a progress bar that updates using a while loop. The problem is that the view only updates after the entire while loop has finished running, even though I am successfully changing the update progress value with ea ...

The issue with prerendering leads to a SyntaxError: Forbidden to utilize import statement in a non-module context

When attempting to prerender my Angular code by running prerender.ts as outlined in this tutorial, I encountered an issue. The error message appeared when trying to execute it using ts-node prerender.ts: import 'zone.js/dist/zone-node'; ...

Please ensure that the subscription data is fully loaded before utilizing it as input

Recently, I have been developing a service that retrieves a list of users to be used as input for a child component. However, I encountered an issue where the component loads before the users list is fully loaded. One solution I came up with is to implemen ...

Enhancing User Authentication: Vue 3 with TypeScript Login

Recently, I came across a new technology called Supabase and noticed that most resources mention registration on JavaScript instead of TypeScript. As I started working on a project using Vue 3 + TypeScript, I encountered some errors that I need help resolv ...

Calling a function within another function

In my code, I have a function that formats the price and retrieves the value needed for refactoring after upgrading our dependencies. I'm struggling with passing the form value to the amountOnBlur function because the blur function in the dependencie ...

Displaying Information in Angular Modal Windows

I'm facing an issue while trying to create an edit button for a formGroup that is initially saved. When the user clicks on the adjacent button, a modal should open with editable data. However, I encountered this error and haven't been able to res ...