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
Use different transformers for client<->server #378
Comments
Hmm.. there is no such option right now, and I'd be hesitant to build it as it feels a bit like a can of worms to differentiate this in each direction and a bit fiddly to add. If devalue can't be used for serialization from client->server you'd still have the same bottleneck when passing input args from client->server, although they are indeed usually much smaller. Are you sure you're not doing premature optimization? Is the superjson really that slow? I do find superjson a bit overkill myself as I only really care about dates and nothing else (and I find date objects a bit annoying as well as equality checks on two different dates - maybe write your own lib called trpc/packages/server/src/transformer.ts Lines 3 to 6 in 8628cb5
|
Thanks for your very fast answer! I totally see adding this as an option could be difficult and I absolutely agree that dates are annoying. :D
This only occurs in one specific app, where I have a huge collection (~1000) of models and every model contains another collection of models (~50). All models need to have multiple dates. Yes, I could do some optimization (strip non-necessary updatedAt field for example), but still the conversion with superjson would take >300ms. I made a short test with the benchmark script provided by superjson:
So superjson is not that slow, but devalue still is much faster and in my case I would like to use that extra performance. Would it take a big effort to modify the transformer interface like this? This should not break any existing implementation. export type DataTransformer = {
serialize(object: any, direction: 'up' | 'down'): any;
deserialize(object: any, direction: 'up' | 'down'): any;
}; PS: Sorry for submitting the comment to early... |
I was thinking something like this to avoid breaking change export type DataTransformer = {
serialize(object: any): any;
deserialize(object: any): any;
};
export type DataTransformerOption = DataTransformer | { up: DataTransformer, down: DataTransformer } I don't really have time to do this + the tests required, I'd be open to merging it if you could figure it out? I'd open VSCode & search for everywhere in the project where the keyword "transformer" is used and start hacking |
Development workflowgit clone git@github.com:trpc/trpc.git
cd trpc
yarn Hacking around with itIn one terminal, will run yarn dev In another terminal, you can for instance navigate to Testingcd packages/server
yarn test --watch |
Thank you! I will have a look and try to implement that! Probably taking some days playing around with the library first. Edit: See here for a draft PR (#393) |
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you. |
First of all I want to say thank you very much for making this library! I was already looking for a library like this for quite some time.
There is one thing I would like to ask/suggest:
Superjson is great for preserving type information, but has some performance drops for example when using with lots of nested dates. There is another library, devalue, which has a much better performance, but it is not usably for client->server for security reasons.
Thats why I currently use superjson for client->server and devalue for server->client in some of my apps.
Is it currently possible to use different transformers for each direction? If not, I would like to suggest adding this.
The text was updated successfully, but these errors were encountered: