Skip to content

Backend Architecture and Database Schema

Ori Ochayon edited this page Nov 24, 2023 · 2 revisions

File Structure of the Node.js App

my-book-app/
│
├── models/                    # Contains Mongoose models that define the structure of database documents.
│   ├── user.js                # Defines the User model with fields like username, password, and reading preferences.
│   └── book.js                # Defines the Book model with details like title, author, content, and audiobook link.
│
├── routes/                    # Holds the Express routes, mapping endpoints to controller logic.
│   ├── users.js               # User-related routes (registration, login, profile management).
│   └── books.js               # Book-related routes (listing, details, rating, and reviews).
│
├── controllers/               # Contains the logic to process requests, interact with the database, and return responses.
│   ├── userController.js      # Functions for user management, like creating new users or updating user profiles.
│   └── bookController.js      # Functions for book operations, like fetching book details or updating book ratings.
│
├── middleware/                # Custom middleware for functionalities like authentication and error handling.
│   └── auth.js                # Middleware for user authentication, possibly using JWT for secure token-based auth.
│
├── utils/                     # Utility functions and helpers, such as for pagination or common response patterns.
│   └── [various utility files] # Could include a pagination helper, error handling utilities, etc.
│
├── app.js                     # The main server file. Sets up Express, connects middleware, and links routes.
└── package.json               # Node project manifest listing dependencies, scripts, and project metadata.

Database Schema

User Model (models/user.js)

  • Detailed Schema Explanation:
    • username: A unique string identifier for each user. Used for login and identification.
    • password: A hashed password string for security. Hashing should be done using a library like bcrypt.
    • readingPreferences: An object containing user-specific settings like preferred font size and theme.
    • progressTracking: An array to track the user's reading progress across different books.
    • favorites: An array of Book model ObjectIds marking the user's favorite books for quick access.

Book Model (models/book.js)

  • Detailed Schema Explanation:
    • title: The title of the book. A string that is required for every book entry.
    • author: Author(s) of the book. This field can accommodate multiple authors if necessary.
    • publishedYear: The year the book was published. Useful for sorting and categorization.
    • genres: An array of strings, each representing a genre that the book belongs to.
    • summary: A brief description or summary of the book's content.
    • content: An array where each element represents a page in the book, including the page number and text.
    • audiobookLink: An optional string field containing a URL to the book's audiobook file, if available.
    • ratings: An array of objects for user-submitted ratings, linking to the User model and the rating value.
    • reviews: An array of objects for user-submitted reviews, including the User model reference and review text.

Routes and Controllers

User Routes (routes/users.js)

  • Detailed Explanation:
    • Registration Endpoint: Allows new users to register. Should handle data validation and user creation.
    • Login Endpoint: Authenticates users. Should verify credentials and issue a token or session.
    • Profile Retrieval: Enables users to view their profile information.
    • Profile Update: Allows users to update their profile details, like reading preferences.

Book Routes (routes/books.js)

  • Detailed Explanation:
    • Book Listing: Provides a list of books, potentially with pagination. Can include filters like genre or author.
    • Book Detail: Retrieves detailed information about a specific book, including content and audiobook link.
    • Book Rating: Endpoint for users to submit a rating for a book.
    • Book Review: Endpoint for users to write a review for a book.