npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

my-controllers

v1.1.8

Published

my-controllers is a versatile Node.js package designed to streamline controller management in Express.js applications. It simplifies the integration of MongoDB through Mongoose, providing utility functions for CRUD operations and model creation. This pack

Downloads

32

Readme

my-controllers

my-controllers is a Node.js package that simplifies CRUD operations with MongoDB using Express.js and Mongoose. This package provides utility functions for managing RESTful APIs and Mongoose models.

Installation

To install my-controllers, run:

npm install my-controllers

Documentation

Controllers

The package provides a set of controller functions to manage CRUD operations:

  1. createPost(req, res, model)

    Creates a new document in the database using the specified model.

    Parameters:

    • req (Express Request): Request object containing data to be saved.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to create the document..

    Response:

    • 201 Created with the new document if successful.
    • 400 Bad Request if an error occurs.

    Example Usage:

    const express = require('express');
    const app = express();
    const { createPost } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.post('/posts', (req, res) => createPost(req, res, SampleModel));
  2. getAllController(req, res, model)

    Retrieves all documents from the database using the specified model.

    Parameters:

    • req (Express Request): Request object.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to retrieve documents.

    Response:

    • 200 OK with the list of documents if successful.
    • 400 Bad Request if there is an error or no documents are found.

    Example Usage:

    const express = require('express');
    const app = express();
    const { getAllController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.get('/posts', (req, res) => getAllController(req, res, SampleModel));
  3. getSingleController(req, res, model)

    Retrieves a single document by its ID.

    Parameters:

    • req (Express Request): Request object containing the document ID in req.params.id.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to retrieve the document.

    Response:

    • 200 OK with the document if found.
    • 404 Not Found if the document is not found.
    • 400 Bad Request if there is an error.

    Example Usage:

    const express = require('express');
    const app = express();
    const { getSingleController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.get('/posts/:id', (req, res) => getSingleController(req, res, SampleModel));
  4. updateController(req, res, model)

    Updates a document by its ID.

    Parameters:

    • req (Express Request): Request object containing the document ID in req.params.id and the update data in req.body.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to update the document.

    Response:

    • 200 OK with the updated document if successful.
    • 404 Not Found if the document is not found.
    • 400 Bad Request if there is an error.

    Example Usage:

    const express = require('express');
    const app = express();
    const { updateController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.put('/posts/:id', (req, res) => updateController(req, res, SampleModel));
  5. deleteController(req, res, model)

    Deletes a document by its ID.

    Parameters:

    • req (Express Request): Request object containing the document ID in req.params.id.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to delete the document.

    Response:

    • 200 OK if successful, with a confirmation message.
    • 404 Not Found if the document is not found.
    • 400 Bad Request if there is an error.

    Example Usage:

    const express = require('express');
    const app = express();
    const { deleteController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.delete('/posts/:id', (req, res) => deleteController(req, res, SampleModel));
  6. getByPropertyController(req, res, model, property, value)

    Retrieves documents by a specific property and value.

    Parameters:

    • req (Express Request): Request object containing the property and value in req.params.property and req.params.value.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to retrieve documents.
    • property (String): The property to filter by.
    • value (String): The value of the property to match.

    Response:

    • 200 OK with the list of documents if successful.
    • 400 Bad Request if there is an error or no documents are found.

    Example Usage:

    const express = require('express');
    const app = express();
    const { getByPropertyController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.get('/posts/:property/:value', (req, res) => getByPropertyController(req, res, SampleModel, req.params.property, req.params.value));
  7. searchController(req, res, model, searchTerm)

    Searches for documents based on a search term.

    Parameters:

    • req (Express Request): Request object containing the search term in req.params.term.
    • res (Express Response): Response object to send the result.
    • model (Mongoose Model): Mongoose model to search documents.
    • searchTerm (String): The term to search for.

    Response:

    • 200 OK with the list of matching documents if successful.
    • 400 Bad Request if there is an error or no documents are found.

    Example Usage:

    const express = require('express');
    const app = express();
    const { searchController } = require('my-controllers');
    const SampleModel = require('./models/sampleModel');
    
    app.get('/posts/search/:term', (req, res) => searchController(req, res, SampleModel, req.params.term));

Mongoose Integration

createModal(schema, collectionName)

Creates a Mongoose model based on the provided schema and collection name.

Parameters:

  • schema (Object): Schema definition for the model.
  • collectionName (String): The name of the collection in MongoDB.

Returns:

  • A Mongoose model.

Example Usage:

const mongoose = require('mongoose');
const { createModal } = require('my-controllers');

const userSchema = {
    name: { type: String, required: true },
    email: { type: String, required: true },
};

const UserModel = createModal(userSchema, 'User');

mongoConnecter(uri)

Connects to a MongoDB database using the provided URI.

Parameters:

  • uri (String): MongoDB connection URI.

Example Usage:

const { mongoConnecter } = require('my-controllers');

const uri = 'mongodb://localhost:27017/mydatabase';
mongoConnecter(uri);

Usage Example

Here is a simple Express.js setup using my-controllers:

const express = require('express');
const mongoose = require('mongoose');
const { createPost, getAllController, getSingleController, updateController, deleteController, getByPropertyController, searchController } = require('my-controllers');

const app = express();
app.use(express.json());

const userSchema = {
    name: { type: String, required: true },
    email: { type: String, required: true },
};
const UserModel = createModal(userSchema, 'User');

const uri = 'mongodb://localhost:27017/mydatabase';
mongoConnecter(uri);

app.post('/users', (req, res) => createPost(req, res, UserModel));
app.get('/users', (req, res) => getAllController(req, res, UserModel));
app.get('/users/:id', (req, res) => getSingleController(req, res, UserModel));
app.put('/users/:id', (req, res) => updateController(req, res, UserModel));
app.delete('/users/:id', (req, res) => deleteController(req, res, UserModel));
app.get('/users/:property/:

value', (req, res) => getByPropertyController(req, res, UserModel, req.params.property, req.params.value));
app.get('/users/search/:term', (req, res) => searchController(req, res, UserModel, req.params.term));

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});