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

monfact

v1.0.1

Published

MongoDb factory to translate API Queries to MongoDb

Downloads

4

Readme

MonFact

MongoDb factory to translate API Queries to MongoDb

Build Status

Monfact is an npm package to handle all mongodb queries on top of mongoose ORM In roder to generalize the targets.

Features

  • Easy to use.
  • Standard Error response.
  • Has most required operations on mongodb (CRUd, with a graphQl filter query).
  • You can append it to the mongoose model itself in order to be used as static methods or use the factory to custom queries.
  • Makes the code pretty clean, scalable and maintainble.
  • Injected to the mongoose Model interface so the intellisense will recognise it.

Installation

npm i monfact

Methods

For injected version:

export interface IMonFact {
  getOne(params: IObject<any>, select: string): Promise<any>;
  getAll(queryObj: IQuery): Promise<any>;
  deleteOne(params: IObject<any>, next: NextFunction): Promise<any>;
  updateOne(
    params: IObject<any>,
    update: IObject<any>,
    next: NextFunction
  ): Promise<any>;
  updateMany(
    params: IObject<any>,
    update: IObject<any>,
    next: NextFunction
  ): Promise<any>;
  createOne(doc: IObject<any>, next: NextFunction): Promise<any>;
  count(queryObj: IQuery): Promise<any>;
  getOneRes(): void;
  getAllRes(): void;
  deleteOneRes(): void;
  updateOneRes(): void;
  createOneRes(): void;
  getOnePopulatedRes(): void;
  countRes(): void;
}

**when using the factory or ResFactory version you must pass the mongoose model as the first paramater.

ResMethods

ResMethods usees (req.body, req.params, req.query);

  • getOneRes: uses req.params to filter
  • getAllRes: uses req.query to filter
  • deleteOneRes: uses req.params to filter
  • updateOneRes: uses req.params to filter and req.body to update
  • createOneRes: uses req.body to create
  • getOnePopulatedRes: uses req.params to filter but make sure that there is a filed "popFields" to be populated
  • countRes: uses req.query to filter

Query

{
  "page": 1, // optional default 1
  "limit": 10, // optional default 10
  "skip": 20, // optional default 0
  "sort": "name"| "-name"| "-name,age", // optional default '-createdAt'
  "fields": "name"| "name,_id", // optional default all fields
  "popFields": "products,favourites.meals", // optional default null
  'exclude': "excludeId", // optional default null
  .
  .
  //other fields on the object
  "name":"islam",
  "age":{gt:15, lt:20},
  "list":{push:"item1", pull:"item2"} // remove item2 and add item1
}

Usage

// there is two ways to use monfact
// first is to inject it to the mongoose model and use injected methods.

// in category.model.js
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { monfact } = require("monfact");

const CategorySchema = new Schema({
  name: { type: String, required: true },
  icon: { type: String, required: true },
  description: { type: String, required: true },
});

const CategoryModel = mongoose.model("Categories", CategorySchema);
// inject monfact to CategoryModel
monfact(CategoryModel);
// export CategoryModel
module.exports = CategoryModel;

// in category.controller.js
// you can use it inside a function like this
exports.getAllCategories = async (req, res, next) => {
  const categories = await CategoryModel.monfact.getAll({ name: "souly" });
  res.json({ status: "success", data: categories });
};

// or use the res version which uses the req.body & req.params & req.query
exports.getAllCategories = CategoryModel.monfact.getAllRes();

// then pass this function to the router
router.get("/", getAllCategories);

// the second method is to use the factory to build queries
// with res version
const { resFactory } = require("monfact");
exports.getAllCategories = resFactory.getAllRes(CategoryModel);

// plain version
exports.getAllCategories = async (req, res, next) => {
  const categories = await factory.getAll(CategoryModel, { name: "souly" });
  res.json({ status: "success", data: categories });
};

Response

The response is in standard format (status, message, errCode)

{
"status": "error",
"errCode": "err-code",
"message": "error - message"
}
{
"status": "success",
"data":  [] | {} | null
}

License

MIT

Feel free to fork it!