-
Notifications
You must be signed in to change notification settings - Fork 23
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
self-references #35
Comments
Could you provide a use case for such refernece? |
The classic one is a person / employee that has a supervisor which is also a person / employee. |
I've found a workaround: In mongoose, you have an I split my model declaration in two steps. First I add everything not related to current model and next with const UserSchema = createSchema({
name: Type.string({ required: true }),
office: Type.string()
});
UserSchema.add(
createSchema({
superior: Type.schema().of(UserSchema),
accountant: Type.schema().of(UserSchema)
})
); It works for me as Hope it helps |
This above example works for me if I only define like this and do not use it. Once I need to assign/access self-reference property, such as |
@grimmer0125 What kind of exception? It's fine on my side |
code snippet:
TypeScript: 3.7.2 yarn run v1.16.0 120 p.seriesRef = p._id; Found 1 error. error Command failed with exit code 2. |
@grimmer0125 Add |
Thanks. @tuarrep The modified code:
This time
PhaseTask.ts:9:23 is this line |
For reference, here a working snippet from my code base: export const ProductSchema = createSchema({
diluent: Type.object({ required: true }).of({
paintbrush: Type.ref(Type.objectId()),
spray: Type.ref(Type.objectId())
})
});
/* WORKAROUND: https://github.com/BetterCallSky/ts-mongoose/issues/35 */
ProductSchema.add(
createSchema({
diluent: Type.object({ required: true }).of({
paintbrush: Type.ref(Type.objectId()).to("Product", ProductSchema),
spray: Type.ref(Type.objectId()).to("Product", ProductSchema)
})
})
); |
I think it would be nice to have this kind of example in the docs - for me that'd be enough to close the issue. |
Hi
How would I model a self-reference, such as
Both
Type.schema().of(UserSchema)
andType.ref(Type.objectId()).of("User", UserSchema)
will complain:The text was updated successfully, but these errors were encountered: