Business entities represent legal, operational, customer, organizational, or ownership structures. They are hierarchical, maintained through a closure table.

Acme Holdings
  Acme Trading Ltd
  Acme Treasury Ltd

Bluewater Trust
  Bluewater Operating Company

Schema: business_entities

fieldtypenotes
idbigintPK
parent_idbigintself-reference; nil for roots
namestringrequired
legal_namestring
entity_typestringindividual, company, trust, partnership, fund, bank, branch, department, counterparty, internal
statusstringpending, active, inactive, frozen, closed
jurisdictionstring
external_idstringunique
metadatamap
inserted_at / updated_atutc_datetime

Schema: business_entity_closure

fieldtype
ancestor_idbigint
descendant_idbigint
depthinteger

Each row records that ancestor is an ancestor of descendant at depth. A self-row has depth 0. The closure table is rewritten on insert and on move_entity/2 so that:

  • a self-row (depth 0) is always present
  • cycles are prevented
  • subtree and ancestor queries are O(closure rows), not recursive CTEs

Functions

Logistiki.BusinessEntities.create_entity(attrs)
Logistiki.BusinessEntities.update_entity(entity, attrs)
Logistiki.BusinessEntities.get_entity!(id)
Logistiki.BusinessEntities.list_entities(opts \\ [])
Logistiki.BusinessEntities.list_children(entity)
Logistiki.BusinessEntities.list_descendants(entity)
Logistiki.BusinessEntities.list_ancestors(entity)
Logistiki.BusinessEntities.move_entity(entity, new_parent)

list_descendants/1 and list_ancestors/1 read the closure table directly, which is fast and predictable for subtree balance aggregation and reporting.