Comments (10)
Hello, thanks for your interest in Typegoose. Yes I'm afraid currently sub schemas are supported only. This is an already requested feature so it is on the list.
from typegoose.
@szokodiakos I'm afraid you won't be able to do that. Typescript reflection is very limited and the only way to have sub documents is by defining sub schemas using classes.
from typegoose.
@szokodiakos Any estimation for this? Is this complicated and do you have time for this enhancement?
from typegoose.
@MickL Sorry I don't have the time for research and implementation right now. Currently I'm just trying to fix critical issues, and review other folks PRs.
@19majkel94 that is bad news, then maybe there isn't a viable solution currently at all. 😞
from typegoose.
@MickL @szokodiakos there's a workaround that i just found.
You could create a class for your address and use it in person.
All you have to do, is cast it to an object while setting it before saving.
Here's an example:
class Address {
street: string
streetnumber: number
}
class Person extends Typegoose {
@prop()
id: string;
@prop()
address: Address
});
export let PersonModel = new Person().getModelForClass(Address)
// Here the code before saving
const person = new PersonModel()
const address = new Address()
address.street = "some street"
address.streetnumber = 42
person.address = Object.assign({}, address)
person.save()
No need to cast it back to Address when loading, the only trick is the Object.assign({}, address)
which creates an object containing the fields in address.
Hope that helps you!
from typegoose.
This issue should be closed? The work around is to add a new class and use that class as type. Another option is the use object
(works with latest typegoose)
from typegoose.
What if I have a Ref in a nested object? For example, I have an Environment model:
class EnvironmentModel extends Typegoose {
@prop()
name: string;
//... other properties below
}
and then I have an Organization model that references that Environment model in a nested object. The desired schema is:
const OrganizationSchema = new mongoose.Schema({
name: { type: String, required: true, index: { unique: true } },
suborganizations: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Organization' }],
settings: {
environments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Environment' }]
}
} // exported with mongoose.model('Organization', OrganizationSchema)
I've tried the following:
class OrgSettings {
environment: Ref<EnvironmentModel>[]
}
class OrganizationModel extends Typegoose {
@prop({required: true, index: true, unique: true })
name: string;
@arrayProp({ itemsRef: OrganizationModel })
suborganizations: Ref<OrganizationModel>[];
@prop()
settings: OrgSettings
}
This doesn't appear to work and I'm curious if I'm misunderstanding something.
from typegoose.
Anyone come up with a good solution for the problem mentioned above?
from typegoose.
This seems to work in version 6.0.0
from typegoose.
@Ben305 this can be closed because this should work in 5.9.0+
from typegoose.
Related Issues (20)
- Please use the new Repository to open new issues HOT 4
- Pre hook supplied function error HOT 56
- TypeError: Class constructor Typegoose cannot be invoked without 'new' HOT 1
- [Request] Setting Collection Name and other Schema options via annotation and reflections
- [Question] Import ObjectId from Typegoose? HOT 4
- Please archive this repo
- Is there any way to achieve schema inheritance with typegoose? HOT 2
- [Question] Type of User.x property is not a y HOT 1
- Is it possible to use getModelForClass with generics? HOT 1
- [Question] HOT 1
- my populate field cant be queried out HOT 3
- [Question] HOT 1
- [Question] Problems reusing the same schema for both collection and subdocument HOT 1
- [Question] Can we add custom error messages when @prop options fail? HOT 2
- [Question] Are transactions supported for deleteMany? HOT 1
- [Question] Possible to have Query type support? HOT 1
- bi-directional virtuals population issue HOT 1
- Cannot use same model on two connections. HOT 2
- [Question] Is there a `required if` feature on model classes? HOT 2
- Both updatedBy and CreatedBy are getting updated after update operation on model object HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typegoose.