Skip to content

Entities ​

Frozen values with a stable identity, compared by id, not by fields.

package @smitejs/domain

An entity is a value that keeps a stable identity across changes. Two entities are the same when their id matches, even if every other field differs — that is what distinguishes an entity from a value object.

domain.entity({ name, id, schema }) declares the identity key and the shape. create(input) validates and returns a frozen entity (or a domain.validation failure), and equals(other) compares by identity. Entities are the natural carrier for the things you persist and reference: orders, customers, accounts.

Keep the id in the schema and point id at it. Everything else — note, status, quantity — can change, but the identity is fixed for the lifetime of the entity.

ts
const Order = entity({
  name: "Order",
  id: "id",
  schema: z.object({ id: z.number().int().positive(), note: z.string() }),
});

const same = Order.create({ id: 1, note: "first" })
  .map((a) =>
    Order.create({ id: 1, note: "second" })
      .map((b) => a.equals(b))
      .unwrapOr(false),
  )
  .unwrapOr(false);
ts
true

Released under the MIT License.

/ search•[ ] prev/next•j/k scroll•h home•t theme•guide/domain/entities