mongoose-actions
v0.1.5
Published
`mongoose-actions` is a Mongoose plugin that tracks changes to documents and logs actions such as creation, updates, and deletions.
Downloads
139
Readme
mongoose-actions
mongoose-actions is a Mongoose plugin that tracks changes to documents and logs actions such as creation, updates, and deletions.
Installation
npm install mongoose-actionsUsage
Plugin Setup
First, apply the plugin to your Mongoose schema:
import mongoose from 'mongoose';
import mongooseActionsPlugin from 'mongoose-actions';
const testSchema = new mongoose.Schema({
name: String,
description: String,
untracked: String,
});
testSchema.plugin(mongooseActionsPlugin, { fields: ['name', 'description'] });
const TestModel = mongoose.model('Test', testSchema);Example
Here is an example of how to use the plugin:
import mongoose from 'mongoose';
import TestModel from './path-to-your-model';
async function run() {
const testDoc = new TestModel({ name: 'Test', description: 'Initial description' });
await testDoc.save();
testDoc.description = 'Updated description';
const user = new mongoose.Types.ObjectId();
await testDoc.modifiedBy(user).save();
// the listActions method returns an array of actions
const actions = await testDoc.listActions({limit: 10, skip: 0});
console.log(actions);
}
run();Running Tests
To run tests, use the following command:
npm testLicense
This project is licensed under the ISC License.
