Helma Logo
main list history

Creating collections programmatically

Helma 1.7 introduces a much requested feature to create HopObject collections programmatically and on the fly, without having to define them in the prototype's type.properties file.

HopObject constructors now have a new static getCollection() method that takes a single JS object argument. The argument contains the collection properties as you would normally define them in the _children section of the type.properties file, e.g.:

var c = Page.getCollection({
    order: "name",
    filter: "id > 10",
};

You can also specify the type of contained objects using the "collection" property, so the following collection is equivalent to the one defined above:

var c = HopObject.getCollection({
    collection: "Page",
    order: "name",
    filter: "id > 10",
};

Note that for "nested" properties such as group.order or local.1 you have to use quoted "flat" properties, not nested objects:

var c = Page.getCollection({
    group: "author",
    "group.order": "author",
    "group.prototype": "AuthorGroup"
});

Since I thought it would be handy, I also implemented "limit" and "offset" collection properties to implement easy pagination:

So to fetch pages 11-20, you would do something like this:

var q = Page.getCollection({
    limit: 10,
    offset: 10
});

Note that "limit" is just an alias for "maxSize" introduced to be more consistent with the underlying SQL syntax.

This feature is currently implemented and known to work on MySQL, Postgresql, and Oracle.

Links to this page: HopObject Query Object, Helma 1.7.0 Changelog, Helma 1.7.0 Released