you can use a generic way to be applicable for any model you need, only you pass model and data type, and you can apply all crud operations on it.
source github
- CREATE
async function create<T>(data:T , model:any){
const newObj = await new model(data);
await newObj.save()
return newObj;
- READ
async function read(model:any){
return await model.find({});
}
- UPDATE
async function update<T>(id:string, data:T , model:any){
return await model.findByIdAndUpdate(id,data);
}
- DELETE
async function getById (id:string,model:any){
const data = await model.findOne({id});
if(!data) {
return 'id is not valid';
}
return data ;
}
Top comments (0)