A wropper around mongoose for fetching related data
npm install mongoose-clampjs
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
`js
//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...
`
$3
All the other mongodb method is on the property model
`js
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()
`js
await User.find().get()
`
##### findById()
`js
await User.findById(id).get()
//
await User.findById([id1, id2]).get()
`
##### findOne()
`js
await User.findOne().get()
`
##### pagination
`js
await User.find().page(3).perpage(12).get()
`
The above code will return something like this
`js
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
`js
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)
`js
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.
`js
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)
`js
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.
`js
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(), ...
`js
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(), ...
`js
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.
`js
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.
`js
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(), ...
`js
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(), ...
`js
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.
`js
await Comments.findById(id).belongsTo(Post, where = {}, select = {}, limit = null, skip = 0, sort = {}).get()
// Gets all the Comments belonging of the post
``