Skip to content
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

Custom type with transformation upon read #12904

Closed
1 task done
OoDeLally opened this issue Jan 12, 2023 · 4 comments
Closed
1 task done

Custom type with transformation upon read #12904

OoDeLally opened this issue Jan 12, 2023 · 4 comments
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary Stale

Comments

@OoDeLally
Copy link

OoDeLally commented Jan 12, 2023

Prerequisites

  • I have written a descriptive issue title

Mongoose version

6.8.2

Node.js version

6.17.1

MongoDB version

4.13.0

Operating system

Linux

Issue

I would like to manipulate a type X in my codebase, store it as Y format in the DB, and then get back a X when I fetch.

(codebase) X ==saving==> (db) Y ==fetching==> (codebase) X

X => Y can be done with a custom schema type's cast().
However, I cannot find the way to do the opposite, i.e. deserialize Y into X upon fetching. The document I get from .find() uses Y.

A few workaround I've tried:

  • use transform() with .toObject(). But .toObject is only available in hydrated docs everytime, and I would prefer getting lean documents.
  • use post-query hooks. This forces me to manually set the hook to every single schema that uses the type X. This is hardly scalable.
  • Create my own mongoose.Type and mongoose.SchemaType. I tried to mimic what https://github.com/mongoosejs/mongoose-function is doing. But it did not work, and I see this library is not maintained since 2013, so I'm not sure it would work with current mongoose.

This topic seems similar, but there are no solution proposed: #10423

@OoDeLally OoDeLally added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Jan 12, 2023
@IslandRhythms
Copy link
Collaborator

Lean may not be the solution here https://mongoosejs.com/docs/api.html#query_Query-lean
You want to transform the data coming out of the db and lean is designed for read only cases.
A cursor may be what you are looking for: https://mongoosejs.com/docs/api/query.html#query_Query-cursor
After a find call, you can iterate through each document converting Y back into X.
Although the easiest solution would be to save it as x to the db if you're having to convert it back when fetching. Is there something preventing you from doing so that you can share?

@OoDeLally
Copy link
Author

We want to store large integers.
Our use case would be to use bigint in the codebase, and store them as Long in the mongodb.
Classic number cannot safely store numbers above 9,007,199,254,740,991 (Number.MAX_SAFE_VALUE). So we would like to use bigint, which are arbitrary long.
MongoDB Long can store up to 9,223,372,036,854,775,807 (int64 => 2^63), so it is slightly more than Number.MAX_SAFE_VALUE.

@github-actions
Copy link

github-actions bot commented Feb 3, 2023

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Feb 3, 2023
@github-actions
Copy link

github-actions bot commented Feb 9, 2023

This issue was closed because it has been inactive for 19 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary Stale
Projects
None yet
Development

No branches or pull requests

2 participants