[Type] Collection

Part of: mobl
Inherits from: mobl::Object

A Collection represents a (often) virtual collection of items. These collections can be filtered, sorted and truncated.

Syntactic sugar

There is a nicer, more SQL syntax that translates down to the methods defined below. The syntax in BNF-like format looks as follows:

Exp    ::= Exp Filter+
Filter ::= "where" SetExp
Filter ::= "order" "by" OrderExp
Filter ::= "prefetch" {ID ","}+
Filter ::= "limit" Exp
Filter ::= "offset" Exp
SetExp ::= ID "==" Exp
         | ID "!=" Exp
         | ID "<" Exp
         | ID "<=" Exp
         | ID ">" Exp
         | ID "in" Exp
         | ID "not" "in" Exp
OrderExp ::= ID | ID "asc" | ID "desc"

Example:

var coll = Task.all() where done == true
                      order by date desc
                      prefetch category
                      limit 10
                      skip 10;

Instantiation

A new Collection can be created by using the Collection<T>() constructor.

Example:

var coll = Collection<Task>();

Instance methods

one() : T

Returns one element of the collection (or null if the collection is empty).

Example:

var oneTask = Task.all().one()

prefetch(property : String) : Collection<T>

Returns a new collection that prefetches the given property. In effect performs a join with the property's table (only works for reference properties).

Example:

list(t in Task.all().prefetch("category")) {
  "[" label(t.category.name) "]"
  label(t.name)
}

filter(property : String, op : String, value : Object) : Collection<T>

Returns a new collection that filters based on a property. Supported operators are: =, !=, <, >, <=, >=, in and not in.

Example:

var coll = Task.all().filter("done", "=", true);

order(property : String, ascending : Bool) : Collection<T>

Returns a new collection that is sorted based on a property in either ascending (ascending is true) or descending (ascending is false).

Example:

var coll = Task.all().order("date", false);

reverse() : Collection<T>

Returns a new collection that reverses its result set.

Example:

var coll = Task.all().order("date", true).reverse();

destroyAll() : void

Removes all elements in the collection from the database.

Example:

Task.all().filter("date", "<", old).destroyAll();

count() : Num

Returns the number of items in the collection.

Example:

Task.all().count()

list() : Array<T>

Turns the collection into an array.

Example:

var ar = Task.all().list()

selectJSON(properties : [String]) : JSON

Returns a JSON representation of a subset of a collection. The properties argument 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

limit(n : Num) : Collection<T>

Returns a collection that limits the number of results to n.

Example:

Task.all().limit(10)

skip(n : Num) : Collection<T>

Returns a collection that skips the first n elements, for instance to implement paging.

Example:

Task.all().limit(10).skip(10) // page 2

add(item : T) : void

Adds item item to the collection.

Example:

coll.add(Task(name="Groceries"));

remove(item : T) : void

Removes item item from the collection (but does not remove the item itself).

Example:

coll.remove(t);
mobl/collection.txt · Last modified: 2013/10/01 02:28 (external edit)