Smart Library Docs

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

FieldTypeRequiredUniqueDescription
emailStringUser's email address
passwordHashStringArgon2id-hashed password
firstNameStringUser's first name
lastNameStringUser's last name
roleIdObjectIdRef: Role
statusEnumACTIVE, PENDING, SUSPENDED, DISABLED
rollNumberString✅ (sparse)University roll number
contactNumberStringPhone contact
tokenVersionNumberIncremented to invalidate all sessions
loginAttemptsNumberTracks brute-force attempts
lockUntilNumberTimestamp 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 roleId to permissionId (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.

FieldTypeDescription
isbnStringUnique sparse ISBN
titleStringBook title
authorStringAuthor name
categoryIdObjectIdRef: 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.

FieldTypeDescription
bookIdObjectIdRef: Book
barcodeStringUnique barcode
statusEnumAVAILABLE, CHECKED_OUT, RESERVED, etc.

7. Transaction Model

File: backend/models/Transaction.js Tracks checkout lifecycle.

FieldTypeDescription
userIdObjectIdRef: User
bookCopyIdObjectIdRef: BookCopy
issuedAtDateIssue timestamp
dueDateDateWhen return is expected
returnedAtDateNull 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).

On this page