ListingLike

Overview

services/api/src/models/ListingLike.js defines the Mongoose model for storing relationships between a Listing and a User. It represents a like record in the listing_likes collection and uses MongoDB object references for both sides of the relationship.

This model is intentionally small and focused. It provides the persistence shape for listing-like data, along with a uniqueness rule that prevents duplicate likes from the same user on the same listing.

[!NOTE] The schema uses timestamps: true, so Mongoose manages createdAt and updatedAt automatically.

How it works

The file follows a simple persistence flow:

  • It creates a Mongoose Schema.
  • It declares two required object reference fields: listingId and userId.
  • It stores documents in the listing_likes collection.
  • It adds a compound unique index across listingId and userId.
  • It exports the compiled ListingLike model for use elsewhere in the API.
flowchart TD
    A[services api src models ListingLike js] --> B[ListingLikeSchema]
    B --> C[listingId ref Listing required]
    B --> D[userId ref User required]
    B --> E[timestamps enabled]
    B --> F[collection listing_likes]
    B --> G[unique index listingId userId]
    G --> H[ListingLike model]

The main responsibility is data integrity. The schema ensures each like is tied to one listing and one user, and the index keeps that relationship unique.

Key components

{
    "title": "ListingLikeSchema",
    "content": "Mongoose schema that defines the structure of a listing like record, including required listing and user references, timestamps, and the target collection.",
    "type": "class",
    "filePath": "services/api/src/models/ListingLike.js",
    "badges": [
        "public"
    ]
}
{
    "title": "ListingLike",
    "content": "Compiled Mongoose model exported from the schema. It is the main entry point for creating, reading, and enforcing constraints on listing like documents.",
    "type": "class",
    "filePath": "services/api/src/models/ListingLike.js",
    "badges": [
        "public"
    ]
}

Data & fields

[
    {
        "name": "listingId",
        "type": "Schema.Types.ObjectId",
        "required": true,
        "description": "Reference to a `Listing` document."
    },
    {
        "name": "userId",
        "type": "Schema.Types.ObjectId",
        "required": true,
        "description": "Reference to a `User` document."
    },
    {
        "name": "createdAt",
        "type": "Date",
        "description": "Automatically added by Mongoose because timestamps are enabled."
    },
    {
        "name": "updatedAt",
        "type": "Date",
        "description": "Automatically added by Mongoose because timestamps are enabled."
    }
]

Field relationships

  • listingId references the Listing model through ref: "Listing".
  • userId references the User model through ref: "User".
  • The schema stores documents in the listing_likes collection.
  • The compound index applies to listingId and userId together.

Behaviors

  • mongoose.model("ListingLike", ListingLikeSchema) — creates and exports the ListingLike model from the schema.
  • ListingLikeSchema.index({ listingId: 1, userId: 1 }, { unique: true }) — enforces one like per user per listing.
  • timestamps: true — adds automatic createdAt and updatedAt management.

Gotchas & edge cases

[!WARNING] The unique compound index prevents duplicate records for the same listingId and userId pair.

[!IMPORTANT] Both listingId and userId are required. Documents without either field do not satisfy the schema.

[!NOTE] The schema does not define extra metadata fields. It only stores the relationship between a listing and a user.

This model is directly connected to the Listing and User data models through Mongoose references. It is part of the API persistence layer and uses MongoDB collection storage through Mongoose.

  • Listing — referenced by listingId
  • User — referenced by userId
  • listing_likes — collection name used for storage