Function getAllDocumentAndSendResponse

  • Throws

    error if mongoose error, in this case it will send response with status 500, and error message

    Example

    import { getAllDocumentAndSendResponse } from '@mdazad/common-utils';
    import express from 'express';
    import { User } from '../models/user';

    const router = express.Router();

    router.get("/", async (req, res) => {
    await getAllDocumentAndSendResponse(req, res, User);
    });

    Example

    if you want to populate fields in response

    import { getAllDocumentAndSendResponse } from '@mdazad/common-utils';
    import express from 'express';
    import { User } from '../models/user';

    const router = express.Router();

    router.get("/", async (req, res) => {
    await getAllDocumentAndSendResponse(req, res, User, [], ['posts']);
    });

    Example

    if you want to search in specific fields

    import { getAllDocumentAndSendResponse } from '@mdazad/common-utils';
    import express from 'express';
    import { User } from '../models/user';

    const router = express.Router();

    router.get("/", async (req, res) => {
    await getAllDocumentAndSendResponse(req, res, User, [], [], ['name', 'email']);
    });

    Example

    your custom query filter object, you can also modifiy the filter object based on your needs or user role etc

    import { getAllDocumentAndSendResponse } from '@mdazad/common-utils';
    import express from 'express';
    import { User } from '../models/user';

    const router = express.Router();

    router.get("/", async (req, res) => {

    const filter = {
    active: true,
    role: 'admin'
    };

    // in this case it will return all documents where active is true and role is admin, also it will use query object if passed in request
    await getAllDocumentAndSendResponse(req, res, User, filter);
    });

    Query

    pass query object, it will return documents where the field value is equal to the query value, you can pass multiple query objects

    make sure when you passing query object, use same field name as in your schema

    GET /api/users?key=value

    // multiple query objects
    GET /api/users?key=value&key2=value2

    pass inList query object, it will return documents where the field value is in the list

    GET /api/users?inList[email]=xyz@gmail.com,abc@gmail.com
    

    pass notInList query object, it will return documents where the field value is not in the list

    GET /api/users?notInList[email]=xyz@gmail,com,abc@gmail,com
    

    pass q query to search in all fields

    GET /api/users?q=xyz
    

    query with pagination, it will return 10 documents from page 1

    GET /api/users?page=1&limit=10
    

    query with sort DESC, it will sort the documents by createdAt field in descending order

    GET /api/users?sort=-createdAt
    

    query with sort ASC, it will sort the documents by createdAt field in ascending order

    GET /api/users?sort=createdAt
    

    query with fields, it will return only the selected fields

    GET /api/users?fields=name,email
    

    pass mongo compatible query object, when your query is not supported by the above query objects

    GET /api/users?mongoQuery={"active":{"$exists":true,"$ne":true}}
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

      express request

    • res: Response<any, Record<string, any>>

      express response

    • model: Model<Document<any, any, any>, {}, {}, {}, any>

      mongoose model

    • filter: any = {}

      query filter object

    • populateFields: never[] = []

      fields to populate

    • searchIn: never[] = []

      fields to search in for q query

    Returns Promise<void>

    Object in response like

    {
    "status": "success",
    "data": Array of documents,
    "pageInfo": {
    currentPage: // current page number,
    perPage: // per page document count,
    pageCount: // total page count,
    skipCount: // skip count,
    itemCount: // total document count,
    hasNextPage: // true if has next page,
    hasPreviousPage: // true if has previous page,
    }
    }

Generated using TypeDoc