Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

[Question] Extending existing Typegoose Class within same collection #337

Closed
HendrikRoth opened this issue Jul 17, 2019 · 3 comments
Closed
Labels

Comments

@HendrikRoth
Copy link

HendrikRoth commented Jul 17, 2019

Hello everyone,
is it possible to extend a created typegoose class (entity) but using the same collection as the parent class?

example:

class Base extends Typegoose {
  @prop()
  name: string;
}

class Extended extends Base {
  @prop()
  age: number;
}

instead of creating a new collection for extended i would like to use the same base collection. Is this possible and how? I tried to use mongoose SchemaOptions, but seams not to work for this variant?

const ExtendedModel = new Extended().getModelForClass(Extended, { schemaOptions: { collection: "base" } });

The goal is to be able to query the mongo database with the BaseModel helper.

const BaseModel = new Base().getModelForClass(Base);
await BaseModel.findById(...);

// should deliver also the extended entries.
@hasezoey
Copy link
Contributor

hasezoey commented Jul 17, 2019

so when i understand this correctly:

  • you want to have a parent model
  • you want to have a child model, extending the parent model
  • you want the parent & child to use the same collection
  • and you want to still query the child model, but only with the values from the parent model

right?


yes, it is half possible:

  • you can have a child & parent in the same collection with Discriminators, BUT they are currently not supported with typegoose, WIP: Discriminators #333 adds it, but also breaks some things, so no merge in the near future
  • and no, to my knowledge it is not possible, at least not with typegoose and the correct types ...

@HendrikRoth
Copy link
Author

Yes you did understand me right.
Ok, than i have to figure out how to change my structure, thank you

@hasezoey
Copy link
Contributor

i consider it solved | closed now, thanks that i could help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants