Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Breaking change in v3.2.0 for extending schemas #421

Closed tschneid closed 3 years ago

tschneid commented 3 years ago

Describe the bug

In my project, v3.2.0 introduced a breaking change. That's either a bug in meteor-collection2 or I've used extending schemas in an unintended way. In any case, this could break existing projects and should be fixed or documented.

To Reproduce

The following code yields different results in v3.1.0 and v3.2.0

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';

const MyCollection = new Mongo.Collection('myCollection');

const mySchema = new SimpleSchema({
  foo: { type: String },
});

MyCollection.attachSchema(mySchema);
MyCollection.insert({ foo: 'foo' });

mySchema.extend({
  bar: { type: String, optional: true },
});

// I'm expecting that the `bar` key is set. It isn't in v3.2.0.
MyCollection.update({ foo: 'foo' }, { $set: { foo: 'foo', bar: 'bar' } });

// Logs { _id, foo, bar } in v3.1.0
// Logs { _id, foo } in v3.2.0
console.log(MyCollection.findOne({ foo: 'foo' }));

Expected behavior

Both versions should work the same.

Versions (please complete the following information):

Additional context From what I read about Attaching Multiple Schemas to the Same Collection (introduced in v3.2.0), I will now use MyCollection.attachSchema(extension) instead of mySchema.extend(extension). IMO the second would be more explicit.

harryadel commented 3 years ago

Thanks for your thorough explanation and example code. I pushed a new release could you check it out?

tschneid commented 3 years ago

That was fast! :slightly_smiling_face: Your fix works. Thanks a lot, @harryadel!