4.3.6. Manage Relationships

In this chapter, you'll learn how to manage relationships between data models when creating or updating their records using the module's main service.

Important

Data model relationships are in active development and may change.

Manage One-to-One and One-to-Many Relationship#

When you create a record of a data model that belongs to another, pass the ID of the other data model's record in the {relation_name}_id property.

For example, assuming you have the User and Email data models from the previous chapter:

1// when creating an email2const email = await helloModuleService.createEmail({3  // other properties...4  user_id: "123"5})6
7// when updating an email8const email = await helloModuleService.updateEmail({9  id: "321",10  // other properties...11  user_id: "123"12})

In the example above, you pass the user_id property when creating or updating an email to specify the user it belongs to.


Manage Many-to-Many Relationship#

When you create or update a record of a data model that has a many-to-many relationship to another data model, pass an array of IDs of the other data model's records in the {relation_name}_ids property.

For example, assuming you have the Order and Product data models from the previous chapter:

1// when creating a product2const product = await helloModuleService.createProduct({3  // other properties...4  order_ids: ["123", "321"]5})6
7// when updating an order8const order = await helloModuleService.updateOrder({9  id: "321",10  // other properties...11  product_ids: ["123", "321"]12})

In the example above, you pass the order_ids property when you create (or update) a product, and you pass the product_ids property when you update (or create) an order.

Was this chapter helpful?