Skip to content

autolotto/mongoose-archive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mongoose-archive

NPM

Mongoose archive plugin.

Yet another softdelete plugin, but with this one you will not be querying archived documents by default.

Description

Adds archive([callback]) and restore([callback]) methods to the documents. If archive or restore may be called without callback if used with Mongoose 4.x. In this case, they will return a Promise.

archive([callback]) creates archivedAt property with timestamp of the moment when the method was called.

restore([callback]) removes archivedAt field.

Plugin also patches find, findOne, findOneAndRemove and findOneAndUpdate methods to add { archivedAt: { $exists: false } } to the query object in case condition for archivedAt wasn't specified. This way you will not be querying archived documents (unless you specified archivedAt condition by yourself) as they were actually removed.

Setup

Install

npm install mongoose-archive

Usage

Just require and register plugin for a desired Schema.

import mongooseArchive from 'mongoose-archive';

Model.plugin(mongooseArchive);

After, you can call archive and restore methods on your Model instances.

instance.archive();
// instance will not be queriable with Model.find(), unless you will be querying
// archived documents by specifying proper archivedAt filter
instance.restore();
// now the document was "restored" from archive

// to query archived documents simply add { archivedAt: { $exists: true } } to your query
Model.find().where('archivedAt').exists();

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published