1.1.9 • Published 11 months ago

express-controller-sets v1.1.9

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

controllerSets

controllerSets is a automate tool that help backend developers to create Rest api using express.js and mongoose. Don't need to write same code again and again, use controllerSets to boost your productivity.

Update logs:

  • create crud api by create an object -> don't need to write same code again and again
  • single field file upload -> post and put or patch
  • multi-field file upload -> post and put or patch
  • file Serving -> serve file from uploads folders

Documentation

The official documentation website is controllerSets.

controllerSets 1.0.0 was released on May 2023. You can find more details on backwards breaking changes in 1.0.0 on our docs site.

Support

Installation

First install Node.js and mongoose. Then:

$ npm i express-controller-sets

Importing

// Using ES6 imports
import { ControllerSets, FileUploaderControllerSets } from "express-controller-sets";

Defining an class to create rest api without file upload

import { ControllerSets } from "express-controller-sets";
import taskModel from "../models/taskModels.js";

// Create an instance of the controller
// ControllerSets(Mongoose Model, sorting Field, filters field)
const taskController = new ControllerSets(taskModel, "-_id", [
  "email",
  "status",
]);
export { taskController };

Defining routers

// imports
import { Router } from "express";
import { taskController } from "../controllers/taskClassController.js";

// routers
const router = Router();

// task routes
router.get("/", taskController.getAll.bind(taskController));
router.get("/:id", taskController.getById.bind(taskController));
router.post("/", taskController.create.bind(taskController));
router.put("/:id", taskController.update.bind(taskController));
router.delete("/:id", taskController.delete.bind(taskController));

// exports
export default router;

Define base path

// base path is the main parent folder where your files will be uploaded
// how to create base path --->
// create settings.js
import path from "path";
import { fileURLToPath } from "url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const basePath = path.resolve(__dirname);

export { basePath };

Single files uploading

import {ControllerSets,FileUploaderControllerSets} from "express-controller-sets";
import taskModel from "../models/taskModels.js";
import { basePath } from "../settings.js";

// (Mongoose Model, Upload options, basePath)
// in file upload we have only one method create
const uploadFile = new FileUploaderControllerSets(
  taskModel,
  {
    folder: "uploads/user/images/",
    fileField: "document",
  },
  basePath
);

export { taskController, uploadFile };

Single file uploading routing

// imports
import {taskController,uploadFile} from "../controllers/taskClassController.js";

router.post("/", uploadFile.fileUpload.bind(uploadFile));
router.put("/:id", uploadFile.updateFileUpload.bind(uploadFile));

// exports
export default router;

Multiple files uploading

// multi fields files upload -> create a list of fields name array 
// (Mongoose Model, Upload options, basePath)
const uploadFile = new FileUploaderControllerSets(
  taskModel,
  {
    folder: "uploads/user/files/",
    fileField: "document",

    // multi fields upload options -> {name: field name, maxCount: int}
    multiFields: [
      { name: "profile_img", maxCount: 1 },
      { name: "nid", maxCount: 1 },
      { name: "birth_certificate", maxCount: 1 },
    ],
  },
  basePath
);

// multi files upload routes 
router.post("/", uploadFile.multiFileUpload.bind(uploadFile));
router.put("/:id", uploadFile.updateMultiFileUpload.bind(uploadFile));

serve files

import { basePath } from "../settings.js";
import { FileServe } from "express-controller-sets";

// path -> parent -> child -> sub-child
const paths = ["uploads", "demo", "images"];
// (file path array, base path)
const filesController = new FileServe(paths, basePath);
export { filesController };

// routes
import { filesController } from "./controllers/taskClassController.js";

// use app or router ->
// don't rename params -> :fileName
app.get("/files/:fileName", filesController.serve.bind(filesController));

License

Copyright (c) 2023 LearnBoost <https://sabbirmahmud.com/>;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.1.9

11 months ago

1.1.7

11 months ago

1.1.5

11 months ago

1.1.3

11 months ago

1.1.1

11 months ago

1.0.21

11 months ago

1.0.19

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.13

12 months ago

1.0.12

12 months ago

1.0.11

12 months ago

1.0.10

12 months ago

1.0.9

12 months ago

1.0.8

12 months ago

1.0.7

12 months ago

1.0.6

12 months ago

1.0.5

12 months ago

1.0.4

12 months ago

1.0.3

12 months ago

1.0.2

12 months ago

1.0.1

12 months ago

1.0.0

12 months ago