Is it possible to utilize a partial entity for saving with TypeORM?

My current table structure looks like this:

--changeset 0004-order:ccushing
create table if not exists "order"."order"
(
    id          uuid primary key                                          not null default uuid_generate_v4(),
    state       uuid references "order".order_status
);

--changeset 0004-h0-table-order_event_type:ccushing
create table if not exists "order".order_event_type
(
    id          uuid primary key                                          not null default uuid_generate_v4(),
    key         text unique                                               not null
);

--changeset 0004-h1-table-order_event:ccushing
create table if not exists "order".order_event
(
    id          uuid primary key                                          not null default uuid_generate_v4(),
    order_id    uuid                                                      not null references "order"."order" (id),
    event_type  uuid                                                      not null references "order".order_event_type (id),
    event       jsonb                                                     not null,
    unique (order_id, event),
    unique (order_id, event_type)
);

I am looking to create a new OrderEventEntity, but I want to avoid loading the Order object since I only need the order_id in the event.

@Entity('order.order_event')
export default class OrderEventEntity implements Identifiable<string> {
  @PrimaryGeneratedColumn({ type: 'uuid' })
  readonly id!: string;

  @ManyToOne(() => OrderEventTypeEntity, ({ event }) => event)
  readonly eventType!: string;

  @ManyToOne(() => OrderEntity, ({ events }) => events)
  readonly order!: OrderEntity;
}

Is it possible for me to achieve this by creating a new OrderEventEntity and saving it without loading the entire Order?

const order = new Order({ id: 1234 })
repo.save( new OrderEventEntity({ order: order, ... })

Or is there another way, maybe with some partial load, that allows me to maintain the OneToMany relationship while only having access to the order id?

Answer №1

Consider the following two options:

  1. Add a order_id column to your OrderEventEntity
@Entity('order.order_event')
export default class OrderEventEntity implements Identifiable<string> {
  @PrimaryGeneratedColumn({ type: 'uuid' })
  readonly id!: string;

  @ManyToOne(() => OrderEventTypeEntity, ({ event }) => event)
  readonly eventType!: string;

  @Column()
  order_id: string;

  @ManyToOne(() => OrderEntity, ({ events }) => events)
  readonly order!: OrderEntity;
}
  1. Alternatively, consider using force cast tricky
const order = { id: 1234 } as Order;
repo.save( new OrderEventEntity({ order: order, ... })

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

Minimize the amount of information retrieved and shown based on the timestamp

I am currently working on storing and retrieving the date of a user request. For creating the timestamp, I use this code: const date = firebase.firestore.FieldValue.serverTimestamp(); This is how I fetch and display the data: <tr class="tr-content" ...

What is the best way to include text or a label on my Angular map without using a marker?

I am currently using the agm-map module in my angular project. I want to place a label or text at the center of a polygon on the map without using markers. How can I achieve this functionality in Typescript? I attempted to use the MapLabel Utility for thi ...

The firebase-generated observable is causing the notorious differ error as it is not iterable

Hey there, I'm encountering an issue that's preventing the route from rendering correctly. I initially thought that unwrapping an observable into an iterable using the async pipe would solve it, but this error indicates otherwise. Sometimes obser ...

Dealing with client-side exceptions in a Next.js 13 application's directory error handling

After carefully following the provided instructions on error handling in the Routing: Error Handling documentation, I have successfully implemented both error.tsx and global-error.tsx components in nested routes as well as the root app directory. However, ...

Why won't my code work with a jQuery selector?

I'm struggling to retrieve the value from a dynamically generated <div> using jQuery. It seems like the syntax I'm using doesn't recognize the div with an id tag. The HTML code is stored in a variable, and below is a snippet of code w ...

Can you explain the distinction between `any[]` and `{ [s: string]: any }`?

I was attempting to make this code snippet function properly: test(a: any[]) { let b: string[][] = []; b.push(Object.keys(a[0])); b.push(...a.map(e => Object.values(e))); } However, the compiler is throwing an error for the b.push(...a.ma ...

Guide on how to update an array within typed angular reactive forms

I'm currently working on finding a solution for patching a form array in a strongly-typed reactive Angular form. I've noticed that patchValue and setValue don't consistently work as expected with FormControl. Here's an example of the fo ...

Trouble implementing array filter in React component is a common issue

Hello everyone! I'm facing an issue with deleting an element from my useState array. I have the index of the element that I want to remove, and I've tried the following code snippet: const updatedArray = myArray.filter((item: any, index: number) ...

Automate the compilation of Typescript project references by creating a solution that allows for passing unique options to each

When compiling or building a project with references programmatically, I make use of the ts.createSolutionBuilder API. The challenge I face is that in my specific scenario, I do not have individual tsconfig.json files stored on the filesystem for each pac ...

Guide to correcting the file path of an external css within the public directory on Express framework

I am facing an issue with loading external CSS files and need some help to fix the path. Despite multiple attempts, I have been unsuccessful so far. Below is my category structure: https://i.stack.imgur.com/sLTcN.png Within the header.ejs file, this is h ...

angular 6's distinctUntilChanged() function is not producing the desired results

I have a function that retrieves an observable like so: constructor(private _http: HttpClient) {} getUsers(location){ return this._http.get(`https://someurl?location=${location}`) .pipe( map((response: any) => response), ...

Exploring the use of MediaSource for seamless audio playback

Currently working on integrating an audio player into my Angular web application by following a tutorial from Google Developers and seeking guidance from a thread on Can't seek video when playing from MediaSource. The unique aspect of my implementati ...

pressing the button again will yield a new outcome

I am looking to disable a button (material ui) when it is clicked for the second time by setting disabled={true}. Unfortunately, I have not been able to find any examples related to this specific scenario on StackOverflow. <Button onClick={this.s ...

The content security policy is preventing a connection to the signalr hub

Currently, I am developing an application using electron that incorporates React and Typescript. One of the features I am integrating is a SignalR hub for chat functionality. However, when attempting to connect to my SignalR server, I encounter the followi ...

Converting jQuery drag and drop functionality to Angular: A step-by-step guide

I have implemented drag and drop functionality using jquery and jquery-ui within an angular project. Below is the code structure: Index.html, <!doctype html> <html lang="en"> <head> <link href="//code.jquery.com/ui/1.10.3/themes/ ...

What is the best way to delegate the anonymous function logic contained within the subscribe() method?

Imagine you have a code block similar to this: constructor(private _http: HttpClient) { this.fetchUsers(5); } employees: any[] = []; fetchUsers(count: number) { this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe( ...

Using Angular filter pipe to customize markers in Leaflet maps

I am currently working on a select element called district, which lists all the districts in the city. My objective is to apply a filter that will dynamically display only the leaflet markers corresponding to the selected district on the map. Any suggesti ...

Retrieve the TaskID of an ECS Fargate container for exporting and future use within AWS CDK code

Currently, I am leveraging CDK version 2 alongside Typescript. In my current setup, I encounter a situation where I necessitate the TaskID value from ECS Fargate Container to be incorporated into another command. The process involves me utilizing new ecs ...

Issues with Angular application navigation in live environment

While my website functions perfectly on the development server, I encounter a strange error when I publish it to production on GitHub pages. Visiting the URL (yanshuf0.github.io/portfolio) displays the page without any issues. However, if I try to access y ...

Unrestricted Angular Audio Playback without CORS Restrictions

I am currently developing a web application using Angular4 that will include the feature of playing audio files. Unfortunately, I am facing an issue where I do not have control over the server serving the media files, and therefore cannot make any modifica ...