Automattic / Automattic/mongoose
Abstract base schemas for use exclusively with discriminators
- Dominant language
- JavaScript
- Stars
- 27.5k
- Forks
- 4k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 35
Description
**Do you want to request a *feature* or report a *bug*?**
*feature*
It would be nice to have abstract schemas that you cannot instantiate, and instead must instantiate via discriminators.
```js
var shapeSchema = new Schema({
name: String
}, {
discriminatorKey: "type",
abstract: true // <----
});
var Shape = db.model('Shape', shapeSchema);
var Circle = Shape.discriminator('Circle', new Schema({ radius: Number }));
```
`new Shape()` would be *invalid*
`Shape.create({type: "not a registered discriminator"})` would be *invalid*
`Shape.create({type: "Circle"})` would be *valid*
`new Circle()` would be *valid*
`Shape.find()` would be *valid*
If you agree that this functionality would be useful, I can try to work on a PR.
---
Current workaround is to have a helper function used before `Shape.create` that makes sure the value for the discriminator key is allowed.
Contributor guide
Research direction
Start by tracing the Schema, db.model, Shape.discriminator, Shape.create, and Shape.find entry points described in the issue. Determine how abstract schemas and discriminator registration are represented, then verify that direct Shape construction and unknown discriminator creation are rejected while Circle construction, valid creation, and queries remain valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb, nodejs
- Domain
- backend, databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100