Common

This library works as a repository of common classes, interfaces and constants that are used in almost every other library.

Constants

  • EVENTS_QUEUE is the name of the queue to be used for application specific events (see below).

Events

There are two basic events defined: Internal and External.

Internal

Internal events are events generated by a service to trigger an action in a different service. For example, when a new user is created, the UserService is generating an event to trigger the creation of a bucket in storage layer to hold the user's files.

To handle the aformentioned scenario, we need to inject the appropriate queue:

constructor(@InjectQueue(EVENTS_QUEUE) private readonly eventsQueue: Queue<InternalEvent>) { /* ...  */}

and then create and publish the appropriate event:

const user = new User();
this.eventsQueue.add(UserEvent.Created, new InternalEvent(user, UserEvent.Created));

For more information, see Queues documentation for NestJS.

External

External events were defined to be used for triggering an external service (i.e. push a message to RabbitMQ, call an API), but they are not used for anything so far.

Entities

To avoid code duplication, a BaseEntity abstract class is provided, that includes the columns that are in (almost) every other entity:

  • id: unsigned integer, auto-increment, primary key,
  • createdAt: datetime, the timestamp a record was created,
  • updatedAt: datetime, the timestamp a record last updated (or created, if never updated),

To use it, simply generate a class extending this:

import { BaseEntity } from '@suite5/common';
import { Entity } from 'tyepeorm';

@Entity()
export class User extends BaseEntity { /* ... */ }

Validators

In addition to the above, the core library provides a custom validator, to extend the ones provided by the class-validator package.

EqualToValidator

This validator is used to compare that two attributes are equal. For example, to confirm that the password the user provided and confirmed is correct:

import { IsEqualTo } from '@suite5/common';

export class UpdatePasswordDTO {
    readonly password: string;

    @IsEqualTo('password', { message: 'passwordRepeat must be equal to password' })
    readonly passwordRepeat: string;
}

(additional decorators are omitted to focus only on the usage of our validator).