Models are used to interact with APIs and represent data.
Each Model represents a resource, such as a Property, Branch or Article.
Most models share these async static methods:
findById()
findOne()
findAll()
These methods will return a Promise which resolves to return an object containing either result: an instance of the class (in the case of findOne and findById), or results: an array of instances (in the case of findAll). For example:
// `result` will be an instance of the Property model const { result: property } = awaitProperty.findById(12345);
// articles will be an array of instances of the Article model const { results: articles } = awaitArticle.findAll({ topic:'news' });
Each model has various instance getters (accessors) to interact with the model instance. Typically these render data, e.g.
// renders the property ID: 12345 property.id
Although getters run like normal methods they do not need to be invoked using ().
In case you need to access any data which does not have a getter, you can access the data directly using the get() instance method available on every model:
property.get('display_address');
When fetching multiple resources, it’s often beneficial for performance to resolve the promises concurrently using Promise.all():
// omitting 'await' here so the promise is returned constheroChunkPromise = ContentChunk.find({ name:'home_hero' }); constsidebarChunkPromise = ContentChunk.find({ name:'home_sidebar' });
You can also limit the fields that are returned - this can significantly reduce response times and therefore improve performance:
Note that the string values in the fields array should correspond to the Hestia data keys.
const { results: branches, errors, pagination, } = awaitBranch.findAll({ pageSize:12, page:1, // if we only need the ID and name of each branch, specify that here fields: ['branch_id', 'branch_name'], });
Serializers
There is a directory serializers/ (see Serializers) containing functions to specify the way models should be serialized to JSON.
Because models are complex objects they cannot be passed to client-side code in the same form as they exist on the server,
so they must be converted to serializable versions of those objects. The simplest way to do this is to combine JSON.parse() and JSON.stringify().
// in async server component const { result: property } = awaitProperty.findById(1234);
return ( // 'property' inside the client component will be the object returned by the property serializer <MyClientComponentproperty={serializableProperty}/> )
Consider performance when adding new values to a serializer as each method or accessor will be called when constructing the JSON.
Models are used to interact with APIs and represent data.
Each Model represents a resource, such as a Property, Branch or Article.
Most models share these async static methods:
findById()findOne()findAll()These methods will return a Promise which resolves to return an object containing either
result: an instance of the class (in the case of findOne and findById), orresults: an array of instances (in the case of findAll). For example:Each model has various instance getters (accessors) to interact with the model instance. Typically these render data, e.g.
Although getters run like normal methods they do not need to be invoked using ().
In case you need to access any data which does not have a getter, you can access the data directly using the get() instance method available on every model:
When fetching multiple resources, it’s often beneficial for performance to resolve the promises concurrently using Promise.all():
You can also limit the
fieldsthat are returned - this can significantly reduce response times and therefore improve performance:Note that the string values in the
fieldsarray should correspond to the Hestia data keys.Serializers
There is a directory
serializers/(see Serializers) containing functions to specify the way models should be serialized to JSON. Because models are complex objects they cannot be passed to client-side code in the same form as they exist on the server, so they must be converted to serializable versions of those objects. The simplest way to do this is to combine JSON.parse() and JSON.stringify().Consider performance when adding new values to a serializer as each method or accessor will be called when constructing the JSON.