Appearance
BrandsModule
The BrandsModule
is a dedicated module in NestKit that manages and organizes information related to different brands within an organization's media production. This module facilitates the efficient handling of brand data, enabling easy access, modification, and storage of brand-specific media products such as episodes, videos, streams, and more. By implementing the BrandsModule
, organizations can effectively manage their media content and maintain a structured brand identity.
Entity Inheritance
NestKit provide base classes that can be inherited and customized with your needs. For entities we use TypeORM TableInheritance
.
Key Components
The module provides a set of reusable components that can be easily integrated into a NestJS project:
Brand Entity
The Brand entity represents a unique brand within an organization's media production. It stores essential information such as id, status, title, language, descriptions, logo, cover and metadata related to its creation and updates. The entity also contains details about the organization it belongs to.
Brands Service
The BrandsService is a core component responsible for managing and handling brand-related operations within the application. It provides methods to create, update, delete, and fetch brand data, ensuring seamless interaction with the underlying Brand
entity.
Brands Resolver
The BrandsResolver is a GraphQL resolver that exposes brand-related API endpoints, allowing clients to interact with the Brands API. It handles incoming queries and mutations, delegating the actual processing of the requests to the BrandsService
. By utilizing the BrandsResolver
, clients can easily manage and retrieve brand information, such as creating, updating, fetching, or deleting brands, through GraphQL queries and mutations.
How to install BrandsModule to my app
To create a BrandsModule
in NestJS, you can follow these steps:
Install the required dependencies for
BrandsModule
:Create a
brands
folder in your project'ssrc/modules
directory.Inside the
brands
folder, create abrand.entity.ts
file and extendsBaseBrand
from NestKit with the following code:ts// src/modules/brands/brand.entity.ts import { BaseBrand } from '@deeepvision/nest-kit/dist/modules/brands'; import { MaybeNull } from '@deeepvision/nest-kit'; import { Field, ObjectType } from '@nestjs/graphql'; import { ChildEntity } from 'typeorm'; import { Image } from '../images/image.entity'; import { Organization } from '../organizations/organization.entity'; @ObjectType() @ChildEntity() export class Brand extends BaseBrand { @Field(() => Image, { nullable: true, }) logo!: Promise<MaybeNull<Image>>; @Field(() => Image, { nullable: true, }) cover!: Promise<MaybeNull<Image>>; @Field(() => Organization) organization!: Promise<Organization>; }
Inside the
brands
folder, create abrands.service.ts
file and extendsBaseBrandsService
from NestKit with the following code:ts// src/modules/brands/brands.service.ts import { BaseBrandsService } from '@deeepvision/nest-kit/dist/modules/brands'; import { IdService } from '@deeepvision/nest-kit/dist/modules/id'; import { Brand } from './brand.entity'; export class BrandsService extends BaseBrandsService<Brand> { constructor( protected readonly idService: IdService, ) { super(); } // protected generateEntityId() { // return this.idService.generateEntityId('b', 'hc'); // } }
Inside the
brands
folder, create abrands.resolver.ts
file and extendsBaseBrandsResolver
with the following code:ts// src/modules/brands/brands.resolver.ts import { ActionContext } from '@/decorators'; import { GraphQLJwtAuthGuard, GraphQLPermissionsGuard, } from '@deeepvision/nest-kit'; import { BaseBrandsResolver } from '@deeepvision/nest-kit/dist/modules/brands'; import { UseGuards } from '@nestjs/common'; import { Resolver, } from '@nestjs/graphql'; import { Brand } from './brand.entity'; @Resolver(() => Brand) @UseGuards(GraphQLJwtAuthGuard, GraphQLPermissionsGuard) export class BrandsResolver extends BaseBrandsResolver(Brand) {}
Inside the
brands
folder, create abrands.module.ts
file with the following code:ts// src/modules/brands/brands.module.ts import { BRANDS_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/brands'; import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Brand } from './brand.entity'; import { BrandsResolver } from './brands.resolver'; import { BrandsService } from './brands.service'; @Module({ imports: [ TypeOrmModule.forFeature([ Brand, ]), ], providers: [ BrandsResolver, BrandsService, { provide: BRANDS_SERVICE_TOKEN, useExisting: BrandsService, }, ], exports: [ BrandsService, { provide: BRANDS_SERVICE_TOKEN, useExisting: BrandsService, }, TypeOrmModule, ], }) export class BrandsModule {}
Finally, import the
BrandsModule
into your mainAppModule
or another module:ts// src/modules/app.module.ts import { Module } from '@nestjs/common'; import { BrandsModule } from './modules/brands/brands.module.ts'; @Module({ imports: [BrandsModule], }) export class AppModule {}
In this code, we are importing the
BrandsModule
and adding it to theimports
array of ourAppModule
.
How to create new brand
Here's an example of using the BrandsService.create
method with a CreateBrandOptions
:
ts
const brand = await this.brandsService.create({
id: 'hcb:gj39hkbngh34',
code: 'HSS',
title: 'Hope Sabbath School',
language: 'eng',
type: BrandType.TV_SHOW,
shortDescription: 'A weekly Bible study program',
longDescription: 'Hope Sabbath School is a weekly Bible study program that explores various topics in the Scriptures',
organizationId: 'hcorg:hj39hklnbmrt',
logoId: 'hcimg:jh839knvbtr',
coverId: 'hcimg:xj349knvbtr',
madeForKids: false,
});
The create method returns a Promise
that resolves to the newly created brand object.
Create new brand with GraphQL
Here's an example GraphQL mutation to create a new brand.
How to get brand by ID
Use the getOne()
method from your BrandsService
to get a brand by id in other parts of your application.
ts
const brand = await this.brandsService.getOne('hcb:xxxxxxxxxxx');
Use the getOneOrFail()
method to retrieve a brand by id or throw an error if the brand is not found.
ts
const brand = await this.brandsService.getOneOrFail('hcb:xxxxxxxxxxx', ctx);
Fetch brand by id with GraphQL
Here's an example GraphQL query to fetch brand by id.
How to get brand by code
Use the getOneByCode()
method in other parts of your application to retrieve a brand by their unique code.
ts
const brand = await this.brandsService.getOneByCode('HCC');
Use the getOneByCodeOrFail()
method to retrieve a brand by their code or throw an error if the brand is not found.
ts
const brand = await this.brandsService.getOneByCodeOrFail('HCC', ctx);
Fetch brand by code with GraphQL
Here's an example GraphQL query to fetch brand by unique code.
How to get brands list
Use the getMany()
method in other parts of your application to retrieve a list of brands.
ts
const [brands, meta] = await this.brandsService.getMany({
filter: {
...
},
orderBy: BrandsOrderBy.title_ASC,
});
See BrandsFilter
and BrandsOrderBy
for more information on how to filter and order brands.
Fetch brand list with GraphQL
Here's an example GraphQL query to fetch brands.
How to update brand
Call the update
method of the BrandsService
and pass in an object of UpdateBrandOptions
:
ts
const updatedBrand = await this.brandsService.update({
id: 'hcb:gj39hkbngh34',
code: 'HSS',
title: 'New Hope Sabbath School',
language: 'eng',
type: BrandType.TV_SHOW,
shortDescription: 'An updated weekly Bible study program',
longDescription: 'New Hope Sabbath School is an updated weekly Bible study program that explores various topics in the Scriptures.',
logoId: 'hcimg:xxxxxxxxxxx',
coverId: 'hcimg:xxxxxxxxxxx',
madeForKids: false,
});
All fields in the upate input are optional except for the id
field.
The update
method returns a Promise
that resolves to the updated brand object.
Update brand with GraphQL
Here's an example GraphQL mutation to update brand.
How to delete brand
Call the delete
method of the BrandsService
and pass in the id
of the brand you want to delete:
ts
await this.brandsService.delete('hcb:xxxxxxxxxxx');
In this example, we are deleting a brand with an id
of hcb:xxxxxxxxxxx
.
Delete brand with GraphQL
Here's an example GraphQL mutation to delete brand.
BrandsService uses Soft-Delete feature
Soft delete is a feature in NestKit that allows you to mark a record as "deleted" instead of physically deleting it from the database. When you use soft delete, the record is not actually removed from the database, but a status sets to DELETED
to indicate that the record has been "soft-deleted". This allows you to retain the data and history associated with the record, while still hiding it from normal queries.