Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

estol-blog

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

estol-blog

Blog's post management dependency

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

estol-blog

estol-blog is a Node.js dependency for managing blog-related operations.

Installation

To use estol-blog in your Node.js project, you can install it using npm:

npm install estol-blog

Usage

Importing and initializing

import { configureBlogDependency, blogController } from 'estol-blog';

// Configure the dependency with your database URL
configureBlogDependency({
  databaseURL: 'your-database-url',
  model: YourPostModel, // Replace YourPostModel with your Mongoose model
});

// Access blogController for various blog management operations
const { getPosts, postManagement } = blogController;

Usage example

Getting posts
const posts = await getPosts({ searchType: 'Last', model: YourPostModel, quantity: 5 });
console.log(posts);

Post management
const newPostData = {
  author: 'John Doe',
  title: 'New Blog Post',
  content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
};

const createPostResult = await postManagement({ action: 'create', model: YourPostModel, newPost: newPostData });
console.log(createPostResult);

Configuration

estol-blog uses Mongoose for database interactions. Make sure to have Mongoose installed and configured in your project.

API Reference

configureBlogDependency(config)

Configures the estol-blog dependency with the provided configuration.

  • config: An object containing the configuration options. It should have either a databaseURL or a model, but not both.

    • databaseURL: The URL of the database to connect to. If provided, the dependency will use its own default model for blog posts.

    • model: The Mongoose model instance from the user's project. If provided, the dependency will use the user's Mongoose model for blog posts.

Note: The user must provide either databaseURL or model, not both. If databaseURL 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, title, content y views) to avoid dependency's malfunctions.

Default Model

If the user chooses to use the default model provided by the estol-blog dependency, the model looks like this:

// post.model.js
import { Schema, model } from "mongoose";
const { ObjectId } = Schema.Types;

const postSchema = new Schema(
   {
      author: { type: ObjectId, ref: "User", required: true },
      title: { type: String, required: true },
      content: { type: String, required: true },
      views: { type: Number, default: 0 },
   },
   {
      timestamps: true,
   }
);

const Post = model("Post", postSchema);

export default Post;

Important: If you opt to use the default model, it's crucial to ensure that your project includes a Mongoose model for "User" to prevent errors. The "author" field in the default blog post model references the "User" model. If the "User" model is not defined in your project, it may lead to reference errors. Please make sure that the "User" model is available in your project to maintain the integrity of the default blog post model.

blogController

A set of functions for managing blog-related operations.

getPosts(options)

Retrieve blog posts based on specified options.

Options

The options object can include several properties:

  • searchType: Should be a string referring to the type of search to be performed. The available options are:

    • "All": Retrieve all the posts stored in the blog.
    • "Last": Retrieve posts in chronological order from the newest to the oldest.
    • "Trending": Retrieve posts ordered by views, from most viewed to least viewed.
    • "One": Retrieve a specific post by providing a postID.
    • "Search": Search for posts by providing a search string (searchStr).
  • 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 (e.g., using "searchType: 'Last'"), 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 10 posts.

  • id: A mandatory property only in the case of requesting a specific post. This property is used by the dependency to perform a search in the database.

  • searchStr: A mandatory property only in the case of wanting to perform a search for posts using a search string. In this case, the dependency will search for all occurrences of that search string in the title and content of all blogs in the database.

Note: Ensure consistency in the naming convention of properties, using camelCase throughout the options object.

postManagement(options)

Perform blog post management actions like creating, updating, or deleting posts.

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.

  • newPost: A mandatory property when the action property is set to "create". The newPost property will be an object containing all the information for the new post (author, title, and content in case the dependency's default model is used).

  • updateData: A mandatory property in case the action property is set to "update". The updateData property will be an object containing the information to be updated in the post. In this case, it is not necessary to provide all the properties, only those to be updated.

  • id: A mandatory property in case the action property is set to "update" or "delete". This property is used by the dependency to identify the post in the database that needs to be updated or deleted.

  • 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 `'your-database-url'`, `YourPostModel`, and adjust the code snippets based on your actual implementation and use case.

Keywords

FAQs

Package last updated on 08 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc