Ways to fetch data based on a specific relationship criterion

In my data structure, there is a Product entity and an Image entity with a OneToMany relationship. This means that One Product can have many Images attached to it.

When an image needs to be removed, instead of deleting the data from the table, I have chosen to simply disable it by adding a boolean column called 'deleted'. This way, when querying for the product, if the image has been deleted, it will not be included in the results.

Currently, I have a query that works perfectly when the product has an image. However, I am unsure of how to handle the scenario where the image has been deleted.

Here is the query I have:

const [ products, count ] = await this.productRepo
      .createQueryBuilder('product')
      .innerJoinAndSelect('product.images', 'image')
      .where([{ name: Like(`%${terms}%`) }, { description: Like(`%${terms}%`) }, { code: Like(terms) }])
      .orderBy('product.' + params.orderBy, params.orderMode)
      .offset(params.skip)
      .limit(params.take)
      .getManyAndCount()

    return { products, count }

Here is the structure of the Image entity:

@Entity()
export class Image extends BaseEntity {
  @Column() 
  name: string
  
  @Column({ default: false })
  deleted: boolean
  
  @ManyToOne(() => Product, product => product.images) product: Product[];
}

And here is the structure of the Product entity:

export class Product extends BaseEntity {
  @Column({ length: 20 })
  code: string

  @OneToMany(() => Image, image => image.product) images: Image[]
}

Answer №1

There is no mention of the new deleted column in the where clause. Typically, you would include something like { deleted: value } in the query.

.where([{ title: Like(`%${keywords}%`) }, { summary: Like(`%${keywords}%`) }, { tag: Like(keywords) }])

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

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...

Redis Cache expiry concept

Recently, I've come across an issue with ioredis where I have been setting a key and expiration for that key in my code. Here's a snippet of what my code looks like: let temp1 = acct.limit; let txn = array.length; let cache = new ioredis(); // p ...

core.js:15723 ERROR TypeError: Unable to access the 'OBJECT' property because it is undefined

Whenever I attempt to run this function, I encounter an issue. My goal is to retrieve the latitude and longitude of an address from Google's API. This error message pops up: core.js:15723 ERROR TypeError: Cannot read property 'geometry' of ...

Is it possible to use a TypeScript Angular (click) event with an object property as the value?

Seeking assistance in creating a dynamic error card featuring various error messages along with a retry button. Below is a snippet from my TypeScript object: errorCard: any = []; if(error) { this.errorCard.errorMessage = "Oops, please try again"; ...

Using Node.js and Express to create a MySQL API that outputs a JSON array as a string

Currently working on setting up an Express API server to fetch data for a portfolio website. I've configured the MySQL table with a JSON data type for the 'images' column, which is supposed to contain multiple image links for a gallery. Howe ...

Error: TypeScript is unable to locate the 'moment' module

My TypeScript React application was set up using npx create-react-app --template typescript. However, when I try to start the app with npm start, I encounter an error in one of my files: TypeScript error in /<path>/App.tsx: Cannot find module ' ...

A deep dive into TypeScript: enhancing a type by adding mandatory and optional fields

In this scenario, we encounter a simple case that functions well individually but encounters issues when integrated into a larger structure. The rule is that if scrollToItem is specified, then getRowId becomes mandatory. Otherwise, getRowId remains option ...

Is it possible that React.createElement does not accept objects as valid react children?

I am working on a simple text component: import * as React from 'react' interface IProps { level: 't1' | 't2' | 't3', size: 's' | 'm' | 'l' | 'xl' | 'xxl', sub ...

The function is trying to access a property that has not been defined, resulting in

Here is a sample code that illustrates the concept I'm working on. Click here to run this code. An error occurred: "Cannot read property 'myValue' of undefined" class Foo { myValue = 'test123'; boo: Boo; constructor(b ...

Error TRPCClient: The unexpected presence of the token "'<'", ""<!DOCTYPE "... invalidates the JSON format within Next.JS

Encountering an error in the authentication call back page: TRPCClientError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON in Next.JS. The issue occurs in src/app/auth-callback/page.tsx and here's the relevant code ...

What is the best way to retrieve data in my client component without risking exposing my API key to unauthorized users?

To retrieve information, I plan to use pagination in order to specify a particular page number within the API URL and fetch additional data by updating the value of page. The process of fetching data in my server component is as follows: // fetchData.tsx ...

Is there a more efficient approach to displaying a list of elements and sharing state in React with TypeScript?

Check out this code sample I'm attempting to display a list with multiple elements and incorporate a counter on the main element that updates every time one of the buttons is clicked. I'm uncertain if this approach is optimal, as I am transition ...

Creating a JSON array or JSON object from an Angular Material table

My task is to create a JSON array or object from an Angular Material table, which I can then utilize to export to an Excel sheet. Here is the data: const ELEMENT_DATA: Element[] = [ {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: &apo ...

What is the process for transforming a string literal type into the keys of a different type?

Imagine having a string literal type like this: type Letters = "a" | "b" | "c" | "d" | "e"; Is there a way to create the following type based on Letters? type LetterFlags = {a: boolean, b: boolean, c: bool ...

Creating a SQL statement to extract information from multiple tables while also including a serialized column

I am working with two tables. Vendor Information vendor_id name country city area 42 Acme Thailand bangkok sukhumvit 43 Todo Thailand phuket old town 45 Goo Spain barcelona ramba 46 Alo ...

Unable to get md-virtual-repeat to work within md-select?

Attempting to use md-select to showcase a large amount of data is causing the browser to freeze upon opening. To address this, I tried implementing md-virtual repeat within md-select for improved performance. However, the code doesn't seem to be funct ...

Using PHP POST session along with Bootstrap Modal, MySQL, and Ajax to populate data in Datatable

For the past 3 months, I've been working on a solution to add devices for users using Datatable. Here's how my MySQL database tables are structured: Table Name: admin Columns: [id] [username] [password] Table Name: device Columns: [id] [uid] [p ...

What are the steps for populating a table cell with data using AJAX, MySQL, and PHP in the given situation?

The challenge is to create an HTML table with two columns where the first column gets its data from PHP and MySQL. The goal is to turn the values in the first column into clickable links that, when clicked, trigger an AJAX call to fetch information from th ...

What is the best way to retrieve the value from a Material UI textfield after hitting the enter key

Having trouble retrieving input values with the provided code. Attempted using onKeyUp, onKeyDown, and onKeyPress, but none of them returned the value as desired. Typically, I would use the onChange property to get the value, but it triggers for every ne ...

"When a class extends another class and utilizes properties within a static property, it essentially becomes

I have been encountering challenges with generics in TypeScript for quite some time now. My current setup is as follows: First, there is a generic class defined as: class Entity { public static schema = {}; } Then, there is a class that extends the ...