Skip to content

Commit

Permalink
Merge pull request #135 from davellx/issue-130-aggregatewithdeleted-d…
Browse files Browse the repository at this point in the history
…iscriminators

Handle aggregate for discriminators
  • Loading branch information
dsanel authored Jun 13, 2023
2 parents 842c8f2 + 59f1e81 commit 008ea81
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [v1.0.1]
> June 13, 2023
- fix: aggregateWithDeleted returns no result with Discriminators #130
- Update `devDependencies` to `"mongoose": "^7.2.4"`

## [v1.0.0]
> June 12, 2023
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ module.exports = function (schema, options) {

if (finalList.indexOf('aggregate') > -1) {
schema.pre('aggregate', function() {
var firsMatchStr = JSON.stringify(this.pipeline()[0]);
var firstMatch = this.pipeline()[0];

if ( firsMatchStr !== '{"$match":{"deleted":{"$ne":false}}}' ) {
if (firsMatchStr === '{"$match":{"showAllDocuments":"true"}}') {
if(firstMatch.$match?.deleted?.$ne !== false){
if(firstMatch.$match?.showAllDocuments === 'true'){
var {showAllDocuments, ...replacement} = firstMatch.$match;
this.pipeline().shift();
} else {
if(Object.keys(replacement).length > 0){
this.pipeline().unshift({ $match: replacement });
}
}else{
this.pipeline().unshift({ $match: { deleted: { '$ne': true } } });
}
}
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"chai": "^4.3.7",
"mocha": "^10.2.0",
"mongoose": "^7.2.3",
"mongoose": "^7.2.4",
"nyc": "^15.0.0"
}
}
61 changes: 60 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1745,6 +1745,64 @@ describe("aggregate methods: { overrideMethods: ['aggregate'] }", function () {
});
});

describe("aggregate methods & discriminator: { overrideMethods: ['aggregate'] }", function () {
var TestSchema = new Schema({ name: String }, { collection: 'mongoose_delete_test_aggregate', discriminatorKey:'kind' });
TestSchema.plugin(mongoose_delete, { overrideMethods: ['aggregate'] });

var TestModel = mongoose.model('Test6_Aggregate', TestSchema);
var DiscriminatorTestModel = TestModel.discriminator('DiscriminatorTest',new Schema({ age: Number }))

beforeEach(async function () {
await DiscriminatorTestModel.create(
[
{ name: 'Lando Calrissian', age: 46, deleted: true },
{ name: 'Han Solo', age: 44 },
{ name: 'Jabba Desilijic Tiure', age:617, deleted: true },
{ name: 'Boba Fett', age: 61 },
]);
});

afterEach(async function () {
await mongoose.connection.db.dropCollection("mongoose_delete_test_aggregate");
});

it("aggregateWithDeleted([{ $match: { age:{ $gte: 50 } } }]) -> should return deleted documents from discriminator (pipeline)", async function () {
try {
var documents = await DiscriminatorTestModel
.aggregateWithDeleted([{ $match: { age:{ $gte: 50 } } }])
.project({ name : 1, age:1 });

documents.length.should.equal(2);
} catch (err) {
should.not.exist(err);
}
});

it("aggregate([{ $match: { age:{ $gte: 50 } } }]) -> should return non-deleted documents from discriminator (pipeline)", async function () {
try {
var documents = await DiscriminatorTestModel
.aggregate([{ $match: { age:{ $gte: 50 } } }])
.project({ name : 1, age:1 });

documents.length.should.equal(1);
} catch (err) {
should.not.exist(err);
}
});

it("aggregateDeleted([{ $match: { age:{ $gte: 50 } } }]) -> should return ONLY deleted documents from discriminator (pipeline)", async function () {
try {
var documents = await DiscriminatorTestModel
.aggregateDeleted([{ $match: { age:{ $gte: 50 } } }])
.project({ name : 1, age: 1 });

documents.length.should.equal(1);
} catch (err) {
should.not.exist(err);
}
});
});

describe("mongoose_delete find method overridden with populate", function () {
var TestPopulateSchema1 = new Schema(
{ name: String },
Expand Down Expand Up @@ -1914,4 +1972,5 @@ describe("model validation on restore (default): { validateBeforeRestore: true }
should.not.exist(err);
}
});
});
});

0 comments on commit 008ea81

Please sign in to comment.