Skip to content

🌳 Feature - Session management #3

Merged
taherashorna1 merged 37 commits intomasterfrom
feature/session-support
Sep 16, 2025
Merged

🌳 Feature - Session management #3
taherashorna1 merged 37 commits intomasterfrom
feature/session-support

Conversation

@taherashorna1
Copy link
Collaborator

AB#254512

In this PR, we are adding session management support to mongoose-delete

This pull request introduces support for MongoDB transactions using Mongoose sessions in the mongoose_delete plugin. It modifies key methods to accept session parameters, ensuring compatibility with transactional workflows. Additionally, new tests are added to validate the behavior of these changes.

Core Enhancements for Transaction Support:

  • Updated updateDocumentsByQuery, delete, deleteById, restore, and related methods to accept and handle session parameters. This ensures that operations like delete and restore can participate in MongoDB transactions. [1] [2] [3] [4]
  • Adjusted save options to include session handling and conditional validation for delete and restore operations. [1] [2]

Test Suite Additions:

  • Added a new test suite to validate session-based transactional behavior for delete, deleteById, and restore methods. These tests cover single-document operations, multi-document operations, and rollback scenarios.
  • Updated the MongoDB connection string in the test setup to include directConnection=true, ensuring compatibility with modern MongoDB configurations.

Copilot AI review requested due to automatic review settings July 16, 2025 05:18
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds session management support to the mongoose-delete plugin so that delete and restore operations can participate in MongoDB transactions, and includes tests to verify transactional behavior.

  • Extended core methods (updateDocumentsByQuery, delete, deleteById, restore, and related statics) to accept an optional session parameter.
  • Updated save calls to pass through session options and conditional validation.
  • Added a new test suite in test/index.js covering delete/restore within transactions, including commit and abort scenarios.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
index.js Updated method signatures and internal calls to accept and propagate session options.
test/index.js Introduced a new “session management” test suite to validate transactional delete/restore.
Comments suppressed due to low confidence (2)

index.js:374

  • Consider supporting the overload where params is a function (callback) by checking typeof params === 'function' and shifting arguments, to match the pattern used in deleteById.
    schema.statics.restore =  function (conditions, params = {}, callback) {

test/index.js:872

  • The static restore test covers commit scenarios but does not exercise rollback behavior; adding an abortTransaction test for multiple-document restore would ensure rollback works as expected.
  it("restore() with multiple documents -> should support transactions with session parameter", async function () {

Copy link

@harveyalex harveyalex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a few minor nit picks from me in the tests

index.js Outdated
Comment on lines +217 to +227
var options = {};
var lastArg = arguments[arguments.length - 1];
if (lastArg && typeof lastArg === 'object' && lastArg.session) {
options.session = lastArg.session;
}
var match = { $match : { showAllDocuments : 'true' } };
arguments.length ? args[0].unshift(match) : args.push([match]);
if (options.session) {
return Model[method].apply(this, args).session(options.session);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎗️ Same change as above

Copy link

@harveyalex harveyalex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +203 to +204
if (lastArg && typeof lastArg === 'object' && lastArg.session) {
session = lastArg.session;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Does this provide the backward compatibility with the accidental fallback session support? If not then maybe we just bump the major version and flag this as a breaking change.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a breaking change now. Maybe it is a good idea to bump it to a major version.

Comment on lines +806 to +813
// Delete multiple documents with session
const result = await TestModel.delete(
{ name: { $in: ['Anakin Skywalker', 'Obi-Wan Kenobi'] } },
{
deletedBy: userId,
session: session
}
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 I'd do this as two separate calls, an $in call will be seen as just a single query and doesn't really test the use case for a transaction

Suggested change
// Delete multiple documents with session
const result = await TestModel.delete(
{ name: { $in: ['Anakin Skywalker', 'Obi-Wan Kenobi'] } },
{
deletedBy: userId,
session: session
}
);
// Delete multiple documents with session
const result1 = await TestModel.delete(
{ name: 'Anakin Skywalker' },
{
deletedBy: userId,
session: session
}
);
const result2 = await TestModel.delete(
{ name: 'Obi-Wan Kenobi' },
{
deletedBy: userId,
session: session
}
);

Comment on lines +702 to +705
if (mongooseMajorVersion < 5) {
// Skip test for older mongoose versions without session support
return this.skip();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice, so this all works and avoids the tests if it's not the right Mongoose?

Copy link

@AndrewFinlay AndrewFinlay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, let's get this in!

@taherashorna1 taherashorna1 merged commit 825dd39 into master Sep 16, 2025
12 checks passed
@taherashorna1 taherashorna1 deleted the feature/session-support branch September 16, 2025 03:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants