
Data models in WebDSL are defined using entity definitions. An entity definition consists of the entity's name, possibly a super-entity from which it inherits, 0 or more properties and 0 or more entity functions:
entity User {
name :: String (length = 25)
email :: Email
password :: Secret
homepage :: URL
pages -> Set<Page>
function checkPassword(String s) : Bool {
return password.check(s);
}
}
A property consists of 4 parts:
The difference between reference and composite property kinds is that composite indicates that the referred entity is part of the one referring to it. The only effect this currently has is that composite cascades delete (deleting the entity will also delete the referred entity).
For a complete overview of the available types, see Types.
An example data model for a blogging site:
entity Author {
name :: String
email :: Email
password :: Secret
posts -> Set<Post> (inverse=Post.author)
}
entity Post {
author -> Author
title :: String
text :: Text
comments <> Set<Comment> (inverse=Comment.post)
}
entity Comment {
post -> Post
author :: String
text :: Text
}
The 'name' property is special, it is declared for each entity. By default it is a derived property that simply returns the id of the entity (which is also a special property declared for each entity, id:UUID is set automatically). The name can be customized by declaring a real name property:
name :: String
Or derived name property:
name :: String := firstname + lastname
Or by declaring a property as the name using an annotation:
someproperty :: String (name)
The name property is used in input and select template elements to refer to an entity. Example:
application exampleapp
init{
var u := User{};
u.save();
u := User{};
u.save();
u := User{};
u.save();
}
entity User{}
entity UserList{
users -> List<User>
}
var globalList := UserList{}
define page root(){
for(u:User in globalList.users){
output(u.name) //there is always a name property
}
form{
input(globalList.users) //this will show three UUIDs as options
submit("save",action{})
}
}
If the name is not a real property, you cannot create an input for it or assign to it.
Entities can inherit properties and functions from other entities, like subclassing in Object-Oriented programming.
Example:
entity Sub : Super {
str :: String
}
entity Super {
i :: Int
}
function test(){
var e1 := Sub{ i := 1 str := "sdf" };
var e2 := Super{ i := 1 };
}
Subclass entities can be passed whenever an argument of one of its super types is expected.
Example:
function test(){
var e1 := Sub{ i := 1 str := "sdf" };
test(e1);
}
function test(s:Super){
log(s.i);
}
Checking the dynamic type of an entity can be done using isa and casting is performed using as.
Example:
function test(s:Super){
if(s isa Sub){
var su :Sub := s as Sub;
log(su.str);
}
}
For defined entities, a number of global functions are automatically generated. Replace Entity with the defined entity name below.
If the Entity has an id annotation on a property, the following functions are generated (idtype is the type of the id property):
getUniqueEntity
getUniqueEntity(id : idtype) : Entity
If the Entity with the given id already exists, it is returned. If it did not exist, it is created once and a flush to the database is performed (this will commit any changes made to the entities in memory, e.g. the changes from data binding of input fields), repeated calls to this function with the same argument will keep returning that created Entity.
isUniqueEntity
isUniqueEntity(ent : Entity) : Bool
This function returns false when the value of the id property of ent is already taken. The function returns true when the id property is not taken, but will do so only once, subsequent calls with different entities but the same id will then return false (which makes this function suitable for processing a batch of entities in an action).
isUniqueEntityId
isUniqueEntityId(id : idtype, ent : Entity) : Bool
This function returns false when the entity would not be unique when given the id argument. The function returns true when the entity would be unique, but will do so only once for a given id, checking a different entity with the same id will return false in the rest of the action handling.
isUniqueEntityId(id : idtype) : Bool
This function returns false when the given id is not available for the Entity type. The function will return true only once, to cope with batch processing.
Note that these functions use one collection per entity to determine whether an id is available, so a call to isUniqueUserId(id) can influence the result of isUniqueUser(ent).
findEntity
findEntity(id : idtype) : Entity
This function returns the Entity with the given id value, null if it does not exist.
For each String property in an Entity, a find function is generated (repace Property with the property name):
findEntityByProperty
findEntityByProperty(val : String) : List<Entity>
This function returns a list of all Entitys with the exact given Property value, an empty list if there are none.
findEntityByPropertyLike
findEntityByPropertyLike(val : String) : List<Entity>
This function returns a list of all Entitys with the given Property value as substring, an empty list if there are none.
Every entity has a name, which is always a string. This name can be retrieved by the automatically generated getName() function.
The name of an entity is determined as follows:
If a property of the entity has the name annotation, the name of the entity equals this property. This property must be of type String.
If a property of the entity is called 'name' and is of type String, this property determines the entity name.
Otherwise, the id of the entity (converted to its string-value) is used.
A typical scenario where these functions come in handy is a create/edit page for an entity. In the following example the isUniquePage function is used to verify that the new page has a unique identifier property:
entity Page {
identifier :: String (id, validate(isUniquePage(this), "Identifier is taken")
}
define page createPage(){
var p := Page{}
form{
label("Identifier"){input(p.identifier)}
action("save",save())
action save(){
p.save();
message("New page created.");
return home();
}
}
}
You can quickly generate basic pages for creating, reading, updating and deleting entities using derive crud -entityname-. It will create pages that allows creating and deleting such entities, and editing of all entities of this type in the database.
Example:
application test
entity User {
username :: String
}
derive crud User
//application global var
var u_1 := User{username:= "test"}
define page root(){
navigate(createUser()){ "create" } " "
navigate(user(u_1)){ "view" } " "
navigate(editUser(u_1)){ "edit" } " "
navigate(manageUser()){ "manage" }
}
As the navigates indicate, the pages that are created are:
view:
define page entity(arg:Entity){...}
create:
define page createEntity(){...}
edit:
define page editEntity(arg:Entity){...}
manage (delete):
define page manageEntity(){...}
These pages are particularly useful when you're just constructing the domain model, because the generated pages are usually too generic for a real application.