Database Models
Complete reference for all Mongoose schemas — field definitions, types, validations, virtuals, indexes, and relationships.
Database Models
All data models are defined using Mongoose schemas and stored in MongoDB. The models live in backend/models/ and define the complete data layer of the SLMS.
Entity Relationship Diagram
┌──────────┐ ┌──────────┐ ┌───────────┐
│ Category │──1:N──│ Book │──1:N──│ BookCopy │
└──────────┘ └──────────┘ └─────┬─────┘
│
1:N │ 1:N
▼
┌──────────┐ ┌─────────────┐
│ User │──1:N──│ Transaction │
└────┬─────┘ └──────┬──────┘
│ │
│ │
┌──────────┴──────────┐ │
│ │ │
▼ ▼ ▼
┌────────────────┐ ┌────────────┐┌──────────┐
│ RolePermission │ │Reservation ││ Fine │
└────────────────┘ └────────────┘└──────────┘
│
▼
┌──────────┐
│ Role │
└──────────┘
Core Security Models
1. User Model
File: backend/models/User.js
| Field | Type | Required | Unique | Description |
|---|---|---|---|---|
email | String | ✅ | ✅ | User's email address |
passwordHash | String | ✅ | — | Argon2id-hashed password |
firstName | String | ✅ | — | User's first name |
lastName | String | ✅ | — | User's last name |
roleId | ObjectId | ✅ | — | Ref: Role |
status | Enum | — | — | ACTIVE, PENDING, SUSPENDED, DISABLED |
rollNumber | String | — | ✅ (sparse) | University roll number |
contactNumber | String | ✅ | ✅ | Phone contact |
tokenVersion | Number | — | — | Incremented to invalidate all sessions |
loginAttempts | Number | ✅ | — | Tracks brute-force attempts |
lockUntil | Number | — | — | Timestamp for account lockout |
Virtuals: transactions, reservations, fines
2. Role & Permission Models
Files: Role.js, Permission.js, RolePermission.js
Decoupled RBAC system.
- Role:
name,description - Permission:
name,description,module - RolePermission: Maps
roleIdtopermissionId(Compound unique index)
3. RefreshToken
File: backend/models/RefreshToken.js
Stores Argon2id hashes of refresh tokens for long-lived sessions.
Includes userId, sessionId, ip, device, browser, and expiresAt (TTL indexed).
Library Domain Models
4. Category Model
File: backend/models/Category.js
Taxonomic classification for organizing books into categories (name, description).
5. Book Model
File: backend/models/Book.js
Logical metadata of a book title.
| Field | Type | Description |
|---|---|---|
isbn | String | Unique sparse ISBN |
title | String | Book title |
author | String | Author name |
categoryId | ObjectId | Ref: Category |
Indexes: { title: 'text', author: 'text' } for full-text search.
6. BookCopy Model
File: backend/models/BookCopy.js
Represents a physical copy in the inventory.
| Field | Type | Description |
|---|---|---|
bookId | ObjectId | Ref: Book |
barcode | String | Unique barcode |
status | Enum | AVAILABLE, CHECKED_OUT, RESERVED, etc. |
7. Transaction Model
File: backend/models/Transaction.js
Tracks checkout lifecycle.
| Field | Type | Description |
|---|---|---|
userId | ObjectId | Ref: User |
bookCopyId | ObjectId | Ref: BookCopy |
issuedAt | Date | Issue timestamp |
dueDate | Date | When return is expected |
returnedAt | Date | Null until returned |
Operations Models
8. Fine Model
File: backend/models/Fine.js
Tracks financial penalties (transactionId, userId, amount, status: UNPAID/PAID).
9. Reservation Model
File: backend/models/Reservation.js
Tracks book holds. Maps a User to a Book (or specific BookCopy).
10. AuditLog Model
File: backend/models/AuditLog.js
Immutable ledger of security events (action, userId, ipAddress, status).
11. SystemConfig Model
File: backend/models/SystemConfig.js
Key-value store for dynamic settings (e.g., FINE_RATE_PER_DAY).