The Sling Data Model syntax allows the programmer to annotate classes with the #dataModel custom modifier, informing the compiler that the class is to be treated as a data model. What exactly that means depends on how this class will then be compiled, particularly the custom filters passed to slingc upon compiling.
See below for sample usage of the #dataModel modifier:
class #dataModel:
prop personId as string #dataField #primary
prop name as string #dataField
prop gender as string #dataField
prop age as int #dataField
Data fields
As also seen in the sample above, the programmer may define data fields inside data models by use of the #dataField modifier. The following options are available for data fields:
Inside data model classes, you must indicate each data field property using the #dataField modifier. Otherwise the property will receive no special treatment.
The modifier #primary is used to indicate that the field is the primary key of this data model.
The modifier #detail is used to indicaet that the field is a "detail" field, and will be excluded in generic queries that produce lists of items.
Modifiers #sortDesc and #sortAsc on data fields are used to tell the compiler that queries should use the given data field for sorting (descending and ascending, respectively).
Special methods
Inside a data model class, the developer may also annotate methods with certain modifiers to give them special meaning. The following modifiers are supported for methods:
#onImport methods will be called whenever data is imported to the data model. This way you can provide code to do any custom initialization.
#onExport methods will be called whenever data is exported from the data model. This way you can provide any computational fields or any other customizations to outgoing data.
#onQuery methods will be called whenever data is queried from the database and brought to the model.
#onInsert methods will be called prior to inserting a new record to the database. Often used eg. to automatically generate custom ID values.
#onUpdate methods will be called prior to updating the database record contents with the instance of the class.
#onDelete methods will be called prior to deleting a record from the database.
Usage notes
Normally, data models are used in server side development to automatically generate SQL code to store and retrieve data to/from a database. This functionality is generated by The Sling compiler when then @capexDataModelSql prefilter is enabled.
Relevant tutorials
The data model functionality is used in the following tutorials: