Home > Posts Entity type

Max Heinritz

Entity type

As described in the domain-driven design post, I think it’s best for startups to have a single domain model with a global entity namespace for as long as possible. A shared global entity type enum helps with this.

Enum

import { Enum, EnumValue } from "@kejistan/enum";

export const EntityType = Enum({
  ADDRESS: "ADDRESS",
  ARTIFACT: "ARTIFACT",
  // ...
  TENANT: "TENANT",
  USER: "USER",
});

Use cases

Generating qids:

const qid = generateQid(EntityType.ARTIFACT);

Validating qids in DTOs:

class ArtifactDto {
  @Expose()
  @IsQid(EntityType.ARTIFACT);
  qid: Qid;

  // ...
}

Asserting qid inputs:

const qid = assertQidForEntityType(str, EntityType.ORGANIZATION_NAME_ALIAS);

Matching in tests:

expect(paymentDto.qid).toBeQid(EntityType.PAYMENT);

Constructing qids from uuids in URLs:

// https://corp.foo.com/artifacts/28d48a10-33b8-4bfa-a48e-d31e92442609
const params = useParams();
const userUuid = params.userUuid;
const qid = qidFromUuid(EntityType.ARTIFACT, nullthrows(userUuid));