ObjectClass

Utility class for requests, responses, and entities with the from() factory.

ObjectClass is a lightweight base class used in create requests, in ReadManyPersonResponse, and in entities (EntityBase). It provides the static from() method to build instances from plain objects.

typescript
// src/core/base/object-class.ts
export type ObjectClassProps<T> = {
  [K in keyof T as T[K] extends Function ? never : K]: T[K];
};

export abstract class ObjectClass<T = object> {
  declare protected readonly _propsType?: ObjectClassProps<T>;

  static from<This extends new () => object>(
    this: This,
    props: ObjectClassProps<InstanceType<This>>,
  ): InstanceType<This> {
    return Object.assign(new this(), props) as InstanceType<This>;
  }
}

Create requests extend ObjectClass and combine @AutoMap() and @ApiProperty() decorators:

typescript
export class CreatePersonRequest extends ObjectClass<CreatePersonRequest> {
  @ApiProperty({ example: 'John Doe' })
  @AutoMap()
  name: string;

  @ApiProperty({ type: CreatePersonAddressRequest })
  @AutoMap()
  address: CreatePersonAddressRequest;

  @ApiProperty({ type: CreatePersonContactRequest, isArray: true })
  @AutoMap({ type: () => CreatePersonContactRequest })
  contacts: CreatePersonContactRequest[];
}

List responses also use ObjectClass and the from() factory:

typescript
export class ReadManyPersonResponse extends ObjectClass<
  ListResponse<ReadManyPersonResponseItem>
> {
  @ApiProperty({ type: [ReadManyPersonResponseItem] })
  @AutoMap({ type: () => ReadManyPersonResponseItem })
  items: ReadManyPersonResponseItem[];

  @ApiProperty()
  @AutoMap()
  count: number;
}

In the handler, the response is built with:

typescript
return ReadManyPersonResponse.from(
  await this.repository.findMany(query).then((result) => ({
    items: result.items.map((item) =>
      AutoMapper.map(item, Person, ReadManyPersonResponseItem),
    ),
    count: result.count,
  })),
);

Domain entities extend EntityBase<T>, which inherits from ObjectClass<T>. This maintains consistency across layers without coupling TypeORM entities to HTTP requests.

Koala Nest

A facilitator for building NestJS APIs with DDD architecture. Code copied into your repository — readable, adaptable, and under your control.

Creator

igordrangel.com.br

Design, back-end, and product strategy.

Quick Commands

Global CLI and scripts in the generated project

  • bun install -g @koalarx/nest
  • kl-nest new
  • kl-nest add cache
  • bun run migration:run # CRUD template
  • kl-nest --help
© 2026 Koala NestBuilt for NestJS developers and AI-assisted workflows.