Build order for Classes to Schemas is changed to bottom-up #629
-
https://github.com/typegoose/typegoose/blob/master/CHANGELOG.md#-breaking-changes "Build order for Classes to Schemas is changed to bottom-up, which can affect some environments" We are on version 8 across all services right now, do you have any more info on what is meant by bottom-up in this context? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
i hope the following example explains what is affected: on 8.3.0: // NodeJS: 16.11.0
// MongoDB: 4.2-bionic (Docker)
import { getModelForClass, prop } from '@typegoose/typegoose'; // @typegoose/typegoose@8.3.0
import { assertion } from '@typegoose/typegoose/lib/internal/utils';
import * as mongoose from 'mongoose'; // mongoose@5.13.13
class Animal {
@prop({ default: 'AnimalType' })
public type?: string;
public getName(): string {
return 'AnimalGetName';
}
}
class Cat extends Animal {
@prop({ default: 'CatType' })
public type?: string;
@prop({ default: 'unknownSubType' })
public subtype?: string;
public getName(): string {
return 'CatGetName';
}
}
class MaineCoon extends Cat {
@prop({ default: 'MaineCoonSubType' })
public subtype?: string;
public getName(): string {
return 'MaineCoonGetName';
}
}
const MaineCoonModel = getModelForClass(MaineCoon);
const CatModel = getModelForClass(Cat);
const AnimalModel = getModelForClass(Animal);
(async () => {
await mongoose.connect(`mongodb://localhost:27017/`, {
dbName: 'verifyMASTER',
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
});
const animaldoc = await AnimalModel.create({});
console.log(animaldoc.type, animaldoc.getName());
assertion(animaldoc.type === 'AnimalType');
assertion(animaldoc.getName() === 'AnimalGetName');
const catdoc = await CatModel.create({});
console.log(catdoc.type, catdoc.subtype, catdoc.getName());
assertion(catdoc.type === 'CatType');
assertion(catdoc.subtype === 'unknownSubType');
assertion(catdoc.getName() === 'CatGetName');
const mainecoondoc = await MaineCoonModel.create({});
console.log(mainecoondoc.type, mainecoondoc.subtype, mainecoondoc.getName());
assertion(mainecoondoc.type === 'AnimalType'); // notice this
assertion(mainecoondoc.subtype === 'MaineCoonSubType');
assertion(mainecoondoc.getName() === 'MaineCoonGetName');
await mongoose.disconnect();
})(); with output: AnimalType AnimalGetName
CatType unknownSubType CatGetName
AnimalType MaineCoonSubType MaineCoonGetName and on 9.2.0: // NodeJS: 16.11.0
// MongoDB: 4.2-bionic (Docker)
import { getModelForClass, prop } from '@typegoose/typegoose'; // @typegoose/typegoose@9.2.0
import { assertion } from '@typegoose/typegoose/lib/internal/utils';
import * as mongoose from 'mongoose'; // mongoose@6.0.12
class Animal {
@prop({ default: 'AnimalType' })
public type?: string;
public getName(): string {
return 'AnimalGetName';
}
}
class Cat extends Animal {
@prop({ default: 'CatType' })
public type?: string;
@prop({ default: 'unknownSubType' })
public subtype?: string;
public getName(): string {
return 'CatGetName';
}
}
class MaineCoon extends Cat {
@prop({ default: 'MaineCoonSubType' })
public subtype?: string;
public getName(): string {
return 'MaineCoonGetName';
}
}
const MaineCoonModel = getModelForClass(MaineCoon);
const CatModel = getModelForClass(Cat);
const AnimalModel = getModelForClass(Animal);
(async () => {
await mongoose.connect(`mongodb://localhost:27017/`, {
dbName: 'verifyMASTER',
});
const animaldoc = await AnimalModel.create({});
console.log(animaldoc.type, animaldoc.getName());
assertion(animaldoc.type === 'AnimalType');
assertion(animaldoc.getName() === 'AnimalGetName');
const catdoc = await CatModel.create({});
console.log(catdoc.type, catdoc.subtype, catdoc.getName());
assertion(catdoc.type === 'CatType');
assertion(catdoc.subtype === 'unknownSubType');
assertion(catdoc.getName() === 'CatGetName');
const mainecoondoc = await MaineCoonModel.create({});
console.log(mainecoondoc.type, mainecoondoc.subtype, mainecoondoc.getName());
assertion(mainecoondoc.type === 'CatType'); // this changed
assertion(mainecoondoc.subtype === 'MaineCoonSubType');
assertion(mainecoondoc.getName() === 'MaineCoonGetName');
await mongoose.disconnect();
})(); with output: AnimalType AnimalGetName
CatType unknownSubType CatGetName
CatType MaineCoonSubType MaineCoonGetName the order until 8.x for class
the order in 9.x and forward for class
TL;DR: if classes extend some classes, and have functions defined and overwritten in the extended from classes and not overwritten in top-level class, then the overwrite is basically not applied because this is a change in behavior, it is a breaking change that could affect some environments that worked-around this issue or somehow made use of it |
Beta Was this translation helpful? Give feedback.
i hope the following example explains what is affected:
on 8.3.0: