Deep defaults
See original GitHub issueWe’re currently in the process of migrating from mongoose to papr. Here’s a question that I have:
Say I have schema for users
collection:
const userSchema = schema(
{
numPosts: types.number(),
emailPreferences: types.object({
monthly: types.boolean(),
newsletter: types.boolean(),
followers: types.boolean(),
}),
appreciations: types.array(types.object({
badge: types.objectId(),
count: types.number(),
})),
},
{
defaults: {
numPosts: 0,
emailPreferences: {
monthly: true,
newsletter: true,
followers: true,
},
appreciations: [
{
count: 0
}
],
}
}
)
As you can see, I have 3 root fields in this schema: numPosts
, emailPreferences
, and appreciations
. I use papr’s default functionality for all 3.
For numPosts
, it’s straight forward.
As for emailPreferences
, what happens when I try to insert an object that only has one field defined ? will papr merge that with the defaults of the 2 remaining fields ?
EDIT: The answer is no. It will not be merged w/ the other defaults. When I tried the following insert:
UserModel.insertOne({
name: "dsafnaisdhfujabsf1",
email: "iqwuheiqhsiadiasidu1@gmail.com",
username: "dsafnaisdhfujabsf1",
emailPreferences: {
newsletter: false,
},
});
The resulting document only included newsletter: false
:
...
emailPreferences: { newsletter: false },
This is not ideal. The expected behaviour here is that it should merge the one field I defined with other undefined fields from the defaults object.
And as for appreciations
, how do I represent defaults for an array of objects ? is this the correct way ?
What would happen if I try to insert this document without appreciations
field ? will it insert just the count
field ?
EDIT: the answer is yes. Papr is going to insert an array that contains: appreciations: [{ count: 0 }]
by default.
The expected behaviour here is that these defaults are only used when I try to insert an array. How do I achieve this ?
Does Papr allow me to use nested schemas ? EDIT: the answer is no. Papr models will work, but they will not make use of the nested schema.
Overall, I think the current API for defaults in papr is really limited and confusing. I believe defaults shouldn’t be in a separate object. They should be included in the type definitions themselves. Which is the case in mongoose and ts-mongoose:
newsletter: Type.boolean({ default: false })
Is such a change possible/planned ? And if not, how can I achieve the same capabilities for defining defaults using Papr ?
Thanks a lot for your amazing package.
Issue Analytics
- State:
- Created a year ago
- Comments:9
I’m OOO now, but I saw this thread and wanted to comment.
Regarding your last comment about the API resembling the
ts-mongoose
one, I remember that was an initial implementation of our first version ofpapr
, before we open sourced it. But we had to move away from that API, because we need the defaults type as a generic type as well at the top level of aModel
in order to use it in certain methods likeinsertOne
, etc.If you can make this API change, and keep all the existing functionality in the tests, we’re more than happy to consider such a change.
@ejmartin504 From an API standpoint, I think the defaults API of
ts-mongoose
(may she rest in piece) is a good place to be at.What do you think about this ?