Automattic / Automattic/mongoose
How to extend a mongoose schema without duplicating its memory footprint?
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
### Prerequisites
- [X] I have written a descriptive issue title
### Mongoose version
6.5.4
### Node.js version
16.17.0
### MongoDB version
6.0.5
### Operating system
Linux
### Operating system version (i.e. 20.04, 11.3, 10)
Arch
### Issue
Hello!
We use mongoose in a big production application, and recently I am starting to become concerned about the memory usage of our schemas. We have a multi tenant application, in which every tenant can extend some schemas adding what we call "custom" fields. We have around ~50 base schemas, and for every tenant, we search the defined customs fields, and clone the schema. After this process, we have around 900 schemas in memory.
The issue is that these schemas are starting to take up a significant amount on memory, and I am wondering if there is any way to reduce this memory footprint.
The main approach I was thinking about was if there was any way to create a new schema that extends another schema, adding some fields, **without** the need to clone the entire base schema, and 'reusing' the definitions from the base schema. I do not know enough about the mongoose internals to know if this approach would be possible, but theoretically it should be doable.
To summarize, my questions would be the following:
- Is it possible to extend a mongoose schema without the need to clone the schema?
- Do you recommend any other approach in order to solve this problem?
Contributor guide
Research direction
Start with the reported setup: roughly 50 base schemas are cloned per tenant, producing about 900 schemas in memory. Review the Mongoose 6.5.4 schema extension and cloning behavior, then measure memory for the current approach and any documented alternative. Done means establishing whether schema definitions can be reused and documenting a supported recommendation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, node.js
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100