Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
@xavisoft/mongoose-cascade
Advanced tools
An npm package for implementing ON DELETE CASCADE, SET NULL, and RESTRICT behavior for Mongoose
This npm package enables easy implementation of ON DELETE CASCADE, SET NULL, and RESTRICT behaviors in Mongoose schemas for MongoDB databases.
Unlike SQL-based databases, MongoDB does not provide these features by default. This package fills this gap by simplifying the configuration of cascading operations in your MongoDB databases, ensuring referential integrity in your database schemas.
npm install @xavisoft/mongoose-cascade
const { default: mongoose, Schema } = require("mongoose");
const { Cascade, constants } = require('@xavisoft/mongoose-cascade');
const { ON_DELETE } = constants;
// create models
const User = mongoose.model('User', new Schema({
name: String,
surname: String,
}));
const Comment = mongoose.model('Comment', new Schema({
text: String,
user: {
type: mongoose.SchemaTypes.ObjectId,
ref: 'User',
onDelete: ON_DELETE.SET_NULL,
},
createdAt: Date,
}));
// create docs
const cap = await User.create({
name: 'Steve',
surname: 'Rogers',
});
const comment = await Comment.create({
text: 'I can do this all day!',
user: cap._id,
createdAt: new Date(),
});
// delete
const cascade = new Cascade();
cascade.init();
await cascade.delete(User, { _id: cap._id });
// check
const updatedComment = await Comment.findById(comment._id);
console.log(updatedComment.user); // output: null
To configure on delete behavior for your references, provide onDelete
value with your schema references as shown in the above example, with one of the values below:
ON_DELETE.SET_NULL
: Set the references to the document(s) being deleted to null
ON_DELETE.CASCADE
: Delete all the document referencing the document(s) being deletedON_DELETE.RESTRICT
: Do not delete if there are still documents referencing the document(s). Instead raise a DeleteRestrictedError
ON_DELETE.PULL
: Apply the $pull
operator on the array to remove items referencing document(s) being deletedParam | Type | Description |
---|---|---|
conn | mongoose.Connection | The mongoose connection to use. Defaults to mongoose.connection |
Method to delete, cascading as per schema definitions
Param | Type | Description |
---|---|---|
Model | mongoose.Model | The model to delete from |
filter | object | the where clause |
opts | object | |
opts.session | mongoose.ClientSession | Provide this if the deletion needs to be part of an already started transaction |
Initializes the instance. Throws an error if something is wrong with your schemas
FAQs
An npm package for implementing ON DELETE CASCADE, SET NULL, and RESTRICT behavior for Mongoose
We found that @xavisoft/mongoose-cascade demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.