Develop a query builder in TypeORM where the source table (FROM) is a join table

I am currently working on translating this SQL query into TypeORM using the QueryBuilder:

SELECT
  user_places.user_id,
  place.mpath
FROM
  public.user_root_places_place user_places
INNER JOIN
  public.place place
  ON place.id = user_places.place_id

The entities involved are:

@Entity()
export class User {
  @Column({ unique: true, primary: true })
  id: string;

  @ManyToMany(() => Place)
  @JoinTable()
  rootPlaces: Place[];
}

@Entity()
export class Place {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  mpath: string;
}

When creating a query builder, it is necessary to use an entity or table, but the join table may not be directly accessible in TypeORM


I understand that switching the order of the inner join table can resolve the issue, however I am specifically interested in cases where the source table is also the join table

Answer №1

If you choose not to utilize the auto-generated name, simply specify the join table name explicitly

@Entity()
export class User {
  @Column({ unique: true, primary: true })
  id: string;

  @ManyToMany(() => Place)
  @JoinTable({
    name: 'user_places' // <---
  })
  rootPlaces: Place[];
}

Furthermore:

createQueryBuilder('user_places')
  .select(['user_places.userId', 'place.mpath'])
  .innerJoin(Place, 'place', 'place.id = user_places.place_id')
  .getMany();

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

Access information from a different component within the route hierarchy

Suppose you have three components named A, B, and C with the following routing direction: A -> B -> C To retrieve data from the previous component (going from C to get data from B), you can use the following lines of code: In Component C: private ...

The issue in Angular2 viewmodel where the boolean value fails to update the *ngIf template

I'm seeking assistance with an unusual issue in my Angular2 and Typescript application. Within my template, I have the following HTML utilizing ngIf: <div *ngIf="loading" class="row"> <div class="small-3 small-centered columns" > ...

What is the best way to send multiple values from the view to a method without using two-way binding?

When I change the dropdown value for the route type in my code, I need to pass both the gender value and the route type ID to my data retrieval method. Currently in my HTML file, I have only written a change event. I attempted two-way binding but encounte ...

Create a Typescript function that adheres to a specified type

Imagine a scenario where a specific type of function is declared within a type type Callback = (err: Error | null, result: any) type UselessFunction = (event: string, context: any, callback: Callback) => void The objective is to declare functions that ...

The name 'Diagnostics' cannot be located

I've downloaded the Typescript repository and am currently reviewing the code. However, I keep encountering this recurring error message: Cannot find name 'Diagnostics' This error pops up on lines that are similar to this: Diagnostics._ ...

Using Django's JSONField Nested within an ArrayField

I am encountering an issue when trying to insert data into a field using ArrayField with JSONField nested inside. models.py locations = ArrayField(JSONField(null=True, blank=True), blank=True, null=True) Insert location_arr = [{"locations": "loc1", "amou ...

Encountering an error when trying to import a node module in an Angular TypeScript file. The module

Currently, I am in the process of developing an Electron application specifically designed for managing Airplay on MacOS. To accomplish this task, I am utilizing Angular and TypeScript to wrap APIs from a unique npm package known as Airplay npm package. ...

Tips for defining data types for spreading properties in TypeScript

I'm grappling with adapting this code to function properly in TypeScript type ScrollProps = { autoHide: boolean autoHideTimeout: number autoHideDuration: number } const renderThumb = ({ style, ...props}) => { const thumbStyle = { borde ...

Having trouble uploading a pdf file into a .tsx file

As a beginner in learning TypeScript, I recently embarked on building a portfolio website using React JS and TypeScript. However, I encountered a problem when trying to import a PDF file from my images into the resume.tsx file within my project folder. Th ...

Angular 2 module that is loaded lazily - service does not follow singleton pattern

After successfully implementing lazy loading modules into my application, I have ensured that the app.module.ts is properly configured. @NgModule({ declarations: [ AppComponent, HeaderComponent, HomeComponent ], imports: [ BrowserMod ...

What is the reason behind the absence of compile time errors when using 'string' functions on an 'any' field type variable in TypeScript?

Looking at the following typescript code snippet: let a; a = "number"; let t = a.endsWith('r'); console.log(t); It is worth noting that since variable 'a' is not declared with a specific type, the compiler infers it as ...

What is the impact of MUL key joins on MySQL performance?

I have a MySQL query that involves three table joins across four tables, with two of the joins on MUL keys and one on primary keys. However, fetching 100 rows with this query takes an entire minute. I suspect that if I index or convert these unique pairs ...

Organizing objects into arrays in Node.js

I have an array and I need to concatenate an object after the array. Here is my array: const users = [ {name: "Joe", age: 22}, {name: "Kevin", age: 24}, {name: "Peter", age: 21} ] And here is my object: ...

Dynamically incorporate new methods into a class

Currently, I am in the process of implementing setters and getters for items that will be stored in session storage. These methods are being written within a service. However, upon attempting to call these functions in my component, I am encountering a tra ...

Visual Studio 2017, ASP.NET framework, Typescript programming language, and node package manager

My ASP.net application in Visual Studio used to only utilize JavaScript, but now I am looking to incorporate Typescript. While the installation and transpiling process went smoothly, I encountered an issue when attempting to import modules. I decided to u ...

Executing an Observable function in Angular Typescript a single time

Within my Angular application, there exists a service responsible for retrieving data from a server. load.service.ts: load = new Observable(observer => { console.log('load function called'); // asynchronous tasks with time delay obser ...

Angular: Converting JSON responses from HttpClient requests into class instances

I am facing an issue with the following code: public fetchResults(searchTerm: string): Observable<Array<SearchResult>> { let params = new HttpParams().set('searchTerm', searchTerm); return this.http .get<Array< ...

Struggling with using Redux with tassign in Angular (typescript) to combine state.array and action.array. However, encountering an issue where state.array.join is not a function

Redux function to combine all videos: function combineAllVideos(state, action) { return tassign(state, { allVideos: state.allVideos.concat([action.data]) }); } Declaration + State for all videos array: allVideos: Array<Object>; OR allVid ...

MUI options - The specified type 'string' cannot be matched with type '"icon" | "iconOnly" | "text" | "outlined" | "contained" | undefined'

Is it possible to utilize custom variants in MUI v5? I am having trouble using a custom variant according to their documentation: https://mui.com/material-ui/customization/theme-components/#creating-new-component-variants declare module "@mui/material ...

The module "node_modules/puppeteer/lib/types" does not contain the export "Cookie"

Currently facing an issue with puppeteer types. I am attempting to import the Cookie type, but it seems to be not functioning on versions above 6.0.0. import { Cookie } from 'puppeteer'; Here is the error message: /node_modules/puppeteer/lib/typ ...