UniqueEntityID

Defines a unique ID (128 bits - RFC 4122) for an entity

It provides immutable UUID for entity

new UniqueEntityID(param?: string); // Optional param

const id = new UniqueEntityID();

console.log(id);

> UniqueEntityID {value: "e867e8cf-2604-4034-8858-c38da17c4f88"}

You can get the result if you want.

const id = new UniqueEntityID();

console.log(id.value);

> "e867e8cf-2604-4034-8858-c38da17c4f88"

If you provide a value, the id will be created with provided value.

const id = new UniqueEntityID('my_id_value');

console.log(id.value);

> "my_id_value"

It has its own method of comparing values.

const idA = new UniqueEntityID('my_id_value_A');
const idB = new UniqueEntityID('my_id_value_B');
const idC = new UniqueEntityID('my_id_value_B');

console.log(idA.equals(idB));
> false

console.log(idB.equals(idC));
> true

Use DomainId

all domain entities by default have an id defined by BaseDomainEntity

Import { DomainId } from 'types-ddd';

const ID = DomainId.create();

console.log(ID.value)
> "e867e8cf-2604-4034-8858-c38da17c4f88"

The argument is optional. If informed, It will be considered as value.

const ID = DomainId.create("uuid-from-database");

console.log(ID.value)
> "uuid-from-database"

Short ID

If you prefer short ids, you may use ShortDomainId, default 16 bytes

import { DomainId, ShortDomainId } from 'types-ddd';

const ID = DomainId.create();

console.log(ID.uid);
> "461235de-ec04-48aa-af94-31fbfa95efcf"

console.log(ID.toShort());
> "31fbb4859e3301fg"

console.log(ID.toShort(18));
> "31fbb4859e3301fcfe"

console.log(ShortDomainId.create())
> "4859eec0123595ef"

Last updated