Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I use a partial entity to save with typeorm?

Tags:

So I have this table structure

--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 want to create a new OrderEventEntity, but I don't want to load Order when doing it, since I'll just be getting 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;
}

am I able to do

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

or similar (maybe some partial load) without losing the OneToMany? but still only having the order id.

like image 310
xenoterracide Avatar asked Feb 17 '20 23:02

xenoterracide


People also ask

How does TypeORM save work?

save - Saves a given entity or array of entities. If the entity already exist in the database, it is updated. If the entity does not exist in the database, it is inserted. It saves all given entities in a single transaction (in the case of entity, manager is not transactional).

What is repository in TypeORM?

Repository is just like EntityManager but its operations are limited to a concrete entity. You can access the repository via EntityManager. Example: import { User } from "./entity/User" ​

What is the use of TypeORM?

TypeORM is a TypeScript ORM (object-relational mapper) library that makes it easy to link your TypeScript application up to a relational database database. TypeORM supports MySQL, SQlite, Postgres, MS SQL Server, and a host of other traditional options.


1 Answers

There are two options:

  1. Define order_id column in 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. Use force cast tricky
const order = { id: 1234 } as Order;
repo.save( new OrderEventEntity({ order: order, ... })
like image 184
hoangdv Avatar answered Sep 28 '22 09:09

hoangdv