You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was playing around to see if I am able to ensure (some sort) of type safety when running the mutation operation because if you are using typescript, you can't even pass in a simple command like $unset operator in patch operation. I hope that I did not miss out on anything obvious and make this whole thing complex. 😃
It all started with not being able to do this (I'm using mongoose/mongo)
Because $unset (or any operator) isn't declared as part of the interface, typescript will complain, so if I want to use those commands, I will have to do something like
However. this is definitely not recommended since I will lose the typings...
So, I came up with something in a short time to test some idea and it does seem to work for my limited use-case, but it probably will not be able to apply for all use-case, or in general.
// Credits to stackoverflow for this OptionalKeystypeOptionalKeys<T>={[KinkeyofT]-?: Record<string, unknown>extendsPick<T,K> ? K : never}[keyofT];// This will be painful if you were to define all the possible operations and the data type// also, different database has different operators// but for my use-case, I only allow maybe a couple of mongo operators to pass throughinterfaceMongoQueryOps<T>{// OptionalKeys allows me to extract only optional keys from the interface// Ensure that only optional fields can be unset$unset: Partial<Record<OptionalKeys<T>,string>>;}// Creates the Base class for the actual service to extends upon// see Workspace class belowclassBase<T> extends Service<T>{// overwrite patch method to add MongoQueryOps typepatch(id: NullableId,data: Partial<T&MongoQueryOps<T>>,params?: Params): Promise<T|T[]>{returnsuper.patch(id,data,params);}_patch(id: NullableId,data: Partial<T&MongoQueryOps<T>>,params?: Params): Promise<T>{returnsuper._patch(id,data,params);}}interfaceWorkspaceInt{_id: string;text: string;description?: string;}
class Workspace extends Base<WorkspaceInt>{}
So I ended up with
So, now I can pass in $unset and also ensure that only certain (in this case, optional) fields can be $unset
If someone does manage to come up with something that can apply to all use cases, that would be great! This can definitely be also applied to Query interface where it is now declared as
interfaceQuery{[key: string]: any;}
If there is a better way to do all these, please let me know!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I was playing around to see if I am able to ensure (some sort) of type safety when running the
mutation
operation because if you are usingtypescript
, you can't even pass in a simple command like$unset
operator inpatch
operation. I hope that I did not miss out on anything obvious and make this whole thing complex. 😃It all started with not being able to do this (I'm using mongoose/mongo)
And this is my setup
Because
$unset
(or any operator) isn't declared as part of the interface,typescript
will complain, so if I want to use those commands, I will have to do something likeHowever. this is definitely not recommended since I will lose the typings...
So, I came up with something in a short time to test some idea and it does seem to work for my limited use-case, but it probably will not be able to apply for all use-case, or in general.
So I ended up with
So, now I can pass in
$unset
and also ensure that only certain (in this case, optional) fields can be$unset
If someone does manage to come up with something that can apply to all use cases, that would be great! This can definitely be also applied to
Query
interface where it is now declared asIf there is a better way to do all these, please let me know!
Beta Was this translation helpful? Give feedback.
All reactions