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.
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).
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"
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.
There are two options:
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;
}
const order = { id: 1234 } as Order;
repo.save( new OrderEventEntity({ order: order, ... })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With