Overview
Memberships represent the many-to-many relationship between users and companies. Each membership defines:- A user’s association with a specific company
- Their status within the company (invited, active, suspended)
- Their assigned roles and permissions
- Their organizational position (job title, department, supervisor)
- Contract details (type, hourly rate)
Membership Model
Membership Structure
Unique Constraint
Each user can only have one membership per company:- No duplicate memberships
- Clear user-company relationship
- Simplified permission checks
Membership Status
Memberships progress through different statuses:INVITED
Initial state when a membership is created via invitation.Characteristics:
- User can see the invitation in their pending invitations list
- User cannot access company resources yet
- Membership includes assigned roles (typically default role)
invitedAttimestamp is setactivatedAtis null
- User accepts → Status changes to
ACTIVE - User declines → Membership is deleted
- Invitation expires → Optionally auto-expire membership
ACTIVE
Active state when user has accepted the invitation.Characteristics:
- User has full access to company resources (subject to permissions)
- User appears in company member lists
- Roles and permissions are enforced
activatedAttimestamp is set
- Admin suspends member → Status changes to
SUSPENDED - Admin removes member → Membership is deleted
SUSPENDED
Suspended state when access is temporarily revoked.Characteristics:
- User cannot access company resources
- Membership data is preserved
- Can be reactivated by admin
- User still appears in member lists (with suspended indicator)
- Admin reactivates → Status changes to
ACTIVE - Admin removes member → Membership is deleted
Status Transitions
Contract Types
Memberships include contract classification for employment tracking:- EMPLOYEE
- FREELANCE
- INTERN
- CONTRACTOR
- OTHER
Full-time or part-time employees with standard employment contracts.Common for:
- Regular staff members
- Salaried positions
- Benefits-eligible workers
Hourly Rate
OptionalhourlyRate field stores compensation for time tracking:
- Time entry calculations
- Project costing
- Payroll estimation
- Client billing
Supervisor Hierarchy
Memberships support a self-referential supervisor structure:Supervisor Relationship
supervisorMembershipIdreferences another membership in the same company- Creates a tree structure within each company
ON DELETE SetNull- removing supervisor preserves subordinate records- One supervisor per member (direct reporting structure)
Hierarchy Example
Querying Hierarchy
Role Assignment
Memberships can have multiple roles through theMembershipRole join table:
Membership Operations
List Company Members
Invite Member
Update Member Roles
Remove Member
User’s Pending Invitations
Accept Invitation
Decline Invitation
Search Non-Members
When inviting members, you can search for users who are not yet members:Metadata Field
Memberships include a flexiblemetadata JSON field:
- Custom fields per company (employee ID, office location, etc.)
- Integration data (external system references)
- Temporary flags or settings
- Audit trail for custom workflows
Access Control
Memberships are the foundation of company-level access control:Checking Access
Middleware Integration
ThecheckCompanyAccess middleware uses memberships:
Best Practices
Validate Company Context
When updating memberships, verify that related entities (roles, supervisors) belong to the same company.
Handle Status Transitions
Implement proper state machines for status changes. Validate that transitions are valid (e.g., can’t go from INVITED to SUSPENDED).
Audit Membership Changes
Log who added, removed, suspended, or reactivated members. Track role assignments and supervisor changes.
Clean Up Invitations
Periodically review INVITED memberships and expire those past their invitation date.
Prevent Circular Supervisors
Validate supervisor assignments to prevent cycles in the hierarchy (A → B → C → A).
Handle Member Removal
When removing a member, decide what to do with their subordinates (reassign or clear supervisor).
Use Soft Suspensions
Prefer SUSPENDED status over deletion to preserve membership history and enable reactivation.
Optimize Queries
Use the composite index on
(companyId, userId) for access checks. Include roles/user data in single queries.Related Concepts
Multi-Tenancy
Learn how memberships connect users to company tenants
RBAC
Understand how roles are assigned to memberships
Invitations
Explore how memberships are created via invitations