From 2a3aee10ba608ce8045a8e21d698cd90e014b72a Mon Sep 17 00:00:00 2001 From: Kevin Mattutat Date: Fri, 15 Nov 2024 17:26:36 +0100 Subject: [PATCH] chore: Fixed typos and highlighted code snippets --- .../developer-guide/channel-aware/index.md | 25 +++++++++++-------- .../src/service/services/channel.service.ts | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/docs/docs/guides/developer-guide/channel-aware/index.md b/docs/docs/guides/developer-guide/channel-aware/index.md index 31952789c5..f78a024b38 100644 --- a/docs/docs/guides/developer-guide/channel-aware/index.md +++ b/docs/docs/guides/developer-guide/channel-aware/index.md @@ -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'; @@ -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. @@ -55,19 +56,20 @@ export class RequestService { async create(ctx: RequestContext, input: CreateRequestInput): Promise { 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" @@ -83,9 +85,11 @@ export class RequestService { findOne(ctx: RequestContext, requestId: ID, relations?: RelationPaths) { +// highlight-start return this.connection.findOneInChannel(ctx, ProductRequest, requestId, ctx.channelId, { relations: unique(effectiveRelations) }); +// highlight-end } findAll( @@ -97,6 +101,7 @@ export class RequestService { .build(ProductRequest, options, { ctx, relations, +// highlight-next-line channelId: ctx.channelId, }) .getManyAndCount() diff --git a/packages/core/src/service/services/channel.service.ts b/packages/core/src/service/services/channel.service.ts index 216e1f1f2b..93d39bc687 100644 --- a/packages/core/src/service/services/channel.service.ts +++ b/packages/core/src/service/services/channel.service.ts @@ -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( entity: T,