@sigfox/mongoose-archive

1.0.1 • Public • Published

mongoose-archive - Mongoose plugin adding the method .archive() to your documents schemas

When use it?

This module can be used if you need to archive some Mongo resources.

Features

  • Adds .archive() method to your schema: set archivedAt: new Date() & isArchived: true.
  • Adds .restore() method to your schema: set archivedAt: undefined & isArchived: false.
  • On methods .find(), .findOne(), .findOneAndRemove() & .findOneAndUpdate(), archived documents will not be found anymore.

Install

npm install @sigfox/mongoose-archive

Usage

Schema declaration

const mongoose = require('mongoose');
const mongooseArchive = require('@sigfox/mongoose-archive');

const { Schema, mongo } = mongoose;

const userSchema = new Schema({
  firstName: { type: String, required: true },
  lastName: { type: String, required: true },
  email: { type: String, required: true }
});

userSchema.plugin(mongooseArchive);

Archive resource

const archiveUser = async UserModel => {
  const user = await UserModel.findOne({ email: 'foobar@mail.com' });
  await user.archive();
  ctx.status = 204;
};

Restore resource

const restoreUser = async user => {
  await user.restore();
};

Test

npm test

Licence

This project is licensed under the MIT License - see the LICENSE file for details.

Readme

Keywords

Package Sidebar

Install

npm i @sigfox/mongoose-archive

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

6.19 kB

Total Files

5

Last publish

Collaborators

  • robnriks
  • cgrimal
  • damien-s
  • lbdremy