New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@xavisoft/mongoose-cascade

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xavisoft/mongoose-cascade

An npm package for implementing ON DELETE CASCADE, SET NULL, and RESTRICT behavior for Mongoose

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@xavisoft/mongoose-cascade

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.

Installation

npm install @xavisoft/mongoose-cascade

Example


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

Documentation

onDelete

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 deleted
  • ON_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 deleted
Cascade
  • Cascade

new Cascade(conn)
ParamTypeDescription
connmongoose.ConnectionThe mongoose connection to use. Defaults to mongoose.connection

cascade.delete(Model, filter, opts)

Method to delete, cascading as per schema definitions

ParamTypeDescription
Modelmongoose.ModelThe model to delete from
filterobjectthe where clause
optsobject
opts.sessionmongoose.ClientSessionProvide this if the deletion needs to be part of an already started transaction

cascade.init()

Initializes the instance. Throws an error if something is wrong with your schemas

Keywords

FAQs

Package last updated on 30 Aug 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc