New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
getDiscriminatorModelForClass doesn't respect the model's existingConnection option #789
Comments
it should, from what i know
from what i know
are you sure you want a discriminator on |
@hasezoey Sorry, that was my lazy translation from the real source. I've dug deeper and found that it was my bug, however, it would be super helpful if this spat out a warning ;) import {getDiscriminatorModelForClass, getModelForClass, prop} from "@typegoose/typegoose";
import mongoose from "mongoose";
export class ModelOne
{
@prop()
public var1: string;
}
export class ModelTwo extends ModelOne
{
@prop()
public var2: string;
}
async function test()
{
const db = await mongoose.createConnection('mongodb://127.0.0.1:27017/database1', {}).asPromise();
// To reproduce the issue we have to first get the model from the first db
getModelForClass(ModelTwo, {
existingConnection: db
});
const newDB = db.useDb('database2');
const model1 = getModelForClass(ModelOne, {
existingConnection: newDB
});
const model2 = getDiscriminatorModelForClass(model1, ModelTwo, {
existingConnection: newDB
});
const test = new model2();
test.var2 = 'test';
await test.save();
// Model2 will be created on database1
}
test().catch((e) =>
{
console.error(e);
})
|
🎉 This issue has been resolved in version 10.1.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Actually, I don't think the document change covers this entirely. I think the issue is that you can't define the same model on multiple connections/databases. I think we also need .. Or in fact, throw an error, since this is always going to result in unexpected behaviour |
no that is possible, that is a different issue - which is already documented as Models with same name or if this is not what you mean, please clarify |
Well, from my example script I first get ModelTwo on database1
Then a subsequent attempt to get ModelTwo on database2 results in it ending up on database1 despite it deriving from a model which is successfully established on database2
|
So actually because of this double-initialisation it results in the opposite behaviour than is documented by your changes |
the documented behavior is not for double defining the same model name i will consider adding a way to disable caching so this can be allowed (while disabling the getting from cache) Edit:
actually it does not create a new model, see Lines 427 to 429 in 4be10be
|
released typegoose for now the beta documentation can be found at Github Models with same name: Disable Caching |
Thanks! Could this be an option that could be passed to |
locally disabled cache is added with 78ac3bc, which will be in please test if this is properly working for now the beta documentation can be found at Github Models with same name: Disable Caching AND Github modelOptions: disableCaching (also the global option has been renamed from |
Versions
What is the Problem?
I would expect getDiscriminatorModelForClass to use the same connection that the base model uses. But that doesn't happen, even if you explicitly specify it.
Code Example
Do you know why it happens?
no
The text was updated successfully, but these errors were encountered: