mongoose-trash
v0.0.6
Published
This package helps to retrieve MongoDB documents from Trash. The package is tested on express.js, fastify.js and nest.js
Downloads
16
Maintainers
Readme
Mongoose Trash For MongooseJS
mongoose-trash
This package helps to retrieve MongoDB documents from Trash. The package is tested on express.js, fastify.js and nest.js
npm i mongoose-trash
TypeScript Integration
Create an interface representing a document in MongoDB With Mongoose Trash Document.
import { MongooseTrashDocument } from 'mongoose-trash'; interface User extends MongooseTrashDocument { name: string; email: string; }
Create a model with Mongoose Trash Query Helper
import { Model } from "mongoose"; import { MongooseTrashQueryHelpers } from 'mongoose-trash'; type UserModelType = Model<User, MongooseTrashQueryHelpers<User>>;
Create a Schema corresponding to the document interface with MongooseTrash And MongooseTrashQueryHelpers.
import { Schema } from "mongoose"; import { MongooseTrashQueryHelpers, MongooseTrash } from 'mongoose-trash'; const userSchema = new Schema<User, UserModelType, {}, MongooseTrashQueryHelpers<User>>({ name: { type: String, required: true }, email: { type: String, required: true }, }).plugin(MongooseTrash);
Create a model and trash many items
const User = model<User, UserModelType>("User", userSchema); const userIds = ["631b08391a4e2f00c19368d4", "631b08391a4e2f00c19368d6"]; await User.find({ _id: { $in: userIds, }, }).trashMany();
NestJS Integration
user.schema.ts
Fileimport { Prop, SchemaFactory } from '@nestjs/mongoose'; import { Document } from 'mongoose'; import { MongooseTrash, MongooseTrashDocument } from 'mongoose-trash'; export type UserDocument = User & MongooseTrashDocument & Document; export class User { @Prop() field: string; } export const UserSchema = SchemaFactory.createForClass(User).plugin(MongooseTrash);
user.service.ts
Injecting mongoose trash in user modelimport { Injectable } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { MongooseTrashQueryHelpers } from 'mongoose-trash'; @Injectable() export class UserService { constructor( @InjectModel(User.name) private readonly userModel: Model< UserDocument, MongooseTrashQueryHelpers<UserDocument> >, ) {} }
Trash many crud items given by mongo ids, like: ["6319ecec4131dd1b32e7441c", "6319ed3b4131dd1b32e7441f"]
async trashMany(ids: String[]) { const trashUser = await this.userModel .find({ _id: { $in: ids, }, }) .trashMany(); return trashUser; }
Trash And Restore A Single Document
const user = await User.where('name', 'Bill').withTrashed().findOne();
user?.trash(); // trash single document
user?.restore(); // restore single document
Query Helpers
| Method | Example |
| ------------------------ | ---------------------------------------------------- |
| trashMany()
| User.find({ _id: { $in: ['631b08391a4e2f00c19368d4'], }, }).trashMany()
|
| restoreMany()
| User.where('name', 'Md Al Amin').restoreMany()
|
| withTrashed()
| User.find({ "gmail": { $regex: '.*email.*' } }).withTrashed()
|
| onlyTrashed()
| User.find({ "gmail": { $regex: '.*email.*' } }).onlyTrashed();
|