Skip to content

Accounts & Organizations Schema

The accounts schema owns companies, memberships, and inter-company business relationships.

It links business entities to users (identity) and physical locations (places) without mingling operational or device data.


Ownership Split

  • identity.users — Defines the human actor or individual account.
  • accounts.companies — Defines the legal or operational organization (customer, contractor, vendor, partner, property_owner, etc.).
  • accounts.company_members — Maps users to companies with explicit roles (owner, admin, manager, member, viewer, billing). Membership requires accepted_at IS NOT NULL and revoked_at IS NULL.
  • accounts.company_relationships — Directional links between companies (from_company_id $\rightarrow$ to_company_id) defining partnerships, vendor agreements, and contractor associations.

Row-Level Security & Helpers

Policies evaluate access via RLS-safe functions in access.*:

  • access.has_company_role(company_id, user_id, role) — Checks if a user has an active, accepted role within a company.
  • access.can_read_company(company_id, user_id) — Grants visibility to active members or related business partners.
  • access.can_manage_company(company_id, user_id) — Restricts administrative mutations to owner and admin members.

Exposed API Contract

  • Views: api.companies, api.my_companies, api.company_members, api.company_relationships (security-invoker views scoped to the caller).
  • RPCs: api.create_company(...) manages company initialization and automatic owner membership in a single transaction.