meteor-astronomy icon indicating copy to clipboard operation
meteor-astronomy copied to clipboard

Immutable field on a nested class

Open mazak opened this issue 7 years ago • 3 comments

I'm using a nested class with an _id field that I'd like to have be unique and readOnly. This is my setup:

const NestedClass = Class.create({ 
// ... 
  fields: {
    _id: {
      type: String,
      immutable: true,
      default() {
        return new Mongo.Mongo.ObjectID()._str;
      },
    },
  }
// ...
});
const MainClass = Class.create({
// ...
  fields: {
    nested: {
      type: [NestedClass],
      optional: true,
      default() {
        return [];
      },
    },
  },
// ...
});

Only the first element of the nested class added to the object is added with the _id field. Every object that I add more has the _id field absent, or removed if passed along in new NestedClass({ _id: 'something' });. I've tried playing with setting different combinations of optional, default and immutable and it always boils down to immutable. When that's set to false - it works fine. Turning it off is fine as a temporary solution, but if this issue can't be fixed I'll have to think of some additional verification to make sure that my ID stays untouched. Is that a bug? Can that be fixed?

Thank you for this great package!

mazak avatar Mar 05 '18 16:03 mazak

Please, create reproduction repository so I can test it.

lukejagodzinski avatar Mar 05 '18 20:03 lukejagodzinski

I can confirm this bug. If a nested field is set to immutable then only the first nested element will contain those values. Subsequently added nested element will not containe the immutable values. My code is as follows:

const Player = Class.create({
	name: 'Player',
	fields: {
		id: {
			type: String,
			immutable: true,
		},
        ...

const Game = Class.create({
	name: 'Game',
	collection: Games,
	fields: {
		player1: {
			type: Player,
			optional: true,
		},
                player2: {
			type: Player,
			optional: true,
		},
        ...

Then in a class Game helper:

this.player1 = new Player({id: id1});
this.player2 = new Player({id: id2});

Now only this.player1.id will be set; this.player2.id will be undefined and not saved.

paul8046 avatar Feb 07 '19 01:02 paul8046

Can you create reproduction repository?

lukejagodzinski avatar Feb 09 '19 16:02 lukejagodzinski