[Type] Entity

Part of: mobl
Inherits from: mobl::Object

Entity is the implicit super type of every entity type.

Static methods

all() : Collection<T>

Returns a Collection of all objects of this entity type.

Example:

list(t in Task.all()) {
  ...
}

load(id : String) : T

Load a particular object by id.

Example:

var t = Task.load("89DE7F6C7C764A7186A16450C9635A50");

findBy(property : String, value : String) : T

Load an object by property and value. Returns null if no object matches.

Example:

var t = Task.findBy("name", "Groceries");

search(query : String) : Collection<T>

Performs a full-text search on all entity properties marked with the (searchable) annotation. Matches full words only, unless the * is used.

Example:

entity Task {
  name : String (searchable)
}
screen root() {
  list(t in Task.search("groc*")) {
    ...
  }
}

searchPrefix(query : String) : Collection<T>

Same as search() except that * is automatically added to the end of each word in the query.

Example:

entity Task {
  name : String (searchable)
}
screen root() {
  list(t in Task.searchPrefix("groc")) {
    ...
  }
}

fromSelectJSON(json : JSON) : T

Creates a new entity object based on the data in the JSON object.

Example:

var t = Task.fromSelectJSON(json);
add(t);

Instance methods

fetch(prop : String) : T

Fetches the object referred to in the prop property, if it has not been fetch already. This is required when this value has not been prefetched.

Example:

t.category.name // -> Exception, entity not fetched
t.fetch("category");
t.category.name // -> "Main category"

toJSON() : JSON

Returns a JSON representation of this entity instance.

Example:

var t = Task(name="Task", done=false);
t.toJSON() // -> {"name": "Task", done: false}

selectJSON(properties) : JSON

Returns a JSON representation a defined set of attributes. The properties arguments expects an array with property names. Some examples:

  • ['id', 'name'], will return an object with the id and name property of this entity
  • ['*'], will return an object with all the properties of this entity, not recursive
  • ['project.name'], will return an object with a project property which has a name property containing the project name (hasOne relationship)
  • ['project.[id, name]'], will return an object with a project property which has an id and name property containing the project name (hasOne relationship)
  • ['tags.name'], will return an object with an array tags property containing objects each with a single property: name

Example:

var t = Task(name="Task", done=false);
var c = Category(name="Category");
t.category = c;
t.selectJSON(["name", "category.[id,name]"])
  // -> {"name":"Task","category": {"name":"Category"}}
mobl/entity.txt · Last modified: 2013/10/01 02:28 (external edit)