Appearance
OrganizationGroupsModule
The NestJS organization groups module is a pre-built module for managing organization groups in a NestJS application. For example Hope Channel International
and Hope Channel Ukraine
belogs to Hope Channel
group.
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:
OrganizationGroup Entity
The OrganizationGroup entity is a data model that represents a group of organizations in a NestJS application. It typically includes fields such as a unique identifier (id
), a group display name (title
), a description (shortDescription
), and other attributes.
The OrganizationGroup
entity is used in conjunction with the Organization entity, which represents individual organizations within the application.
NestJS Organization Groups Service
A OrganizationGroupsService provides programmatic access to the Organization Groups API. It typically includes methods for creating, retrieving, updating, and deleting groups entities.
GraphQL Organization Groups Resolver
The GraphQL OrganizationGroupsResolver in NestKit provides a convenient way to retrieve and manipulate organization groups data in a GraphQL API.
How to install OrganizationGroupsModule to my app
To create a OrganizationGroupsModule
in NestJS, you can follow these steps:
Install the required dependencies for
OrganizationGroupsModule
:Create a
organization-groups
folder in your project'ssrc/modules
directory.Inside the
organization-groups
folder, create aorganization-group.entity.ts
file and extendsBaseOrganizationGroup
from NestKit with the following code:ts// src/modules/organization-groups/organization-group.entity.ts import { MaybeNull } from '@deeepvision/nest-kit'; import { BaseOrganizationGroup } from '@deeepvision/nest-kit/dist/modules/organization-groups'; import { Field, ObjectType } from '@nestjs/graphql'; import { ChildEntity } from 'typeorm'; import { Image } from '../images/image.entity'; import { Organization } from '../organizations/organization.entity'; import { User } from '../users/user.entity'; @ObjectType() @ChildEntity() export class OrganizationGroup extends BaseOrganizationGroup { @Field(() => User, { nullable: true, }) creator!: Promise<MaybeNull<User>>; @Field(() => Image, { nullable: true, }) logo!: Promise<MaybeNull<Image>>; @Field(() => [Organization]) organizations!: Promise<Organization[]>; }
Inside the
organization-groups
folder, create aorganization-groups.service.ts
file and extendsBaseOrganizationGroupsService
from NestKit with the following code:ts// src/modules/organization-groups/organization-groups.service.ts import { BaseOrganizationGroupsService } from '@deeepvision/nest-kit/dist/modules/organization-groups'; import { OrganizationGroup } from './organization-group.entity'; export class OrganizationGroupsService extends BaseOrganizationGroupsService<OrganizationGroup> {}
Inside the
organization-groups
folder, create aorganization-groups.resolver.ts
file and extendsBaseOrganizationGroupsResolver
with the following code:ts// src/modules/organization-groups/organization-groups.resolver.ts import { BaseOrganizationGroupsResolver } from '@deeepvision/nest-kit/dist/modules/organization-groups'; import { Resolver } from '@nestjs/graphql'; import { OrganizationGroup } from './organization-group.entity'; @Resolver(() => OrganizationGroup) export class OrganizationGroupsResolver extends BaseOrganizationGroupsResolver(OrganizationGroup) {}
Inside the
organization-groups
folder, create aorganization-groups.module.ts
file with the following code:ts// src/modules/organization-groups/organization-groups.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { ImagesModule } from '../images/images.module'; import { OrganizationGroup } from './organization-group.entity'; import { OrganizationGroupsResolver } from './organization-groups.resolver'; import { OrganizationGroupsService } from './organization-groups.service'; import { Organization } from '@/modules/organizations/organization.entity'; import { ORGANIZATION_GROUPS_SERVICE_TOKEN } from '@deeepvision/nest-kit/dist/modules/organization-groups'; @Module({ imports: [ TypeOrmModule.forFeature([OrganizationGroup, Organization]), ImagesModule, ], providers: [ OrganizationGroupsService, { provide: ORGANIZATION_GROUPS_SERVICE_TOKEN, useExisting: OrganizationGroupsService, }, OrganizationGroupsResolver, ], exports: [ OrganizationGroupsService, { provide: ORGANIZATION_GROUPS_SERVICE_TOKEN, useExisting: OrganizationGroupsService, }, TypeOrmModule, ], }) export class OrganizationGroupsModule {}
Finally, import the
OrganizationGroupsModule
into your main AppModule or another module:ts// src/modules/app.module.ts import { Module } from '@nestjs/common'; import { OrganizationGroupsModule } from './modules/organization-groups/organization-groups.module.ts'; @Module({ imports: [OrganizationGroupsModule], }) export class AppModule {}
In this code, we are importing the
OrganizationGroupsModule
and adding it to theimports
array of ourAppModule
.Overall, creating a
OrganizationGroupsModule
in NestJS is a simple process that involves extending an entity, resolver and service class and then importing the module into your mainAppModule
.
How to create new organization group
Call the create
of OrganizationGroupsService
and pass in an object of CreateOrganizationGroupOptions
:
ts
const group = await this.organizationGroupsService.create({
title: 'Hope Channels',
code: 'HC',
logoId: 'https://example.com/logo.png', // optional
shortDescription: 'Hope Channels is a group of channels that are broadcasting the gospel of Jesus Christ' // optional
});
The create method returns a Promise
that resolves to the newly created user object.
Create new group with GraphQL
Here's an example GraphQL mutation to create a new organization group.
How to get group by ID
Use the getOne()
method from your OrganizationGroupsService
to get a group by id in other parts of your application.
ts
const group = await this.organizationGroupsService.getOne('hcgrp:xxxxxxxxxxx');
Use the getOneOrFail()
method to retrieve a organization group by id or throw an error if the group is not found.
ts
const group = await this.organizationGroupsService.getOneOrFail('hcgrp:xxxxxxxxxxx', ctx);
Fetch group by id with GraphQL
Here's an example GraphQL query to fetch group by id.
How to get group by code
Use the getOneByCode()
method in other parts of your application to retrieve a group by their unique code.
ts
const group = await this.organizationGroupsService.getOneByCode('HC');
Use the getOneByCodeOrFail()
method to retrieve a group by their code or throw an error if the group is not found.
ts
const group = await this.organizationGroupsService.getOneByCodeOrFail('HC', ctx);
Fetch group by code with GraphQL
Here's an example GraphQL query to fetch group by unique code.
How to get groups list
Use the getMany()
method in other parts of your application to retrieve a list of organization groups.
ts
const [groups, meta] = await this.organizationGroupsService.getMany({
filter: {
...
},
orderBy: OrganizationGroupsOrderBy.title_ASC,
});
See OrganizationGroupsFilter
and OrganizationGroupsOrderBy
for more information on how to filter and order groups.
Fetch groups list with GraphQL
Here's an example GraphQL query to fetch groups.
How to update group
Call the update
method of the OrganizationGroupsService
and pass in an object of UpdateOrganizationGroupOptions
:
ts
const updatedGroup = await this.organizationGroupsService.update({
id: 'hcgrp:hope-channels',
title: 'Hope Channels Network', // optional
code: 'HCNET', // optional
logoId: 'https://example.com/logo2.png', // optional
shortDescription: 'Hope Channels is a group of channels that are broadcasting the gospel of Jesus Christ' // optional
});
All fields in the upate input are optional except for the id
field.
The update
method returns a Promise
that resolves to the updated organization group object.
Update group with GraphQL
Here's an example GraphQL mutation to update organization group.
How to delete group
Call the delete
method of the OrganizationGroupsService
and pass in the id
of the group you want to delete:
ts
await this.organizationGroupsService.delete('hcgrp:xxxxxxxxxxx');
In this example, we are deleting a group with an id
of hcgrp:xxxxxxxxxxx
.
Delete group with GraphQL
Here's an example GraphQL mutation to delete organization group.
OrganizationGroupsService 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.