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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-clamp

v1.0.1

Published

A wropper around mongoose for fetching related data

Readme

Mongoose Clamp

Mongoose clamp wraps around mongoose to provide additional functionly like fetching related data from the mongodb database easily.

Usage

  • Defind the schema.
const mongoose = require('mongoose')
const mongooseClamp = require('mongoose-clamp')
// 
const postSchema = new mongoose.Schema({
    post:{
        type:String,
        required:true,
        max:300,
    },
    user_id:{
        type:String,
        required:true,
    }
}, {timestamps:true}) 

let Post = mongooseClamp("Post", postSchema)
  • Then fetch the data from the database
//All the Post
await Post.find().get()

//All the post together with their comments 
await Post.find().withAll(Comments).get()

//All the post with pagination together with their comments 
await Post.find().page(1).perpage(20).withAll(Comments).get()

//all the post attaching the user who posted them
await Post.find().attach(User).get()

//update
await Post.update(Id, {name:'tobby'})

//delete
await Post.delete(Id)

//And many more...

Accessing all the other mongodb methods

All the other mongodb method is on the property model

await User.model.find()
await User.model.update()
await User.model.delete()

Some of the methods that we have additionaly are;

finding data

Both find() and findById()

find()
await User.find().get()
findById()
await User.findById(id).get()
// 
await User.findById([id1, id2]).get()
findOne()
await User.findOne().get()
pagination
await User.find().page(3).perpage(12).get()

The above code will return something like this

await User.find().page(3).perpage(12).get()
// pagination
// This will return something like this

Other methodes that can be chained on the above methods. All these methods will only work where the foriegn key in the passed collection is named as user_id from model User or post_id from Model Post. When the id is comming from Model Post, any collection that will keep the id from Post should keep it as post_id

- withAll()

Appends all the data from the Passed Model that has the id of the Model being called for each data that meets the criteria. Good for one to many relationship

await User.findById(id).withAll(Post).get()
await User.findOne().withAll(Post).get()
await User.find().withAll(Post).get()
//Gets the user and all of their posts

It also takes in filter, select, sort and limit. withAll(Post, filter, select, sort, limit, skip)

await User.find().withAll(Post, filter, select, sort, limit, skip).get()
// 
await User.find().withAll(Post, {}, {}, {}, 20, 10).get()
// 
// pagination
await Post.find().page(2).perpage(15).withAll(User).get()
- withOne()

Appends one data from the Passed Model that has the id of the Model being called for each data that meets the criteria. Good for one to one relationship.

await User.findById(id).withOne(Post).get()
await User.findOne().withOne(Post).get()
await User.find().withOne(Post).get()
//Gets the user and one of their posts

// pagination
await Post.find().page(2).perpage(15).withOne(User).get()

It also takes in filter, select, sort and limit. .withOne(Post, filter, select, sort, limit, skip)

await User.find().withOne(Post, {}, {}, {}, 10, 5).get()
- attach()

Appends one data from the passed Model id in the called model for each data that meets the criteria. Good for both one to one and one to many relationship.

await Post.findById(id).attach(User).get()
await Post.findOne({}).attach(User).get()
// pagination
await Post.find().page(2).perpage(15).attach(User).get()
- with()

Returns all the the documents in the first Model whose ids exist in the second table. You can chain page(), limit(), attach(), ...

await Post.with(Comments, where = {}, select = {}).get()
// Returns all the posts having comments
- without()

Returns all the the documents in the first Model whose ids do not exist in the second table. You can chain page(), limit(), attach(), ...

await Post.without(Comments, where = {}, select = {}).get()
// Returns all the posts not having comments

calc()

Using with, without, withLeast and withMost fatches data from one collection basing on the other collection. By defalult, it counts the number of occurence of the id in the other table. By using calc() method you can pass a calculation on to which the result to rely.

    await Product.withLeast(Order)
    .calc({$multiply:["$qty", "$price"]})
    .get()
    // qty and price are fields in the record which will be multiplied. Then, the values will be compared before returning the products

    await User.withLeast(Order)
    .calc({$sum:{$multiply:["$qty", "$price"]}})
    .get()
    // qty and price are fields in the record which will be multiplied. Then, the sum of each values will be compared before returning the products

Any mongodb formular can be used

inOrder()

This will return the resuslt in ascending or descending order basing on what the results from onother table. This only works when using with, without, withLeast and withMost. It takes in one aguement , 1 for ascending and -1 for descending order.

    await User.withLeast(Order)
    .inOrder(-1)
    .get()

    // 
    await User.withLeast(Order)
    .calc({$sum:{$multiply:["$qty", "$price"]}})
    .inOrder(-1)
    .get()

Relationshop

- hasOne()

Used in one to one relationship. Returns one record that is the status of the Post. You can chain attach(),withAll(), withOne(), ...

await Post.findById(id).hasOne(Status, where = {}, select = {}).get()
// Status of the post
- hasMany()

Used in one to many relationship. Returns all the record that is the status of the Post. You can chain page(), limit(), attach(), ...

await Post.findById(id).hasMany(Comments, where = {}, select = {}, limit = null, skip = 0, sort = {}).get()
// Gets all the Comments belonging of the post
- belongsTo()

Used in one to one or one to many relationship. Returns one record from the second collection whose id exist in the first collection.

await Comments.findById(id).belongsTo(Post, where = {}, select = {}, limit = null, skip = 0, sort = {}).get()
// Gets all the Comments belonging of the post