Add defaults as option to type.factory
This version introduces defaults and the schema to be overridden when using state.factory
or model.factory
, as well as the schema (was already possible, but poorly documented). This allows for partial instances to be created that can then be merged with their more complete versions, while allowing to define defaults for client-only or base instances. This flexibility is instrumental in using Vry to model API resources.
Breaking Changes
While the API is purely additive, the new propagation behaviour of the defaults for models
, changes previously expected behaviour. The following now does something different:
const User = Model.create({
typeName: 'user',
defaults: {
name: 'Unknown Author'
}
})
const Post = Model.create({
typeName: 'post',
defaults: {
author: {}
},
schema: {
author: User
}
})
const post = Post.factory()
Before these changes, creating a new post
would create an embedded author
with the name Unknown Author
, with the defaults of the nested schema applied. Now, however, the defaults are propagated to the nested model. In this case, the post
would still have a User
model embedded as an author, but with it's defaults set to {}
, not have the default name
prop applied.
To get the old behaviour, you can use the model.defaults()
to explicitly set the defaults for the nested schema:
// ....
const Post = Model.create({
typeName: 'post',
defaults: {
author: User.defaults()
},
schema: {
author: User
}
})