@corluk/mongo-utils v1.4.4
Ligthweight Utils for MongoDB
This is a very lightweigth utilities package for mongodb. The motivation behind this package is making some repetitive tasks more easly.
CollectionHelper
CollectionHelper expose api for CRUD operations on MongoDB collection which include validation before save operations using ValidationHelper
findOne: Wraps mongodb collection findOne method
try{
import Helpers from "@corluk/mongo-utils"
const client = new MongoClient()
const collection = client.db().collection("test")
const collectionHelper= Helpers.CollectionHelper(collection)
const obj = collectionHelper.findOne({key:"value"})
console.log("object" , obj)
}catch (err){
console.log("error", error.message)
}
// returns
// {key:"value"}
find Wraps mongodb collection find method
**
usage:
import Helpers from "@corluk/mongo-utils"
try{
const client = new MongoClient()
const collection = client.db().collection("test")
const collectionHelper= Helpers.CollectionHelper(collection)
const obj = collectionHelper.save({key:"value"})
console.log("object" , obj)
// retuns
// { _id : "61a2934c670ef7e01b8b392f", key:"value"}
ValidationHelper
ValidationHelper expose simple api for simple required field check if not validates throws an Error.
validate: Validates input object, throws error if validation
onValidate: Add custom function may throw error on validate.
addKey: Adds keys to check when validate called.
removeKey:
Removes key inside validation object.If you dont want to create new instance add and remove some keys in runtime.
usage:
import Helpers from "@corluk/mongodb-utils"
const validationHelper = Helpers.ValidationHelper(["key1","key2"])
validationHelper.removeKey("key2")
const obj = {key1: "a" }
validationHelper.validate(obj) // pass because we removed *key2* from
validationHelper.addKey("key2")
validateHelper.validate(obj) // throws error key2 added to required
fields
validationHelper.onValidate((obj)=>{
if(obj.items.length < 1){
throw new Error("items length less than one")
}
})
const obj = {items: []}
validationHelper.validate(obj) // throws error
keylist
*check test/validation.spec.js more usage examples*
PaginationHelper
Enables pagination to FindCursor object. Supports: - costum collation - custom sort - pagination cycle PaginationHelper takes cursor as parameter and paginate the cursor result and returns both count of items and items array setSort Adds sort parameter as shown in https://docs.mongodb.com/drivers/node/current/fundamentals/crud/read-operations/sort/
setCollation Adds collation parameter as shown in https://docs.mongodb.com/manual/reference/method/cursor.collation/
paginate Paginates cursor for given current page and perpage arguments. Cycles back and forth.
params
- page {number}
- perPage {number}
- mode {"lineer:default"| {"cycle"}}
usage
import Helpers from "@corluk/mongodb-utils"
import {MongoClient} from "mongodb"
const client = new MongoClient()
const collectionHelper = Helpers.CollectionHelper(client.db().collection("test"))
for(let i = 0 ; i != 100 ; i++){
await collectionHelper.save({value:i})
}
const paginationHelper = Helpers.PaginationHelper(cursor)
paginationHelper.setSort({value:1})
paginationHelper.setCollation({locale:"en_US", numeric})
const cursor = await collectionHelper.find({})
const page = paginationHelper.paginate(1,10)
// returns {count:100,items:[{value:1},{value:2}...{value:10}]}
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago