Database Schema

Overview

This document describes the database schema for the Expense Management module in the Bee O'clock panel service. The schema is designed using MongoDB with Mongoose ODM, following Domain-Driven Design principles and implementing multi-tenancy, soft delete patterns, and comprehensive audit trails.

Database Design Principles

Multi-Tenancy

  • All collections include tenantId for data isolation

  • Business entity scoping with businessEntityId

  • Automatic tenant filtering in all queries

Soft Delete Pattern

  • Logical deletion using archivedAt timestamp

  • Preservation of historical data for audit purposes

  • Filtered queries exclude archived records by default

Audit Trail

  • Automatic createdAt and updatedAt timestamps

  • User context tracking for all modifications

  • Immutable expense history for compliance

Performance Optimization

  • Strategic indexing for common query patterns

  • Compound indexes for multi-field filtering

  • Text indexes for search functionality

Collections

1. Expenses Collection

Collection Name: expenses

Purpose: Stores comprehensive expense records with itemized breakdown and categorization.

Schema Definition

File: applications/panel.beeoclock/src/modules/expense/infrastructure/persistence/schemas/expense.scheme.ts

Field Specifications

Field
Type
Required
Description
Constraints

_id

ObjectId

Yes

Primary key

Auto-generated MongoDB ObjectId

tenantId

String

Yes

Tenant identifier for multi-tenancy

Inherited from base schema

businessEntityId

String

Yes

Business entity identifier

Inherited from base schema

totalValue

ExpenseValueSchema

Yes

Total monetary amount

Must be positive, embedded document

expensedAt

Date

Yes

Date of expense occurrence

Within 5 years past to 30 days future

description

String

No

Expense description

Max 1000 characters, trimmed

items

ExpenseItemSchema[]

No

Itemized expense breakdown

Sum must match total value

archivedAt

Date

No

Soft delete timestamp

Null for active records

createdAt

Date

Yes

Record creation timestamp

Auto-managed by timestamps

updatedAt

Date

Yes

Last modification timestamp

Auto-managed by timestamps

Business Rules and Constraints

  1. Amount Validation: Total value amount must be positive

  2. Date Validation: Expense date within reasonable range (5 years past to 30 days future)

  3. Currency Consistency: All items must use the same currency as total value

  4. Sum Validation: Sum of item amounts must match total amount (within 1 cent tolerance)

  5. Soft Delete: Archived expenses are excluded from standard queries

  6. Text Search: Description field supports full-text search operations

2. Expense Categories Collection

Collection Name: expense-categories

Purpose: Stores categorization tags for organizing and filtering expenses.

Schema Definition

File: applications/panel.beeoclock/src/modules/expense/infrastructure/persistence/schemas/expense-category.scheme.ts

Field Specifications

Field
Type
Required
Description
Constraints

_id

ObjectId

Yes

Primary key

Auto-generated MongoDB ObjectId

tenantId

String

Yes

Tenant identifier

Inherited from base schema

businessEntityId

String

Yes

Business entity identifier

Inherited from base schema

name

String

Yes

Category name

Max 50 chars, alphanumeric/hyphens/underscores/spaces, lowercase

description

String

No

Category description

Max 500 characters, trimmed

archivedAt

Date

No

Soft delete timestamp

Null for active records

createdAt

Date

Yes

Record creation timestamp

Auto-managed

updatedAt

Date

Yes

Last modification timestamp

Auto-managed

Business Rules and Constraints

  1. Name Uniqueness: Category names must be unique within a tenant (excluding archived)

  2. Name Format: Only alphanumeric characters, hyphens, underscores, and spaces allowed

  3. Case Insensitive: Names are stored in lowercase for consistency

  4. Length Limits: Name limited to 50 characters, description to 500 characters

  5. Soft Delete: Archived categories excluded from standard operations but preserve historical references

Embedded Schemas

1. Expense Value Schema

Purpose: Represents monetary amounts with currency information.

File: applications/panel.beeoclock/src/modules/expense/infrastructure/persistence/schemas/expense-value.schema.ts

2. Expense Item Schema

Purpose: Represents individual line items within an expense.

File: applications/panel.beeoclock/src/modules/expense/infrastructure/persistence/schemas/expense-item.schema.ts

3. Expense Source Schema

Purpose: Represents the source or origin of an expense item.

File: applications/panel.beeoclock/src/modules/expense/infrastructure/persistence/schemas/expense-source.schema.ts

Database Indexes

Primary Indexes

Expenses Collection

Expense Categories Collection

Query Performance Optimization

Common Query Patterns

Data Validation and Constraints

Schema-Level Validation

Application-Level Constraints

Migration Scripts

Initial Schema Creation

Sample Data Migration

Backup and Recovery

Backup Strategy

Data Retention Policy

Performance Monitoring

Query Performance Analysis

Security Considerations

Data Encryption

Access Control

This comprehensive database schema documentation provides detailed insights into the storage structure, performance optimization, security measures, and operational considerations for the expense management system. The schema is designed to be scalable, secure, and performant while maintaining data integrity and supporting complex business requirements.

Last updated

Was this helpful?