ID-Based Relations

ID-based relations provide more control over how related entities are managed. By explicitly including the foreign key (such as customerId) in the entity, you gain more flexibility and can optimize performance by reducing the need to load the related entity in some cases.

Defining an ID-Based Relation

In the Order entity, we add a customerId field to store the ID of the related Customer. We then reference this field in the @Relations.toOne decorator to establish the relationship between Order and Customer.

It’s important to use the correct type arguments, <Order, Customer>, to ensure proper type checking for this relation.

shared/Order.ts
3 collapsed lines
import { Entity, Fields, Relations } from 'remult'
import { Customer } from './Customer'
@Entity('orders')
export class Order {
@Fields.integer()
id = 0
@Fields.integer()
customerId = 0
@Relations.toOne<Order, Customer>(() => Customer, 'customerId') // This establishes the relation
customer!: Customer
@Fields.number()
amount = 0
}

In this setup, the customerId field holds the reference to the customer, and the @Relations.toOne decorator connects the Order entity to the Customer entity.

Defining the Inverse Relation

On the Customer entity, we define the inverse of the relation using @Relations.toMany. This decorator links a Customer to multiple Order records, allowing us to retrieve all orders related to a specific customer.

shared/Customer.ts
3 collapsed lines
import { Entity, Fields, Relations } from 'remult'
import { Order } from './Order'
@Entity('customers')
export class Customer {
@Fields.integer()
id = 0
@Fields.string()
name = ''
@Fields.string()
city = ''
@Relations.toMany<Customer, Order>(() => Order, 'customerId') // This establishes the relation
orders?: Order[]
}

Now, the Customer entity has an orders array, representing all the orders associated with that customer.

Try it out

Check out the output and see that the customerId is included even if you do not explicitly include the relation. This gives you the flexibility to work directly with the ID without always needing to load the related entity.

Working with Existing Data

If you already have existing data in your database where the foreign key column is named customer, but you want to use customerId in your code, you can use the dbName property to map the customerId field to the customer column in the database.

@Fields.integer({ dbName: 'customer' })
customerId = 0

This ensures that your code uses customerId while mapping it correctly to the customer column in the database, allowing for seamless integration with existing data.


By using ID-based relations, you have greater control over your data models and can optimize performance by limiting unnecessary entity loading. This approach also provides a clean and efficient way to manage relations in your Remult applications.


Let me know if this works!

Powered by WebContainers
Files
Preparing Environment
  • Installing dependencies
  • Starting http server