Skip to content

Commit

Permalink
set Document.modelName in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Nov 11, 2022
1 parent f1c433c commit 9b8debe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/document.js
Expand Up @@ -195,6 +195,10 @@ function Document(obj, fields, skipId, options) {
});
}

if (this.modelName === undefined) {
this.modelName = this.constructor.modelName;
}

applyQueue(this);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -94,7 +94,7 @@
"release": "git pull && git push origin master --tags && npm publish",
"release-legacy": "git pull origin 5.x && git push origin 5.x --tags && npm publish --tag legacy",
"mongo": "node ./tools/repl.js",
"test": "mocha --exit ./test/*.test.js",
"test": "mocha --exit --timeout 30000 ./test/*.test.js",
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
"test-tsd": "node ./test/types/check-types-filename && tsd",
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
Expand Down
7 changes: 7 additions & 0 deletions test/document.test.js
Expand Up @@ -11996,6 +11996,13 @@ describe('document', function() {
title: 'The power of JavaScript'
});
});

it('should apply `modelName` property to new document (gh-12669)', async function() {
const Model = db.model('Test', new Schema());

const doc = new Model();
assert.equal(doc.modelName, 'Test');
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 9b8debe

Please sign in to comment.