1.0.0 • Published 2 years ago

crud-master v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Crud Controller

CrudController is a simple Node.js module that provides a base class for creating CRUD controllers with Express.js.

Usage

// index.js

const express = require('express');
const mongoose = require('mongoose');
const CrudMaster = require('crud-master');
const Task = require('./models/Task');

// Connect to MongoDB (replace with your actual MongoDB connection string)
mongoose.connect('mongodb://localhost:27017');

// Instantiate the CrudMaster for tasks
const TaskController = new CrudMaster(Task);

// Create an Express app and use the TaskController's router
const app = express();
app.use(express.json());
app.use('/tasks', TaskController.router);

// Start the Express server
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});
// Task schema
const mongoose = require('mongoose');

const taskSchema = new mongoose.Schema({
  title: { type: String, required: true },
  description: { type: String },
  completed: { type: Boolean, default: false },
});

const Task = mongoose.model('Task', taskSchema);

module.exports = Task;

API Reference

Get all Tasks

  GET /tasks/
ParameterTypeDescription
api_keystringRequired. Your API key

Get a task

  GET /tasks/${id}
ParameterTypeDescription
idstringRequired. Id of item to fetch

Create a task

  POST /tasks/
ParameterTypeDescription
idstringRequired. Id of item to fetch

Pass all parameters of the item to create to request.body.

Update a task

  PUT /tasks/${id}
ParameterTypeDescription
idstringRequired. Id of item to update

Pass all parameters of the item to update to request.body.

Delete a task

  DELETE /tasks/${id}
ParameterTypeDescription
idstringRequired. Id of item to delete