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 thisOther 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 postsIt 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 commentscalc()
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 productsAny 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