Skip to content

Commit

Permalink
chore: Fixed typos and highlighted code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Draykee committed Nov 15, 2024
1 parent 2e1ae2b commit 2a3aee1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions docs/docs/guides/developer-guide/channel-aware/index.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: "Channelaware entities"
title: "Implementing ChannelAware"
showtoc: true
---

# Defining channel-aware entities
## Defining channel-aware entities

Making an entity channel-aware means that it can be associated with a specific [Channel](/reference/typescript-api/channel/).
This is useful when you want to have different data or features for different channels. First you will have to create
a base entity ([Define a database entity](/guides/developer-guide/database-entity/)) that implements the `ChannelAware` interface.
This interface requires the entity to have a `channels` property
an entity ([Define a database entity](/guides/developer-guide/database-entity/)) that implements the `ChannelAware` interface.
This interface requires the entity to provide a `channels` property

```ts title="src/plugins/requests/entities/product-request.entity.ts"
import { DeepPartial } from '@vendure/common/lib/shared-types';
Expand All @@ -29,14 +29,15 @@ class ProductRequest extends VendureEntity {

@Column()
text: string;

// highlight-start
@ManyToMany(() => Channel)
@JoinTable()
channels: Channel[];
// highlight-end
}
```

# Creating channel-aware entities
## Creating channel-aware entities

Creating a channel-aware entity is similar to creating a regular entity. The only difference is that you need to assign the entity to the current channel.
This can be done by using the `ChannelService` which provides the `assignToCurrentChannel` helper function.
Expand All @@ -55,19 +56,20 @@ export class RequestService {
async create(ctx: RequestContext, input: CreateRequestInput): Promise<ProductRequest> {
const request = new ProductRequest(input);
// Now we need to assign the request to the current channel (+ default channel)
await this.channelService.assignToCurrentChannel(requestInput, ctx);
// highlight-next-line
await this.channelService.assignToCurrentChannel(input, ctx);

return await this.connection.getRepository(ProductRequest).save(request);
}
}
```
For [Translatable entities](/guides/developer-guide/translations/), the best place to assign the channels is inside the `beforeSave` input of the [TranslateableSave](/reference/typescript-api/service-helpers/translatable-saver/).
For [Translatable entities](/guides/developer-guide/translations/), the best place to assign the channels is inside the `beforeSave` input of the [TranslateableSave](/reference/typescript-api/service-helpers/translatable-saver/) helper class.


# Querying channel-aware entities
## Querying channel-aware entities

When querying channel-aware entities, you can use the [ListQueryBuilder](/reference/typescript-api/data-access/list-query-builder/#extendedlistqueryoptions) or
the [TransactionalConnection](/reference/typescript-api/data-access/transactional-connection/#findoneinchannel)to automatically filter entities based on the provided channel id.
the [TransactionalConnection](/reference/typescript-api/data-access/transactional-connection/#findoneinchannel) to automatically filter entities based on the provided channel id.


```ts title="src/plugins/requests/service/product-request.service.ts"
Expand All @@ -83,9 +85,11 @@ export class RequestService {
findOne(ctx: RequestContext,
requestId: ID,
relations?: RelationPaths<ProductRequest>) {
// highlight-start
return this.connection.findOneInChannel(ctx, ProductRequest, requestId, ctx.channelId, {
relations: unique(effectiveRelations)
});
// highlight-end
}

findAll(
Expand All @@ -97,6 +101,7 @@ export class RequestService {
.build(ProductRequest, options, {
ctx,
relations,
// highlight-next-line
channelId: ctx.channelId,
})
.getManyAndCount()
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/services/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class ChannelService {
* @description
* Assigns a ChannelAware entity to the default Channel as well as any channel
* specified in the RequestContext. This method will not save the entity to the database, but
* assigns the `channels` property off the entity.
* assigns the `channels` property of the entity.
*/
async assignToCurrentChannel<T extends ChannelAware & VendureEntity>(
entity: T,
Expand Down

0 comments on commit 2a3aee1

Please sign in to comment.