Skip to content

Commit

Permalink
Edits to modeling relationships page (#6636)
Browse files Browse the repository at this point in the history
* Edits to modeling relationships page

* Catching up to main (#6640)

* Update index.mdx (#6618)

Update quickstart

* Defer shortbread (#6575)

* Update CODEOWNERS (#6626)

* Update signInWithRedict docs (#6520)


---------

Co-authored-by: Rene Brandel <[email protected]>

* chore(js-storage): add bytesRange options to the example (#6621)

* custom resource docs updates (#6616)

* custom resource updates

* Apply suggestions from code review

Co-authored-by: josef <[email protected]>

---------

Co-authored-by: josef <[email protected]>

* update pr link checker to use python server (#6476)

* update pr link checker to use github http-server action

* change server to python server

---------

Co-authored-by: Jacob Logan <[email protected]>

* fix: make gen2 banner button align better with text (#6635)

* auth-events: small documentation fix (#6623)

* edit override docs (#6615)

* edit override docs

* Apply suggestions from code review

Co-authored-by: josef <[email protected]>

* add context around L1 and L2 constructs

---------

Co-authored-by: josef <[email protected]>

* Update cspell.json (#6639)

Added an entry for "bidirectionality"

---------

Co-authored-by: Rene Brandel <[email protected]>
Co-authored-by: Tim Nguyen <[email protected]>
Co-authored-by: Scott Rees <[email protected]>
Co-authored-by: Francisco Rodriguez <[email protected]>
Co-authored-by: Hui Zhao <[email protected]>
Co-authored-by: Edward Foyle <[email protected]>
Co-authored-by: josef <[email protected]>
Co-authored-by: jacoblogan <[email protected]>
Co-authored-by: Jacob Logan <[email protected]>
Co-authored-by: Chen Cozoczaru <[email protected]>

---------

Co-authored-by: Rene Brandel <[email protected]>
Co-authored-by: Tim Nguyen <[email protected]>
Co-authored-by: Scott Rees <[email protected]>
Co-authored-by: Francisco Rodriguez <[email protected]>
Co-authored-by: Hui Zhao <[email protected]>
Co-authored-by: Edward Foyle <[email protected]>
Co-authored-by: josef <[email protected]>
Co-authored-by: jacoblogan <[email protected]>
Co-authored-by: Jacob Logan <[email protected]>
Co-authored-by: Chen Cozoczaru <[email protected]>
Co-authored-by: katiegoines <[email protected]>
  • Loading branch information
12 people authored Dec 13, 2023
1 parent 5a683e9 commit 3e5b7a5
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ export function getStaticProps(context) {
};
}

Applications that work with data need to define the entities that represent said data. Entities are typically stored across different tables in a database, and items in a given table need a way to be **uniquely identified**. This is achieved by defining a field (or a combination of fields) that will hold a value (or values) to unambiguously refer to the item. When modeling applications, there is often the need to **establish relationships** between the entities. These relationships are achieved by having a field that holds the value of the unique identifier of the related entity.
Applications that work with data need well-defined entities to represent that data. Typically, entities are stored across different tables in a database, and items in a given table need to be **uniquely identified**. This is achieved by defining a field (or a combination of fields) that will hold a value (or values) to unambiguously refer to the item. When modeling applications, you often need to **establish relationships** between entities. This is done by adding a field that holds the value of the unique identifier of the related entity.

Sometimes when retrieving data for a given entity, retrieving its relationships at the same time can be an expensive operation; for this reason it is important to offer a mechanism for you to specify if relationships should be lazily or eagerly retrieved. Amplify Data provides this mechanism with a "custom selection set" feature.
Retrieving an entity's related data can be expensive, so Amplify Data offers a way to specify lazy or eager loading of relationships. This is done through "custom selection sets." With eager loading, related data is retrieved immediately when the entity is loaded. With lazy loading, related data is only retrieved when explicitly requested. Custom selection sets give you control over when and how an entity's relationships are populated.

## Types of relationships
## Types of relationships

|Relationship|Description|
|------------|--------|
|`a.hasMany()`|Create a one-directional one-to-many relationship between two models. For example, a Post has many comments. This allows you to query all the comments from the post record.|
|`a.manyToMany()`| Creates a bi-directional relationship, it needs to be defined on both related models. For example, a Blog has many Tags and a Tag has many Blogs.|
|`a.hasOne()`|Create a one-directional one-to-one relationship between two models. For example, a Project "has one" Team. This allows you to query the team from the project record.|
|`a.belongsTo()`|Use a "belongs to" relationship to make a "has one" or "has many" relationship bi-directional. This bi-directionality only facilitates your ability to read from both sides of the relationship. For example, a Project has one Team and a Team belongs to a Project. This allows you to reference the team from the project record and vice versa.|
|`a.hasMany()`|Creates a one-directional, one-to-many relationship between two models. For example, a **Post** has many **Comments**. This allows you to query all the comments from the post record.|
|`a.manyToMany()`| Creates a bidirectional relationship. Must be defined on both related models. For example, a **Blog** has many **Tags** and a **Tag** has many **Blogs**.|
|`a.hasOne()`|Creates a one-directional, one-to-one relationship between two models. For example, a **Project** has one **Team**. This allows you to query the team from the project record.|
|`a.belongsTo()`|Makes a "has one" or "has many" relationship bidirectional so you can read from both sides of the relationship. For example, a **Project** has one **Team** and a Team belongs to a Project. This allows you to reference the team from the project record and the other way around.|

## Model a "Has Many" relationship

Create a bi-directional one-to-many relationship between two models using the `hasMany()` method. In the example below, a Team has many Members and the parent Team may be looked up from a given member.
Create a bidirectional, one-to-many relationship between two models using the `hasMany()` method. In the example below, a Team has many Members and the parent Team may be looked up from a given member.

```typescript
const schema = a.schema({
Expand Down Expand Up @@ -72,7 +72,7 @@ await client.models.Member.update({
})
```

### Lazy load a "Has Many" relationships
### Lazy load a "Has Many" relationship

```ts
const { data: team } = await client.models.Team.get({ id: "MY_TEAM_ID"})
Expand All @@ -82,7 +82,7 @@ const { data: members } = await team.members()
members.forEach(member => console.log(member.id))
```

### Eagerly load a "Has Many" relationships
### Eagerly load a "Has Many" relationship

```ts
const { data: teamWithMembers } = await client.models.Team.get(
Expand All @@ -93,8 +93,8 @@ const { data: teamWithMembers } = await client.models.Team.get(
teamWithMembers.members.forEach(member => console.log(member.id))
```

## Model a "Many-to-many" relationship
Create a many-to-many relationship between two models with the `manyToMany()` method. Provide a common `relationName` on both models to join them into a many-to-many relationship.
## Model a "many-to-many" relationship
Create a many-to-many relationship between two models with the `manyToMany()` method. Provide a common `relationName` on both models to join them in a many-to-many relationship.

```typescript
const schema = a.schema({
Expand All @@ -112,7 +112,7 @@ const schema = a.schema({

## Model a "Has One" relationship

Create a one-directional one-to-one relationship between two models using the `hasOne()` method. In the example below, a Project has a Team. By using `hasOne` this way, it will automatically generate a field to hold the identifier of the related model.
Create a one-directional, one-to-one relationship between two models using the `hasOne()` method. In the example below, a Project has a Team. By using `hasOne` this way, it will automatically generate a field to hold the identifier of the related model.

```typescript
const schema = a.schema({
Expand Down Expand Up @@ -143,7 +143,7 @@ const { data: project } = await client.models.Project.create({

### Update a "Has One" relationship between records

To update a "Has One" relationship between records, you first have to get the child item and then update the reference to the parent to another parent.
To update a "Has One" relationship between records, you first retrieve the child item and then update the reference to the parent to another parent. For example, to move a Project to a new Team:

```ts
const { data: newTeam } = await client.models.Team.create({
Expand All @@ -167,14 +167,14 @@ await client.models.Project.update({
});
```

### Lazy load a "Has One" relationships
### Lazy load a "Has One" relationship

```ts
const { data: project } = await client.models.Project.get({ id: "MY_PROJECT_ID"})
const { data: team } = await project.team();
```

### Eagerly load a "Has One" relationships
### Eagerly load a "Has One" relationship

```ts
const { data: project } = await client.models.Project.get(
Expand Down Expand Up @@ -208,15 +208,15 @@ const { data: ics } = await org.individualContributors();
const { data: managers } = await org.managers();
```

## Model a bi-directional relationship
## Model a bidirectional relationship

You can make a "has one" or "has many" relationship bi-directional with the `belongsTo()` method. The bi-directionality of `belongsTo()` grants you the ability to query the related item from both sides of the relationship.
You can make a "has one" or "has many" relationship bidirectional with the `belongsTo()` method. Using the bidirectionality of `belongsTo()`, you can query the related item from both sides of the relationship.

<Callout>
**Note:** The `belongsTo()` method requires that a `hasOne()` or `hasMany()` relationship already exists from parent to the related model.
</Callout>

### Bi-directional Has One relationship
### Create a bidirectional "Has One" relationship

```typescript
const schema = a.schema({
Expand Down Expand Up @@ -244,10 +244,10 @@ const { data: teamProjectRelatedTeam } = await teamProject.team();
```

<Callout warning>
"Belongs to"-relationships **do not** allow you to update both sides of a "has one" and "belongs to" relationship in a transactional nature.
"Belongs to" relationships **do not** allow you to update both sides of a "has one" and "belongs to" relationship in a transactional manner.
</Callout>

### Bi-directional Has Many relationship
### Create a bidirectional "Has Many" relationship

```typescript
const schema = a.schema({
Expand Down

0 comments on commit 3e5b7a5

Please sign in to comment.