Skip to content

Commit 4d3a43d

Browse files
committed
Add distinct method to the list of supported methods #149
1 parent c502921 commit 4d3a43d

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ We have the option to override all standard methods or only specific methods. Ov
244244
| aggregate() | aggregateDeleted | aggregateWithDeleted |
245245
| findById() | Please use findOne | Please use findOneWithDeleted |
246246
| findByIdAndUpdate() | Please use findOneAndUpdateDeleted | Please use findOneAndUpdateWithDeleted |
247-
247+
| distinct() | distinctDeleted | distinctWithDeleted |
248248

249249
### Examples how to override one or multiple methods
250250

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module.exports = function (schema, options) {
121121

122122
if (options.overrideMethods) {
123123
var overrideItems = options.overrideMethods;
124-
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany', 'aggregate'];
124+
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany', 'aggregate', 'distinct'];
125125
var finalList = [];
126126

127127
if ((typeof overrideItems === 'string' || overrideItems instanceof String) && overrideItems === 'all') {
@@ -159,7 +159,7 @@ module.exports = function (schema, options) {
159159
}
160160

161161
finalList.forEach(function(method) {
162-
if (['count', 'countDocuments', 'find', 'findOne'].indexOf(method) > -1) {
162+
if (['count', 'countDocuments', 'find', 'findOne', 'distinct'].indexOf(method) > -1) {
163163
var modelMethodName = method;
164164

165165
schema.statics[method] = function () {

test/index.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,15 @@ describe("check not overridden static methods", function () {
831831
should.not.exist(err);
832832
}
833833
});
834+
835+
it("distinct() -> should return 3 documents", async function () {
836+
try {
837+
const doc = await TestModel.distinct('name');
838+
doc.should.have.members(['Obi-Wan Kenobi', 'Darth Vader', 'Luke Skywalker']);
839+
} catch (err) {
840+
should.not.exist(err);
841+
}
842+
});
834843
});
835844

836845
describe("check overridden static methods: { overrideMethods: 'all' }", function () {
@@ -1166,6 +1175,33 @@ describe("check overridden static methods: { overrideMethods: 'all' }", function
11661175
should.not.exist(err);
11671176
}
11681177
});
1178+
1179+
it("distinct() -> should return 1 documents", async function () {
1180+
try {
1181+
const doc = await TestModel.distinct('name');
1182+
doc.should.have.members(['Darth Vader']);
1183+
} catch (err) {
1184+
should.not.exist(err);
1185+
}
1186+
});
1187+
1188+
it("distinctDeleted() -> should return 2 documents", async function () {
1189+
try {
1190+
const doc = await TestModel.distinctDeleted('name');
1191+
doc.should.have.members(['Obi-Wan Kenobi', 'Luke Skywalker']);
1192+
} catch (err) {
1193+
should.not.exist(err);
1194+
}
1195+
});
1196+
1197+
it("distinctWithDeleted() -> should return 3 documents", async function () {
1198+
try {
1199+
const doc = await TestModel.distinctWithDeleted('name');
1200+
doc.should.have.members(['Obi-Wan Kenobi', 'Darth Vader', 'Luke Skywalker']);
1201+
} catch (err) {
1202+
should.not.exist(err);
1203+
}
1204+
});
11691205
});
11701206

11711207
describe("check the existence of override static methods: { overrideMethods: true }", function () {
@@ -1270,9 +1306,9 @@ describe("check the existence of override static methods: { overrideMethods: tru
12701306
});
12711307
});
12721308

1273-
describe("check the existence of override static methods: { overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany'] }", function () {
1309+
describe("check the existence of override static methods: { overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany', 'distinct'] }", function () {
12741310
var TestSchema = new Schema({name: String}, {collection: 'mongoose_delete_test'});
1275-
TestSchema.plugin(mongoose_delete, {overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany']});
1311+
TestSchema.plugin(mongoose_delete, {overrideMethods: ['testError', 'count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateOne', 'updateMany', 'distinct']});
12761312
var TestModel = mongoose.model('Test7', TestSchema);
12771313

12781314
it("testError() -> method should not exist", function () {
@@ -1374,6 +1410,18 @@ describe("check the existence of override static methods: { overrideMethods: ['t
13741410
it("updateManyWithDeleted() -> method should exist", function () {
13751411
expect(TestModel.updateManyWithDeleted).to.exist;
13761412
});
1413+
1414+
it("distinct() => method should exist", function () {
1415+
expect(TestModel.distinct).to.exist;
1416+
});
1417+
1418+
it("distinctDeleted() => method should exist", function () {
1419+
expect(TestModel.distinctDeleted).to.exist;
1420+
});
1421+
1422+
it("distinctWithDeleted() => method should exist", function () {
1423+
expect(TestModel.distinctWithDeleted).to.exist;
1424+
});
13771425
});
13781426

13791427
describe("check the existence of override static methods: { overrideMethods: ['count', 'countDocuments', 'find'] }", function () {

0 commit comments

Comments
 (0)