Typeorm retrieve values of fields depending on their datatype

I have a TypeORM entity called SuperResource

@Entity()
export class SuperResource extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(() => Resource, {cascade: true, eager: true})
  @JoinColumn()
  resource: Resource

  @Column({type: "enum", enum: ResourceTypes})
  type: string

  ...
}

In addition to that, I have other entities that extend the Resource entity:

Job Entity

@Entity()
export class Job extends Resource {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  jobId: number;

  ...
}

Table Entity

@Entity()
export class Table extends Resource {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  description: string;

  ...
}

The Resource entity looks like this:

@Entity()
export abstract class Resource extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string

  ...
}

When I execute a query like

repository.findOne({ where: {id: parseInt(id)}})
, the object returned in the "resource" field only contains basic information.

Is there a way to retrieve full information based on the object's type?

I know using 2 queries can solve this issue, but I'm curious if there are alternate solutions. Thank you!

Answer №1

If you're looking for guidance on entity inheritance in TypeOrm, the comprehensive documentation provides helpful examples. Utilize the `@TableInheritance` and `@ChildEntity` decorators to address your specific needs.

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

Issue with Mongoose findOne function not finishing execution

Struggling to figure out why my mongoose findOne function is not completing! It runs up until the User.findOne() call and then just hangs, never entering the callback. Here's the code in the order it's executed: ----------------Routing // Proce ...

Sharing multiple object data using socket.io in express

I was able to successfully post single data using socket.io, but when I try to add another (timestamp), nothing shows up on the screen. app.js app.post('/update', function(req, res, next){ io.sockets.emit("update", req.body); io.sockets ...

TypeScript is still throwing an error even after verifying with the hasOwnProperty

There exists a type similar to the following: export type PathType = | LivingstoneSouthernWhiteFacedOwl | ArakGroundhog | HubsCampaigns | HubsCampaignsItemID | HubsAlgos | HubsAlgosItemID | TartuGecko | HammerfestPonies | TrapaniSnowLeop ...

Receiving POST requests with express.js

I have been encountering an issue while trying to configure express to handle POST requests. Despite setting up my routes and configuration properly, I keep getting empty JSON or undefined when attempting to display the POST queries sent to the server. My ...

The function for batch insertion only functions with Postgresql and SQL Server databases

I am a beginner in JavaScript and I am currently working on creating a new restaurant. I have come across a code snippet that inserts a relation into a join-table: await newRestaurant.$relatedQuery('tags', trx).relate(tagIds); Is it not possible ...

Updating an object within an array of objects in Angular

Imagine having a unique object array named itemArray with two items inside; { "totalItems": 2, "items": [ { "id": 1, "name": "dog" }, { "id": 2, "name": "cat" }, ] } If you receive an updated result for on ...

The 'RegExpExecArray | null' type is required to include a method '[Symbol.iterator]()' which returns an iterator to iterate through its values

As someone who is new to TypeScript, I have a good understanding of the basics but recently came across a typecast error that I am struggling to solve. const [full, id]: string | null = /.*media\/[^\/]+\/(.*)/.exec(item.uri) When it comes t ...

Typescript with AngulaJS is unable to access Angular services within a directive's link function

I'm new to TypeScript and I'm attempting to create an Angular.js directive using a TypeScript class. I want to utilize external Angular services in the directive's link function, which is called when the $watch function receives data. Howeve ...

Step-by-step guide on programmatically activating a radio button

I am working with a radio button and input field. I need the ability to programmatically toggle the radio button so that when this.iAreaOfCoverageForThresholdPasser.average-height is set to true, the radio button appears highlighted. Snippet of HTML: < ...

Changing icons within an ngFor loop in Angular 2

Looking for a solution: How can I toggle icons using ngFor? Situation: I am using *ngFor to iterate through an array and display category names. When a day is clicked, I want to open an accordion and show category details (which I have already managed). O ...

Issue with Angular HTTP API get request not reaching intended Express server function

I am facing an issue with my Angular application where I need to retrieve data from a MongoDB collection. The problem arises when I make two HTTP API calls ("query" and "get") from flConstruct to fetch data from the server. The "query" call works perfectly ...

Ensuring Data Integrity in Mongoose And Express: Checking Existing Data in the Database Before Updating

How can validations be performed before saving edited data in mongoose? For instance, if sample.name already exists in the database, an error message should be displayed. Here is a snippet of code illustrating this: //Post: /sample/edit app.post(uri + &a ...

Tips for retrieving specific data from MongoDB using Node.js

Currently facing an issue working with Node.js, express, and mongodb when it comes to passing data between frontend and backend. Note: The code below represents middleware code for communication between frontend and backend. I have successfully retrieved ...

The endpoint for creating a new group at 5000/groups/create is functioning properly, however, when trying to access an existing

I am encountering a 404 not found error when trying to access the route 5000/groups/:id. However, strangely enough, when I make a delete request to the same route, everything works perfectly fine. Even just visiting 5000/groups/:id directly results in a " ...

Having trouble injecting $resource into my Jasmine unit test

I've encountered an issue while trying to test my self-written service that utilizes $resource. I'm facing difficulties when trying to inject $resource into my test spec. My setup includes Typescript with AngularJS 1.6.x, and here is a snippet o ...

Encountering intellisense problems while declaring or utilizing TypeScript types/interfaces within Vue.js Single File Component (SFC) documents

For my Nuxt 3 project, I am developing a component and attempting to declare an interface for the component props to ensure strong typings. Here is an example: <script setup> interface Props { float: number; } const props = defineProps<Props> ...

Retrieving saved MongoDB data using Socket.io in React: A comprehensive guide

I've been exploring tutorials on how to display pre-existing chat messages before joining a chatroom, but I'm struggling with implementing it in React. Below are some questions regarding the server-side setup. On the client side, within the cons ...

typescript filter an array using another array

Struggling to find a solution... I am attempting to filter one array with another array. Here's the scenario: I have an array that needs to be filtered based on dynamically selected checkboxes. After trying various approaches, here is the code I hav ...

Learn how to retrieve data from the console and display it in HTML using Angular 4

Need help fetching data inside Angular4 HTML from ts variable. Currently only able to retrieve 2 data points outside the loop. Can anyone assist with pulling data inside Angular4? HTML: <tr *ngFor="let accept of accepts"> ...

Issue: Encounter of an unexpected error: Unhandled Promise Rejection Error: It seems that there is a TypeError occurring where the property '0' of a null object cannot be read

Ever since I started learning angular2, I have been experiencing this issue with my news.service. @Injectable() export class NewsServices { private news: News[] = []; constructor(private _http: Http) {} getSingleNews(id: string): Obs ...