Database Schema
Overview
The member management module uses MongoDB with Mongoose ODM for data persistence. The schema follows Domain-Driven Design principles with proper separation between domain models and persistence models.
Core Entities
Member Entity
Collection: member
Schema Definition: MemberSchema
_id
ObjectId
Auto
Generated
Unique identifier
firstName
String
No
-
Member's first name
lastName
String
No
-
Member's last name
phone
String
No
-
Contact phone number
email
String
Yes
-
Email address (unique per tenant)
avatar
ObjectId
No
-
Reference to Media entity
memberContext
ObjectId
No
-
Reference to MemberContext entity
profileStatus
String
No
active
Current status of member profile
language
String
No
en
Preferred language code
referal
ReferalSchema
No
-
Invitation and referral information
role
String
No
ADMIN
Legacy role field (deprecated)
roles
Array<RoleSchema>
No
[]
New role-based permissions
assignments
AssignmentsSchema
No
-
Service assignment configuration
stateHistory
Array<StateHistorySchema>
No
[]
State change tracking
createdAt
Date
Auto
Current
Creation timestamp
updatedAt
Date
Auto
Current
Last update timestamp
Profile Status Enum
Usage: Tracks the current state of a member's profile for access control and filtering.
Language Code Enum
Follows ISO 639-1 standard language codes for internationalization support.
Sub-Schemas
Referral Schema (ReferalSchema)
ReferalSchema)Purpose: Manages invitation codes and referral tracking for member onboarding.
invitationCode
String
Yes
Generated
Unique invitation code
active
Number
No
ActiveEnum.YES
Whether invitation is active
Configuration:
_id: false- No separate ID for sub-documenttimestamps: false- No automatic timestamps
Business Rules:
Invitation codes are auto-generated using secure random generation
Active status controls whether invitation can be used
Used for member invitation workflows
Assignments Schema (AssignmentsSchema)
AssignmentsSchema)Purpose: Defines which services and features a member has access to.
service
AssignmentServiceSchema
Yes
-
Service access configuration
Configuration:
_id: false- No separate ID for sub-documenttimestamps: false- No automatic timestamps
Assignment Service Schema (AssignmentServiceSchema)
AssignmentServiceSchema)Purpose: Configures service-level access permissions.
full
Boolean
Yes
false
Whether member has full service access
include
Array<AssignedServiceSchema>
No
[]
Specific services if not full access
Configuration:
_id: false- No separate ID for sub-documenttimestamps: false- No automatic timestamps
Business Logic:
When
full: true, member has access to all servicesWhen
full: false, only services inincludearray are accessibleProvides granular service-level permission control
Assigned Service Schema (AssignedServiceSchema)
AssignedServiceSchema)Purpose: References specific services for granular access control.
service
ObjectId
Yes
-
Reference to Service entity
Configuration:
_id: false- No separate ID for sub-documenttimestamps: false- No automatic timestampsref: Service.name- References Service collection
Relationships
Member โ Media (Avatar)
Type: One-to-One (Optional)
Field:
avatarCollection:
mediaDescription: References uploaded avatar image file
Member โ MemberContext
Type: One-to-One (Optional)
Field:
memberContextCollection:
member-contextDescription: Links member to authentication context and account
Member โ Roles
Type: One-to-Many
Field:
rolesSchema:
RoleSchemaDescription: New role-based permission system
AssignedService โ Service
Type: Many-to-One
Field:
assignments.service.include[].serviceCollection:
serviceDescription: References specific services for access control
Indexes
Recommended Indexes
Performance Considerations
Email Uniqueness: Compound index with tenant ensures email uniqueness per tenant
Text Search: Full-text search index for member discovery
Status Filtering: Index on profileStatus for active/deleted member queries
Role Queries: Index on role names for permission-based filtering
Data Integrity
Validation Rules
Email Format: Must be valid email format
Profile Status: Must be one of defined enum values
Language Code: Must be valid ISO 639-1 code
Role References: Must reference existing roles
Service References: Must reference existing services
Constraints
Email Uniqueness: Email must be unique within tenant
Referral Code Uniqueness: Invitation codes must be globally unique
Required Fields: Email is the only strictly required field
Soft Delete: Use profileStatus instead of physical deletion
Data Migration Considerations
Role Migration: Legacy
rolefield being replaced byrolesarrayAssignment Structure: Support for both full and granular access patterns
Context Linking: MemberContext may be optional during migration
State History: Preserved for audit and rollback capabilities
Audit and History
State History
The stateHistory field tracks all significant state changes:
Profile status changes
Role modifications
Assignment updates
Email changes
Timestamps
createdAt: Automatic creation timestampupdatedAt: Automatic update timestampUsed for audit trails and data analysis
Multi-Tenancy
Tenant Isolation
Members belong to specific tenants
Email uniqueness enforced per tenant
Queries must include tenant context
Cross-tenant access is prevented
Tenant-Aware Operations
All database operations include tenant context:
Create operations include tenant ID
Read operations filter by tenant
Update/Delete operations validate tenant ownership
Backup and Recovery
Backup Strategy
Document Integrity: Full document backup including sub-schemas
Relationship Preservation: Maintain ObjectId references
State History: Include complete audit trail
Media References: Coordinate with media storage backup
Recovery Considerations
Role Dependencies: Ensure role definitions exist before member restore
Service Dependencies: Validate service references during restore
Context Linking: Restore member contexts before members
Invitation Codes: Maintain uniqueness during recovery
Security Considerations
Data Protection
PII Handling: First name, last name, phone, email are PII
Access Control: Role-based access to member data
Audit Logging: State history for compliance
Soft Delete: Maintain data for compliance while marking as deleted
Permission Model
Role-Based: New roles array supports complex permission inheritance
Service-Based: Granular service access control
Owner Protection: Special validation for owner role changes
Self-Service: Limited self-editing capabilities
Performance Monitoring
Query Patterns
Paginated Lists: Monitor pagination query performance
Search Operations: Track full-text search efficiency
Role Filtering: Monitor role-based filtering performance
Assignment Lookups: Track service assignment query patterns
Optimization Strategies
Index Utilization: Ensure proper index usage
Population Strategy: Optimize related data loading
Caching Strategy: Cache frequently accessed member data
Query Aggregation: Use aggregation for complex reporting
Development Guidelines
Schema Evolution
Backward Compatibility: Maintain compatibility during updates
Migration Scripts: Provide migration for schema changes
Default Values: Use appropriate defaults for new fields
Validation Updates: Update validation with schema changes
Testing Strategies
Unit Tests: Test schema validation rules
Integration Tests: Test with real MongoDB instance
Migration Tests: Validate schema migration scripts
Performance Tests: Load test with realistic data volumes
Last updated
Was this helpful?