Mongoose Status plugin
This plugin has a very simple interface:
const User = new mongoose.Schema({
});
User.plugin(status, {
default: "not-enabled",
enum: ["not-enabled", "enabled", "disabled"],
transitions: {
"not-enabled": ["enabled"],
"enabled": ["disabled"],
"disabled": ["enabled"]
}
});
// And then when you need to transition from an state A
// to a state B, you should do:
const result = user.transitionTo("disabled");
if (!result) {
// The transition couldn't be done
}
// The transition went well
Made with