Data Models

This section provides comprehensive documentation of all data models used throughout the Blog API, including their properties, validation rules, and relationships.

User Models

User (Complete Model)

The primary user data model returned in most API responses.

interface User {
  _id: string; // MongoDB ObjectId, read-only
  username: string; // Unique username, max 20 characters
  email: string; // Unique email address, max 50 characters
  role: "admin" | "user"; // User role, read-only, default: "user"
  firstName?: string; // Optional first name, max 20 characters
  lastName?: string; // Optional last name, max 20 characters
  socialLinks: {
    website?: string; // Optional website URL, max 100 characters
    facebook?: string; // Optional Facebook URL, max 100 characters
    instagram?: string; // Optional Instagram URL, max 100 characters
    linkedin?: string; // Optional LinkedIn URL, max 100 characters
    x?: string; // Optional X (Twitter) URL, max 100 characters
    youtube?: string; // Optional YouTube URL, max 100 characters
  };
  createdAt: string; // ISO 8601 timestamp, read-only
  updatedAt: string; // ISO 8601 timestamp, read-only
}

Example:

UserInputRequired (Registration/Login)

Model for user registration and login requests.

Validation Rules:

  • email: Must be valid email format, unique across all users

  • password: Minimum 8 characters, not returned in responses

  • role: Optional, defaults to "user", admin requires whitelist

Example:

UserUpdateInput (Profile Updates)

Model for updating user profiles.

Note: Field names use snake_case in API requests but are stored as camelCase internally.


Blog Models

Blog (Complete Model)

The primary blog post data model.

Example:

BlogInput (Creation)

Model for creating new blog posts.

Form Data Example:

BlogUpdateInput (Updates)

Model for updating existing blog posts.


Comment Models

Comment (Complete Model)

The primary comment data model.

Example:

CommentInput (Creation)

Model for creating new comments.

Example:


Response Models

Paginated Responses

PaginatedUsers

PaginatedBlogs

Authentication Responses

AccessTokenResponse

LoginResponse

Combines access token with user information.

Example:


Error Models

ErrorResponse (General Errors)

Standard error response format for most API errors.

Common Error Codes:

  • AuthenticationError: Token missing/invalid/expired

  • AuthorizationError: Insufficient permissions

  • NotFound: Resource not found

  • BadRequest: General bad request

  • ServerError: Internal server error

Example:

ValidationErrorResponse (Input Validation)

Detailed error response for input validation failures.

Example:


Data Types and Formats

MongoDB ObjectId

All _id fields use MongoDB ObjectId format:

  • Format: 24-character hexadecimal string

  • Example: "60b7c8b4f9b3c12345678901"

  • Validation: Must be a valid ObjectId when provided

ISO 8601 Timestamps

All date/time fields use ISO 8601 format:

  • Format: YYYY-MM-DDTHH:mm:ss.sssZ

  • Example: "2024-01-21T14:30:00.000Z"

  • Timezone: Always UTC (Z suffix)

URLs

All URL fields must be valid URLs:

  • Format: Must include protocol (http:// or https://)

  • Example: "https://example.com"

  • Validation: Must pass URL validation

File Uploads

Banner image files have specific requirements:

  • Formats: PNG, JPEG, WebP

  • Size Limit: 2MB maximum

  • Processing: Automatically optimized via Cloudinary

  • Response: Returns processed image URL with dimensions

Model Relationships

User β†’ Blogs (One-to-Many)

  • Users can author multiple blog posts

  • Blog posts reference the author via author field

  • Deleting a user cascades to their blog posts

Blog β†’ Comments (One-to-Many)

  • Blog posts can have multiple comments

  • Comments reference the blog via blogId field

  • Deleting a blog cascades to its comments

User β†’ Comments (One-to-Many)

  • Users can create multiple comments

  • Comments reference the user via userId field

  • Deleting a user cascades to their comments

User β†’ Likes (Many-to-Many)

  • Users can like multiple blog posts

  • Blog posts can be liked by multiple users

  • Relationship tracked in separate likes collection

  • Aggregate counts stored in blog posts

Field Validation Summary

Field
Type
Required
Max Length
Format
Unique

User.username

string

βœ“

20

alphanumeric

βœ“

User.email

string

βœ“

50

email

βœ“

User.password

string

βœ“

-

min 8 chars

-

User.firstName

string

-

20

text

-

User.lastName

string

-

20

text

-

User.socialLinks.*

string

-

100

URL

-

Blog.title

string

βœ“

180

text

-

Blog.content

string

βœ“

-

HTML

-

Comment.content

string

βœ“

1000

text

-

TypeScript Definitions

For TypeScript applications, you can use these complete type definitions:

Last updated