@abslibs/mongoose-plugin 中文文档教程
Mongoose Plugin
@abslibs/mongoose-plugin 是一个简单轻量级的插件,可以为 mongoose 启用一些基本的必需功能。
Features
- Soft Delete document using destroy method.
Methods and Fields | Description |
---|---|
destroy() | method on document (do not override standard remove() method) |
destroyById() | static method |
deleted | (true-false) key on document |
deletedAt | Add key to store time of deletion |
deletedBy | key to record who deleted document |
- Restore deleted documents using restore method
Feature | Description |
---|---|
Bulk destroy and restore | Bulk Destroy |
Option to override static methods | count, countDocuments, find, findOne, findOneAndUpdate, update |
Disable model validation on destroy | Disable Validation |
Option to create index on destroy fields | deleted, deletedAt, deletedBy |
Installation
使用 npm 安装
npm install @abslibs/mongoose-plugin
Usage
我们可以使用这个插件,有或没有选项。
Setup
const mongoose_plugin = require('@abslibs/mongoose-plugin');
const TestSchema = new Schema({
name: String
});
// Apply on specific model.
// Can apply globally : eg: mongoose.plugin(mongoose_plugin, {})
TestSchema.plugin(mongoose_plugin, {
paranoid: true,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
const Test = mongoose.model('Test', TestSchema);
Options
偏执:软删除需要为真。
timestamps :它将添加 [createdAt & updatedAt] 在架构中。
createdAt :可以用给定的字符串替换createdAt。 例如:createdAt: 'createdat'_
updatedAt :可以用给定的字符串替换 updatedAt。 例如:updatedAt: 'createdat'_
Simple usage
const test = new Test({ name: 'Test' });
test.save(function() {
test.destroy(function() {
// deleted: true
test.restore(function() {});
//deleted: false
});
});
var exampleTestId = mongoose.Types.ObjectId('53da93b16b4a6670076b16bf');
// INFO: Example usage of destroyById static method
Test.destroyById(exampleTestId, function(err, TestDocument) {});
Get sot soft deleted data.
// pass *{ paranoid: false }* as option.
// This will return response including deleted documents.
test.find({ name: 'Arpit' }, null, { paranoid: false }, (err, user) => {});
Who has deleted the data?
var mongoose_plugin = require('@abslibs/mongoose-plugin');
var TestSchema = new Schema({
name: String
});
TestSchema.plugin(mongoose_plugin, { deletedBy: true });
var Test = mongoose.model('Test', TestSchema);
var test = new Test({ name: 'Test' });
test.save(function() {
// mongodb: { deleted: false, name: 'Test' }
var idUser = mongoose.Types.ObjectId('53da93b16b4a6670076b16bf');
test.destroy(idUser, function() {
// mongodb: { deleted: true, name: 'Test', deletedBy: ObjectId("53da93b16b4a6670076b16bf")}
test.restore(function() {});
});
});
Bulk destroy and restore
var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
// destroy multiple object, callback
Test.destroy(function (err, result) { ... });
Test.destroy({age:10}, function (err, result) { ... });
Test.destroy({}, idUser, function (err, result) { ... });
Test.destroy({age:10}, idUser, function (err, result) { ... });
// destroy multiple object, promise
Test.destroy().exec(function (err, result) { ... });
Test.destroy({age:10}).exec(function (err, result) { ... });
Test.destroy({}, idUser).exec(function (err, result) { ... });
Test.destroy({age:10}, idUser).exec(function (err, result) { ... });
// Restore multiple object, callback
Test.restore(function (err, result) { ... });
Test.restore({age:10}, function (err, result) { ... });
// Restore multiple object, promise
Test.restore().exec(function (err, result) { ... });
Test.restore({age:10}).exec(function (err, result) { ... });
Create index on fields
TestSchema.plugin(mongoose_plugin, { indexFields: true });
// Index only specific fields
TestSchema.plugin(mongoose_plugin, {
indexFields: ['deleted', 'deletedBy']
});
// or
TestSchema.plugin(mongoose_plugin, { indexFields: ['deletedAt'] });
Method overridden
我们可以选择覆盖所有标准方法或仅覆盖特定方法。 覆盖的方法将从结果中排除已删除的文档,即具有 deleted = true
的文档。 每个重写的方法都会有两个额外的方法,因此我们将能够处理已删除的文档。
注意:如果paranoid 为真,方法将被覆盖
only not deleted documents |
---|
count() |
find() |
findOne() |
findOneAndUpdate() |
update() |
Mongoose Plugin
@abslibs/mongoose-plugin is simple and lightweight plugin that enables some basic required functionality for mongoose.
Features
- Soft Delete document using destroy method.
Methods and Fields | Description |
---|---|
destroy() | method on document (do not override standard remove() method) |
destroyById() | static method |
deleted | (true-false) key on document |
deletedAt | Add key to store time of deletion |
deletedBy | key to record who deleted document |
- Restore deleted documents using restore method
Feature | Description |
---|---|
Bulk destroy and restore | Bulk Destroy |
Option to override static methods | count, countDocuments, find, findOne, findOneAndUpdate, update |
Disable model validation on destroy | Disable Validation |
Option to create index on destroy fields | deleted, deletedAt, deletedBy |
Installation
Install using npm
npm install @abslibs/mongoose-plugin
Usage
We can use this plugin with or without options.
Setup
const mongoose_plugin = require('@abslibs/mongoose-plugin');
const TestSchema = new Schema({
name: String
});
// Apply on specific model.
// Can apply globally : eg: mongoose.plugin(mongoose_plugin, {})
TestSchema.plugin(mongoose_plugin, {
paranoid: true,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
const Test = mongoose.model('Test', TestSchema);
Options
paranoid : it needs to be true for soft deletion.
timestamps : it will add [createdAt & updatedAt] in schema.
createdAt : can replace createdAt by given string. eg: createdAt: 'createdat'_
updatedAt : can replace updatedAt by given string. eg: updatedAt: 'createdat'_
Simple usage
const test = new Test({ name: 'Test' });
test.save(function() {
test.destroy(function() {
// deleted: true
test.restore(function() {});
//deleted: false
});
});
var exampleTestId = mongoose.Types.ObjectId('53da93b16b4a6670076b16bf');
// INFO: Example usage of destroyById static method
Test.destroyById(exampleTestId, function(err, TestDocument) {});
Get sot soft deleted data.
// pass *{ paranoid: false }* as option.
// This will return response including deleted documents.
test.find({ name: 'Arpit' }, null, { paranoid: false }, (err, user) => {});
Who has deleted the data?
var mongoose_plugin = require('@abslibs/mongoose-plugin');
var TestSchema = new Schema({
name: String
});
TestSchema.plugin(mongoose_plugin, { deletedBy: true });
var Test = mongoose.model('Test', TestSchema);
var test = new Test({ name: 'Test' });
test.save(function() {
// mongodb: { deleted: false, name: 'Test' }
var idUser = mongoose.Types.ObjectId('53da93b16b4a6670076b16bf');
test.destroy(idUser, function() {
// mongodb: { deleted: true, name: 'Test', deletedBy: ObjectId("53da93b16b4a6670076b16bf")}
test.restore(function() {});
});
});
Bulk destroy and restore
var idUser = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
// destroy multiple object, callback
Test.destroy(function (err, result) { ... });
Test.destroy({age:10}, function (err, result) { ... });
Test.destroy({}, idUser, function (err, result) { ... });
Test.destroy({age:10}, idUser, function (err, result) { ... });
// destroy multiple object, promise
Test.destroy().exec(function (err, result) { ... });
Test.destroy({age:10}).exec(function (err, result) { ... });
Test.destroy({}, idUser).exec(function (err, result) { ... });
Test.destroy({age:10}, idUser).exec(function (err, result) { ... });
// Restore multiple object, callback
Test.restore(function (err, result) { ... });
Test.restore({age:10}, function (err, result) { ... });
// Restore multiple object, promise
Test.restore().exec(function (err, result) { ... });
Test.restore({age:10}).exec(function (err, result) { ... });
Create index on fields
TestSchema.plugin(mongoose_plugin, { indexFields: true });
// Index only specific fields
TestSchema.plugin(mongoose_plugin, {
indexFields: ['deleted', 'deletedBy']
});
// or
TestSchema.plugin(mongoose_plugin, { indexFields: ['deletedAt'] });
Method overridden
We have the option to override all standard methods or only specific methods. Overridden methods will exclude deleted documents from results, documents that have deleted = true
. Every overridden method will have two additional methods, so we will be able to work with deleted documents.
NOTE : Method will be overridden if paranoid is true
only not deleted documents |
---|
count() |
find() |
findOne() |
findOneAndUpdate() |
update() |
你可能也喜欢
- @01group/fxl 中文文档教程
- @11tyrocks/eleventy-plugin-objectfit-focalpoint 中文文档教程
- @21epub/epub-json-panel 中文文档教程
- @21kb/react-notification-hook 中文文档教程
- @2bitlab/queue-load 中文文档教程
- @36node/adventurer-core-sdk 中文文档教程
- @3kles/kles-selected-list 中文文档教程
- @7urtle/documenter 中文文档教程
- @7util/log-express 中文文档教程
- @aamodtgroup/frontity-gravity-forms 中文文档教程