Types for find, update etc. for a generic repository? #880
Unanswered
norahmaria
asked this question in
Q&A
Replies: 1 comment 7 replies
-
i dont know if this is actually related to the error, but also i am not quite sure what your generics on |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm having a little bit of an issue with creating a generic repository - primarily with the types to allow the correct inputs when there are refs involved.
I have my schema, that uses a couple references.
My interface used in every other repository (Typegoose is only used in a shared package that exports the db repositories). This is also used client side which is why there is no use of the
ObjectId
type.And I'm trying to figure out the types for generic functions like find and update etc. Below is what I have right now, I also earlier had just a version were it accepted
Partial<U>
,U
being the interface I use for the data in other repositories for the project, so in this exampleTicket
- but then mongoose itself complained aboutfindOne
.The
TicketRepository
then extends this with the types<Ticket, TicketSchema>
from above.So I'm using
DocumentType
, but when I then try to use find for example - none of the inputs I'd like are accepted. For example when I fetch a user, and try to pass it in as thecreator
in the filter forfindOne
, it complains:But if I try to pass in just the ID, it's also not happy.
Most times when I have a reference, I'd like to just pass the id (as a
string
or anObjectId
, as mongoose are ok with both - and same for reference arrays, I'd usually just pass in(string | ObjectId)[]
).Or, in the example case - pass in the reference directly if it's already fetched from the db in the same code.
However, I'm not able to find a utility type for this? It seems
DocumentType<U>
is not the type to use as the input/filter types. I'd expect a type that allows any reference (+ nested refs) to have the typeRef<REF_TYPE> | REF_TYPE | string | ObjectId
, as those are all valid inputs in mongoose for a reference.How do I get the type of just a valid input or valid filter? I'm sure I'm just missing something very obvious, any help appreciated!☺️
Beta Was this translation helpful? Give feedback.
All reactions