estol-comments
is a Node.js dependency for managing comments-related operations.
Installation
To use estol-comments
in your Node.js project, you can install it using npm:
npm install estol-comments
Usage
Importing and initializing
import commentController, { configureCommentsDependency } from "estol-comments";
configureBlogDependency({
mongoose: "your-mongoose-instance",
model: YourCommentModel,
});
const { getComments, commentManagement } = estolCommentController;
Usage example
const comments = await getComments({ fieldsToPopulate: ["author", "postID", "parentCommentID"], quantity: 5, postID: postID });
console.log(comments);
{
success: false,
message: "",
code: 500,
error: error,
}
{
success: true,
comments: [],
newFalg: newFlag,
code: 200,
}
Post management
const newCommentData = {
author:
content: "New comment content",
postID: postID,
parentCommentID: parentCommentID,
};
const createdComment = await commentManagement({ action: 'create', fieldsToPopulate: ["author", "parentCommentID", "postID"], ...newCommentData });
console.log(createdComment);
{
success: false,
message: "",
code: 500,
error: error,
}
{
success: true,
createdCinnebt: {},
code: 201,
}
Note: Please replace // Should be the ObjectId of the author in the User model
with the actual ObjectId of the author in your User model. This adjustment ensures that the "author" field is correctly represented as an ObjectId, maintaining the integrity of the data model.
Configuration
estol-comments uses Mongoose for database interactions. Make sure to have Mongoose installed and configured in your project.
API Reference
Configures the estol-comments
dependency with the provided configuration.
Note: The user must provide either mongoose
or model
, not both. If mongoose
instance is provided, the dependency connects to the database using its default model. If model
is provided, the dependency uses the user's Mongoose model. It's mandatory to provide one of the two options; providing both is not allowed.
Important: If the user decides to provide a model, it is important that this model has at least the same fields in the default model(author, content, likes, postID, replies, parentCommentID and timestamps) to avoid dependency's malfunctions.
Default Model
If the user chooses to use the default model provided by the estol-comments
dependency, the model looks like this:
import { Schema, model } from "mongoose";
const { ObjectId } = Schema.Types;
const commentSchema = new Schema(
{
author: { type: ObjectId, rel: "User", required: true },
content: { type: String, required: true },
likes: { type: Number, default: 0 },
postID: { type: ObjectId, rel: "Post", required: true },
replies: [{ type: ObjectId, rel: "Comment" }],
parentCommentID: { type: ObjectId, rel: "Comment", default: null },
},
{
timestamps: true,
}
);
const Comment = model("Comment", commentSchema);
Important: If you opt to use the default model, it's crucial to ensure that your project includes a Mongoose models for "User" and "Post" to prevent errors. The "author" field in the default comment model references the "User" model, and the "postID" field references the "Post" model. If the "User" or "Post" models is not defined in your project, it may lead to reference errors. Please make sure that the "User" and "Post" model are availables in your project to maintain the integrity of the default comment model.
A set of functions for managing comment-related operations.
Retrieve comments based on specified options.
Options
The options
object can include several properties:
-
fieldsToPopulate
: Should be an array of strings containing the names of all the fields of the model to be populated.
-
useLean
: An optional boolean property. By default, this field is set to true
unless the dependency user needs to receive the complete model instance. In that case, the user should set this property as useLean: false
, and the dependency will return a complete model instance instead of a plain JavaScript object.
-
flag
: An optional string property representing a date to facilitate pagination. When requesting the first page, this property is not necessary to include. The dependency will include the flag for the next page in the returned object after the first page. The user only needs to include this flag in the next request.
-
quantity
: An optional property determining the extent of each page. If not provided, by default, each call will return a maximum of 15 comments.
-
postID
: A mandatory property. This property is used by the dependency to perform the search of the post's comments on database.
Perform comment management actions like creating, updating, or deleting.
Options
The options
object can include several properties:
-
action
: A mandatory string property that can have a value of "create"
, "update"
, or "delete"
, depending on the action to be carried out.
-
fieldsToPopulate
: Should be an array of strings containing the names of all the fields of the model to be populated.
-
author
: A mandatory property when the action
property is set to "create"
. The author
property will be an ObjectId from de User model.
-
content
: A mandatory property in case the action
property is set to "create"
or "update"
. The content
property will be a string containing the text content for the comment to be created or updated.
-
commentID
: A mandatory property in case the action
property is set to "update"
or "delete"
. This property is used by the dependency to identify the comment in the database that needs to be updated or deleted.
-
parentCommentID
: An optional property in case the action
property is set to "create"
and the new comment is a response to another comment. The parentCommentID
property will be an ObjectId from the Comment model.
-
postID
: A mandatory property in case the action
property is set to "create"
. The postID
property will be an ObjectId form de Post model.
-
useLean
: An optional boolean property. By default, this field is set to true
unless the dependency user needs to receive the complete model instance. In that case, the user should set this property as useLean: false
, and the dependency will return a complete model instance instead of a plain JavaScript object.
Note: Ensure consistency in the naming convention of properties, using camelCase
throughout the options
object.
Licence
This project is licensed under the MIT License - see the LICENSE.md file for details.
Make sure to replace placeholders like `