feat: migrate Post and Comment models from MongoDB to CouchDB
- Replace mongoose Post model with CouchDB-based class using couchdbService - Replace mongoose Comment model with CouchDB-based class using couchdbService - Update posts route to use new CouchDB models with embedded user data - Update comments route to use new CouchDB models with embedded user/post data - Maintain all existing API endpoints and functionality - Add like/unlike functionality for posts - Handle image uploads with Cloudinary integration - Preserve Socket.IO events for real-time updates - Use denormalized structure for better performance with embedded data 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
@@ -1,32 +1,162 @@
|
||||
const mongoose = require("mongoose");
|
||||
const couchdbService = require("../services/couchdbService");
|
||||
|
||||
const CommentSchema = new mongoose.Schema(
|
||||
{
|
||||
user: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
post: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "Post",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
maxlength: 500,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
class Comment {
|
||||
static async create(commentData) {
|
||||
const { user, post, content } = commentData;
|
||||
|
||||
// Get user data for embedding
|
||||
const userDoc = await couchdbService.findUserById(user);
|
||||
if (!userDoc) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
// Compound index for efficient querying of comments by post
|
||||
CommentSchema.index({ post: 1, createdAt: -1 });
|
||||
// Get post data for embedding
|
||||
const postDoc = await couchdbService.getById(post);
|
||||
if (!postDoc) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("Comment", CommentSchema);
|
||||
const comment = {
|
||||
_id: `comment_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
type: "comment",
|
||||
user: {
|
||||
userId: user,
|
||||
name: userDoc.name,
|
||||
profilePicture: userDoc.profilePicture || ""
|
||||
},
|
||||
post: {
|
||||
postId: post,
|
||||
content: postDoc.content,
|
||||
userId: postDoc.user.userId
|
||||
},
|
||||
content: content.trim(),
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
const createdComment = await couchdbService.create(comment);
|
||||
|
||||
// Update post's comment count
|
||||
await couchdbService.updatePost(post, {
|
||||
commentsCount: (postDoc.commentsCount || 0) + 1
|
||||
});
|
||||
|
||||
return createdComment;
|
||||
}
|
||||
|
||||
static async findById(commentId) {
|
||||
return await couchdbService.getById(commentId);
|
||||
}
|
||||
|
||||
static async find(query = {}) {
|
||||
const couchQuery = {
|
||||
selector: {
|
||||
type: "comment",
|
||||
...query
|
||||
}
|
||||
};
|
||||
return await couchdbService.find(couchQuery);
|
||||
}
|
||||
|
||||
static async findByPostId(postId, options = {}) {
|
||||
const { skip = 0, limit = 50, sort = { createdAt: -1 } } = options;
|
||||
|
||||
const query = {
|
||||
selector: {
|
||||
type: "comment",
|
||||
"post.postId": postId
|
||||
},
|
||||
sort: Object.keys(sort).map(key => [key, sort[key] === -1 ? "desc" : "asc"]),
|
||||
skip,
|
||||
limit
|
||||
};
|
||||
|
||||
return await couchdbService.find(query);
|
||||
}
|
||||
|
||||
static async countDocuments(query = {}) {
|
||||
const couchQuery = {
|
||||
selector: {
|
||||
type: "comment",
|
||||
...query
|
||||
},
|
||||
fields: ["_id"]
|
||||
};
|
||||
const docs = await couchdbService.find(couchQuery);
|
||||
return docs.length;
|
||||
}
|
||||
|
||||
static async deleteComment(commentId) {
|
||||
const comment = await couchdbService.getById(commentId);
|
||||
if (!comment) {
|
||||
throw new Error("Comment not found");
|
||||
}
|
||||
|
||||
// Update post's comment count
|
||||
if (comment.post && comment.post.postId) {
|
||||
const postDoc = await couchdbService.getById(comment.post.postId);
|
||||
if (postDoc) {
|
||||
await couchdbService.updatePost(comment.post.postId, {
|
||||
commentsCount: Math.max(0, (postDoc.commentsCount || 0) - 1)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return await couchdbService.delete(commentId);
|
||||
}
|
||||
|
||||
static async findByUserId(userId, options = {}) {
|
||||
const { skip = 0, limit = 50 } = options;
|
||||
|
||||
const query = {
|
||||
selector: {
|
||||
type: "comment",
|
||||
"user.userId": userId
|
||||
},
|
||||
sort: [["createdAt", "desc"]],
|
||||
skip,
|
||||
limit
|
||||
};
|
||||
|
||||
return await couchdbService.find(query);
|
||||
}
|
||||
|
||||
static async validateContent(content) {
|
||||
if (!content || content.trim().length === 0) {
|
||||
throw new Error("Comment content is required");
|
||||
}
|
||||
|
||||
if (content.length > 500) {
|
||||
throw new Error("Comment content must be 500 characters or less");
|
||||
}
|
||||
|
||||
return content.trim();
|
||||
}
|
||||
|
||||
// Legacy compatibility methods for mongoose-like interface
|
||||
static async populate(comments, fields) {
|
||||
// In CouchDB, user and post data are already embedded in comments
|
||||
// This method is for compatibility with existing code
|
||||
if (fields) {
|
||||
if (fields.includes("user") || fields.includes("post")) {
|
||||
// Data is already embedded, so just return comments as-is
|
||||
return comments;
|
||||
}
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
|
||||
// Helper method to check if comment belongs to a post
|
||||
static async belongsToPost(commentId, postId) {
|
||||
const comment = await couchdbService.getById(commentId);
|
||||
return comment && comment.post && comment.post.postId === postId;
|
||||
}
|
||||
|
||||
// Helper method to check if user owns comment
|
||||
static async isOwnedByUser(commentId, userId) {
|
||||
const comment = await couchdbService.getById(commentId);
|
||||
return comment && comment.user && comment.user.userId === userId;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Comment;
|
||||
@@ -1,62 +1,194 @@
|
||||
const mongoose = require("mongoose");
|
||||
const couchdbService = require("../services/couchdbService");
|
||||
|
||||
const PostSchema = new mongoose.Schema(
|
||||
{
|
||||
user: {
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
required: true,
|
||||
index: true,
|
||||
},
|
||||
content: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
imageUrl: {
|
||||
type: String,
|
||||
},
|
||||
cloudinaryPublicId: {
|
||||
type: String,
|
||||
},
|
||||
likes: [
|
||||
{
|
||||
type: mongoose.Schema.Types.ObjectId,
|
||||
ref: "User",
|
||||
class Post {
|
||||
static async create(postData) {
|
||||
const { user, content, imageUrl, cloudinaryPublicId } = postData;
|
||||
|
||||
// Get user data for embedding
|
||||
const userDoc = await couchdbService.findUserById(user);
|
||||
if (!userDoc) {
|
||||
throw new Error("User not found");
|
||||
}
|
||||
|
||||
const post = {
|
||||
_id: `post_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
type: "post",
|
||||
user: {
|
||||
userId: user,
|
||||
name: userDoc.name,
|
||||
profilePicture: userDoc.profilePicture || ""
|
||||
},
|
||||
],
|
||||
commentsCount: {
|
||||
type: Number,
|
||||
default: 0,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
},
|
||||
);
|
||||
content,
|
||||
imageUrl,
|
||||
cloudinaryPublicId,
|
||||
likes: [],
|
||||
likesCount: 0,
|
||||
commentsCount: 0,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
// Index for querying posts by creation date
|
||||
PostSchema.index({ createdAt: -1 });
|
||||
const createdPost = await couchdbService.create(post);
|
||||
|
||||
// Update user relationship when post is created
|
||||
PostSchema.post("save", async function (doc) {
|
||||
const User = mongoose.model("User");
|
||||
// Update user's posts array
|
||||
userDoc.posts.push(createdPost._id);
|
||||
userDoc.stats.postsCreated = userDoc.posts.length;
|
||||
await couchdbService.update(user, userDoc);
|
||||
|
||||
// Add post to user's posts if not already there
|
||||
await User.updateOne(
|
||||
{ _id: doc.user },
|
||||
{ $addToSet: { posts: doc._id } }
|
||||
);
|
||||
});
|
||||
return createdPost;
|
||||
}
|
||||
|
||||
// Cascade cleanup when a post is deleted
|
||||
PostSchema.pre("deleteOne", { document: true, query: false }, async function () {
|
||||
const User = mongoose.model("User");
|
||||
static async findById(postId) {
|
||||
return await couchdbService.getById(postId);
|
||||
}
|
||||
|
||||
// Remove post from user's posts
|
||||
await User.updateOne(
|
||||
{ _id: this.user },
|
||||
{ $pull: { posts: this._id } }
|
||||
);
|
||||
});
|
||||
static async find(query = {}) {
|
||||
const couchQuery = {
|
||||
selector: {
|
||||
type: "post",
|
||||
...query
|
||||
}
|
||||
};
|
||||
return await couchdbService.find(couchQuery);
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("Post", PostSchema);
|
||||
static async findAll(options = {}) {
|
||||
const { skip = 0, limit = 20, sort = { createdAt: -1 } } = options;
|
||||
|
||||
const query = {
|
||||
selector: { type: "post" },
|
||||
sort: Object.keys(sort).map(key => [key, sort[key] === -1 ? "desc" : "asc"]),
|
||||
skip,
|
||||
limit
|
||||
};
|
||||
|
||||
return await couchdbService.find(query);
|
||||
}
|
||||
|
||||
static async countDocuments() {
|
||||
const query = {
|
||||
selector: { type: "post" },
|
||||
fields: ["_id"]
|
||||
};
|
||||
const docs = await couchdbService.find(query);
|
||||
return docs.length;
|
||||
}
|
||||
|
||||
static async updatePost(postId, updateData) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
const updatedPost = {
|
||||
...post,
|
||||
...updateData,
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
|
||||
return await couchdbService.update(postId, updatedPost);
|
||||
}
|
||||
|
||||
static async deletePost(postId) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
// Remove post from user's posts array
|
||||
if (post.user && post.user.userId) {
|
||||
const userDoc = await couchdbService.findUserById(post.user.userId);
|
||||
if (userDoc) {
|
||||
userDoc.posts = userDoc.posts.filter(id => id !== postId);
|
||||
userDoc.stats.postsCreated = userDoc.posts.length;
|
||||
await couchdbService.update(post.user.userId, userDoc);
|
||||
}
|
||||
}
|
||||
|
||||
return await couchdbService.delete(postId);
|
||||
}
|
||||
|
||||
static async addLike(postId, userId) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
if (!post.likes.includes(userId)) {
|
||||
post.likes.push(userId);
|
||||
post.likesCount = post.likes.length;
|
||||
post.updatedAt = new Date().toISOString();
|
||||
await couchdbService.update(postId, post);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
static async removeLike(postId, userId) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
const likeIndex = post.likes.indexOf(userId);
|
||||
if (likeIndex > -1) {
|
||||
post.likes.splice(likeIndex, 1);
|
||||
post.likesCount = post.likes.length;
|
||||
post.updatedAt = new Date().toISOString();
|
||||
await couchdbService.update(postId, post);
|
||||
}
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
static async incrementCommentsCount(postId) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
post.commentsCount = (post.commentsCount || 0) + 1;
|
||||
post.updatedAt = new Date().toISOString();
|
||||
return await couchdbService.update(postId, post);
|
||||
}
|
||||
|
||||
static async decrementCommentsCount(postId) {
|
||||
const post = await couchdbService.getById(postId);
|
||||
if (!post) {
|
||||
throw new Error("Post not found");
|
||||
}
|
||||
|
||||
post.commentsCount = Math.max(0, (post.commentsCount || 0) - 1);
|
||||
post.updatedAt = new Date().toISOString();
|
||||
return await couchdbService.update(postId, post);
|
||||
}
|
||||
|
||||
static async findByUserId(userId, options = {}) {
|
||||
const { skip = 0, limit = 20 } = options;
|
||||
|
||||
const query = {
|
||||
selector: {
|
||||
type: "post",
|
||||
"user.userId": userId
|
||||
},
|
||||
sort: [["createdAt", "desc"]],
|
||||
skip,
|
||||
limit
|
||||
};
|
||||
|
||||
return await couchdbService.find(query);
|
||||
}
|
||||
|
||||
// Legacy compatibility methods for mongoose-like interface
|
||||
static async populate(posts, fields) {
|
||||
// In CouchDB, user data is already embedded in posts
|
||||
// This method is for compatibility with existing code
|
||||
if (fields && fields.includes("user")) {
|
||||
// User data is already embedded, so just return posts as-is
|
||||
return posts;
|
||||
}
|
||||
return posts;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Post;
|
||||
Reference in New Issue
Block a user