qwik
qwik copied to clipboard
[✨] Custom Class serialization
Is your feature request related to a problem?
I'm using MongoDB and every request contains one or more ObjectId
class object which can not be serialized.
And I'm lazy to write a bunch of .map
s and .toString()
s to convert those fields to string manually.
Describe the solution you'd like
Write a bunch of .map
s and .toString()
s after every request is too terrible for me.
I'm looking for those solutions
- Add a serializer to handle this
ObjectId
inmongodb
(or inbson
) just likeURL
andDate
. (ideal but a waste for non-mongodb user) - Implement above serializer as a plugin for runtime. (possible?)
- Support custom serialization for any class, such as using
toString()
orqSerialize()
function automatically. (however, usingtoString()
converts any class object intostring
, which requires additional type transform inuseLoader()
anduseAction
, etc.)
Describe alternatives you've considered
n/a
Additional context
No response
https://github.com/bluesky-social/atproto has a class object BlobRef that is also not serializable, would love to write a custom serializer instead of bugging the authors to change it.
Facing a similar challenge with Prisma returning a Decimal object.
Would it be possible to support adding custom serializers via a vite plugin?
Custom serializers is a complex problem, which is why we have been punting it. Perhaps you can send a PR to add the Decimal Object?
I think most custom serializers can be avoided by putting methods in the store that on invocation check if the desired object exists and if not, create it with noSerialize before doing their own work.
The technique I describe here can be useful for that.
I don't think Qwik should ship with support for random libraries but maybe the $sync proposal can help with doing actual custom serialization