Skip to content

Commit

Permalink
Merge pull request #34 from Ebulus7899/master
Browse files Browse the repository at this point in the history
Add support for findOneAndReplace
  • Loading branch information
vkarpov15 authored Apr 27, 2024
2 parents 87eb2ca + bc5b488 commit a1dcb4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = function mongooseLeanGetters(schema) {
schema.post('findOne', fn);
schema.post('findOneAndUpdate', fn);
schema.post('findOneAndDelete', fn);
schema.post('findOneAndReplace', fn);
};

function applyGettersMiddleware(schema) {
Expand Down
20 changes: 20 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,24 @@ describe('mongoose-lean-getters', function() {
assert.strictEqual(user.name, 'ONE');
assert.deepStrictEqual(foundUser.emails, ['TWO', 'THREE']);
});

it('should work with findOneAndReplace (gh-31)', async function() {
const testSchema = new mongoose.Schema({
field: Number,
});
testSchema.plugin(mongooseLeanGetters);

testSchema.path('field').get(function(field) {
return field.toString();
});
const Test = mongoose.model('gh-31', testSchema);

await Test.deleteMany({});
const entry = await Test.create({
field: 1337
});
const doc = await Test.findOneAndReplace({ _id: entry._id }, entry).lean({ getters: true });
assert.equal(typeof doc.field, 'string');
assert.strictEqual(doc.field, '1337');
});
});

0 comments on commit a1dcb4a

Please sign in to comment.