Properties
The properties
method is where you define the output that goes into the view you fetch from the Delivery API
properties: function (sourceEntity, context) {
return {
title: sourceEntity.properties.title,
seo: {
title: sourceEntity.properties.seoTitle,
description: sourceEntity.properties.seoDescription,
}
categoryIds: sourceEntity.properties.categoryIds
.map(categoryId => parseInt(categoryId))
};
}
The properties method must return an object with the data you want to include in your view.
PropertiesContext object
The PropertiesContext
object is passed into the properties
method and gives you access to a set of methods described below.
Methods
Method | Description |
---|---|
lookup | Lookup allows you to search source entities using a filter string and work with the source entities directly in the schema. |
partial | Referencing a partial schema. Mapped data from a partial schema is embedded into the calling schema. |
reference | Referencing a full schema. References to other schemas are resolved on delivery request time. |
lookup
Lookup source entities.
Lookup allows you to search source entities using a filter string and work with the data from the source entities directly in the schema.
Note the lookup
is an async function so you have to use async/await when you are using the lookup
function.
lookup(filter)
Parameters
Parameter | Type | Description |
---|---|---|
filter | string | A filtering criteria. |
See list of filter examples here
Required function calls
After the lookup
function it's required to call toPromise
to excecute the query and return a promise.
toPromise
Using the toPromise function excecutes the query and return a promise you must resolve.
const newsArticles: await context
.lookup("type eq 'newsArticle'")
.toPromise()
Optional function calls
To filter the source entities even further you can call some of the following optional functions.
limit
The limit
function limits the number of source entities.
Parameters
Parameter | Type | Description |
---|---|---|
limit | number | The maximum number of source entities to return. |
const newsArticles: await context
.lookup("type eq 'newsArticle'")
.limit(5)
.toPromise()
orderBy
The order sequence of the source entities.
Parameters
Parameter | Type | Description |
---|---|---|
orderBy | { propertyName: string, direction: "asc" | "desc" } | Allows you to specify your desired sorting order. |
const newsArticles: await context
.lookup("type eq 'newsArticle'")
.orderBy({ propertyName: "properties.createdDate", direction: "desc"})
.toPromise()
sourceGroup
The sourceGroup function lets you specify the source group. By default the source group of the current source entity is used.
Parameters
Parameter | Type | Description |
---|---|---|
sourceGroup | string | Allows you to define a different source group. The sourceGroupAlias should be equal to the desired source group alias where you want to look for source entities. |
If not defined, it uses the current source group.
const newsArticles: await context
.lookup("type eq 'newsArticle'")
.sourceGroup("anotherSourceGroup")
.toPromise()
Examples
properties: async function (sourceEntity, context) {
const latestNews = await context
.lookup("type eq 'newsArticle'")
.orderBy({ propertyName: "properties.createdDate", direction: "desc"})
.limit(3)
.toPromise();
const mappedNews = latestNews
.map((news) => ({
url: news.url,
title: news.properties.title,
teaser: news.properties.teaser
}));
return {
latestNews: mappedNews
}
}
partial
Referencing a partial schema.
The partial mapping property type allows for dynamically including partial schemas into the main schema. This is useful when you need to iterate an array of different objects that has an identifier, like an ID, alias or similar.
Partial schemas are typically used when you want a reusable schema for mapping data that is part of the same entity. E.g. if metadata (title, description, etc.) is the same across different entity types, but the data lives on the entity itself.
Read more about partial schemas here
partial(schemaAlias, input)