A singleton is a special one-element entity introduced in OData V4. It can be referred by its name from the service root, without having to know its key and without requiring an entity set.
You can annotate an entity with @odata.singleton or @odata.singleton.nullable to use it as a singleton in your service in CAP projects.
123456789
service myService {
@odata.singleton entity MySingleton {
key id : String; // can be omitted
prop1 : String;
prop2 : Boolean;
}
}
In your CAP CDS annotations, you can reference properties from the singleton entity by using the $edmjson inline mechanism.
Let us consider a typical use case for singletons in self service apps. Here, you might want to load the user context describing the user's authorizations and default settings. In this case, the back end can simply use the log on ID of the user to send the relevant data through the corresponding singleton, exposing the user context.
Check another example of the use cases for singleton entities in SAP Fiori elements applications for OData V4 "Using Singletons to influence the visibility of the Create, Delete and Edit Buttons". For more information, see Actions.
Note
$edmJson
is supported only with CDS compiler 2.3.0 or higher.
For more information about using the $edmjson inline mechanismĀ in CAP CDS annotations, see Dynamic Expressions section in Serving OData APIs.