ts-mongoose
ts-mongoose copied to clipboard
Two way references not possible
Right now we cannot create two schemas that both have references to each other. This is a variant of the issue #35. I need to have two schemas, one for an Organization and one for a Member. So the code I wrote is this:
const OrganizationSchema = createSchema({
name: Type.string({ required: true }),
members: Type.array({default: []}).of(Type.ref(Type.objectId()).to('Member', MemberSchema))
});
const MemberSchema = createSchema({
_id: Type.string({required: true, unique: true, index: true}),
email: Type.string(),
organization: Type.ref(Type.objectId()).to('Organization', OrganizationSchema)
});
The MemberSchema
used in the definition of OrganizationSchema
does not get recognized by typescript as it is not yet defined at that point.
Trying something like the workaround in issue #35 causes an issue in populateTs()
where the populated items don't have the added properties. Also, I highly dislike the redundant type of code used for this workaround.
It would be great if you can fix this issue! Thanks!
Did you find a better workaround, or is the workaround in #35 the only way to go? One problem I have been facing is circular dependencies since if I split the schemas out into 2 different files the imports get messed up.