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
Maintainers
Keywords
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:
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));
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));
getSingleController(req, res, model)
Retrieves a single document by its ID.
Parameters:
req
(Express Request): Request object containing the document ID inreq.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));
updateController(req, res, model)
Updates a document by its ID.
Parameters:
req
(Express Request): Request object containing the document ID inreq.params.id
and the update data inreq.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));
deleteController(req, res, model)
Deletes a document by its ID.
Parameters:
req
(Express Request): Request object containing the document ID inreq.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));
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 inreq.params.property
andreq.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));
searchController(req, res, model, searchTerm)
Searches for documents based on a search term.
Parameters:
req
(Express Request): Request object containing the search term inreq.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');
});