Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(model): skip applying static hooks by default if static name conf…
…licts with model,document middleware
  • Loading branch information
dragontaek-lee committed Sep 24, 2024
commit c647a051c5dcf61d0a3123dc28502afd39331fd1
48 changes: 48 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5891,6 +5891,54 @@ describe('Model', function() {
assert.equal(called, 1);
});

it('custom statics that overwrite model functions dont get hooks by default', async function() {

const schema = new Schema({ name: String });

schema.statics.insertMany = function(docs) {
return model.insertMany.apply(this, [docs]);
};

let called = 0;
schema.pre('insertMany', function(next) {
++called;
next();
});
const Model = db.model('Test', schema);

const res = await Model.insertMany([
{ name: 'foo' },
{ name: 'boo' }
]);

assert.ok(res[0].name);
assert.ok(res[1].name);
assert.equal(called, 1);
});

it('custom statics that overwrite document functions dont get hooks by default', async function() {

const schema = new Schema({ name: String });

schema.statics.save = async function() {
return 'foo';
};

let called = 0;
schema.pre('save', function(next) {
++called;
next();
});

const Model = db.model('Test', schema);

const doc = await Model.save();

assert.ok(doc);
assert.equal(doc, 'foo');
assert.equal(called, 0);
});

it('error handling middleware passes saved doc (gh-7832)', async function() {
const schema = new Schema({ _id: Number });

Expand Down
Loading