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 managescreatedAtandupdatedAtautomatically.
How it works
The file follows a simple persistence flow:
- It creates a Mongoose
Schema. - It declares two required object reference fields:
listingIdanduserId. - It stores documents in the
listing_likescollection. - It adds a compound unique index across
listingIdanduserId. - It exports the compiled
ListingLikemodel 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
listingIdreferences theListingmodel throughref: "Listing".userIdreferences theUsermodel throughref: "User".- The schema stores documents in the
listing_likescollection. - The compound index applies to
listingIdanduserIdtogether.
Behaviors
mongoose.model("ListingLike", ListingLikeSchema)— creates and exports theListingLikemodel from the schema.ListingLikeSchema.index({ listingId: 1, userId: 1 }, { unique: true })— enforces one like per user per listing.timestamps: true— adds automaticcreatedAtandupdatedAtmanagement.
Gotchas & edge cases
[!WARNING] The unique compound index prevents duplicate records for the same
listingIdanduserIdpair.
[!IMPORTANT] Both
listingIdanduserIdare 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.
Related
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 bylistingIdUser— referenced byuserIdlisting_likes— collection name used for storage