Skip to main content

Relations

You can create one-to-many relationships or many-to-many relationships.

Syntax

The following example showcases the possibilities of Manifest relations:

# manifest/backend.yml

name: Basketball League 🏀

entities:
Player 🤾:
properties:
- name
belongsTo:
- Team
belongsToMany:
- Skill

Team 👥:
properties:
- name

Skill 💪:
properties:
- name

Fixture 🏟️:
properties:
- { name: homeScore, type: number }
- { name: awayScore, type: number }
belongsTo:
- { name: homeTeam, entity: Team }
- { name: awayTeam, entity: Team }

In other words:

  • The belongsTo keyword creates a many-to-one relationship (and its opposite one-to-many)
  • The belongsToMany keyword creates a many-to-many bi-directional relationship
  • The long syntax (as in Fixture.belongsTo) is useful when we want to edit the name of the relationships
tip

All relationships are bi-directional, which means that you just have to specify them in one side only to work with them in both directions.

When you define a belongsTo relationship, it implicitly set the opposite hasMany relationship and allow querying relations in both directions.

Relation params

You can pass arguments using the long syntax:

OptionDefaultTypeDescription
nameEntity namestringThe name of the relation
entity-stringThe class name of the entity that the relationship is with
eagerfalsebooleanWhether the relationship should be eager loaded. Otherwise, you need to explicitly request the relation in the client SDK or API

Work with relations

Load relations

You can specify which relations you want to load with your entities in your query. Eager relations are loaded automatically.

  // Fetch entities with 2 relations.
const cities = await manifest
.from('cities')
.with(['region', 'mayor'])
.find()

// Fetch nested relations.
const cities = await manifest
.from('cities')
.with(['region', 'region.country', 'region.country.planet'])
.find()

Filter by relation

Once the relation is loaded, you can also filter items by their relation id or properties.

  // Get all cats that belong to owner with id 1.
const cats = await manifest
.from('cats')
.with(['owner'])
.where('owner.id = 1')
.find()

// Get all cats that have an owner with name "Jorge".
const cats = await manifest
.from('cats')
.with(['owner'])
.where('owner.name = Jorge')
.find()

Store relations

To store or update an item with its relations, you have to pass the relation id(s) as a property that end with Id for many-to-one and Ids for many-to-many like companyId or tagIds

  // Store a new player with relations Team and Skill.
const newPlayer = await manifest.from('players').create({
name: 'Mike',
teamId: 10,
skillIds: [1,2,3,4,5]
})
note

When storing many-to-many relations, you always need to pass an array, even if you just have one single value.