Skip to content

Commit a72c023

Browse files
authored
Merge pull request #13968 from Automattic/vkarpov15/gh-13956
fix(document): avoid triggering setter when initializing `Model.prototype.collection` to allow defining `collection` as a schema path name
2 parents e1d3bfa + 157823c commit a72c023

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/model.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4764,8 +4764,6 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47644764

47654765
schema._preCompile();
47664766

4767-
model.prototype.$__setSchema(schema);
4768-
47694767
const _userProvidedOptions = schema._userProvidedOptions || {};
47704768

47714769
const collectionOptions = {
@@ -4778,13 +4776,16 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47784776
collectionOptions.autoCreate = schema.options.autoCreate;
47794777
}
47804778

4781-
model.prototype.collection = connection.collection(
4779+
const collection = connection.collection(
47824780
collectionName,
47834781
collectionOptions
47844782
);
47854783

4786-
model.prototype.$collection = model.prototype.collection;
4787-
model.prototype[modelCollectionSymbol] = model.prototype.collection;
4784+
model.prototype.collection = collection;
4785+
model.prototype.$collection = collection;
4786+
model.prototype[modelCollectionSymbol] = collection;
4787+
4788+
model.prototype.$__setSchema(schema);
47884789

47894790
// apply methods and statics
47904791
applyMethods(model, schema);
@@ -4793,8 +4794,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47934794
applyStaticHooks(model, schema.s.hooks, schema.statics);
47944795

47954796
model.schema = model.prototype.$__schema;
4796-
model.collection = model.prototype.collection;
4797-
model.$__collection = model.collection;
4797+
model.collection = collection;
4798+
model.$__collection = collection;
47984799

47994800
// Create custom query constructor
48004801
model.Query = function() {

test/document.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12608,6 +12608,18 @@ describe('document', function() {
1260812608
['I am NumberTyped', 'I am StringTyped']
1260912609
);
1261012610
});
12611+
12612+
it('can use `collection` as schema name (gh-13956)', async function() {
12613+
const schema = new mongoose.Schema({ name: String, collection: String });
12614+
const Test = db.model('Test', schema);
12615+
12616+
const doc = await Test.create({ name: 'foo', collection: 'bar' });
12617+
assert.strictEqual(doc.collection, 'bar');
12618+
doc.collection = 'baz';
12619+
await doc.save();
12620+
const { collection } = await Test.findById(doc);
12621+
assert.strictEqual(collection, 'baz');
12622+
});
1261112623
});
1261212624

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

0 commit comments

Comments
 (0)