Zero Resource Model Overview
Contents
This article contains the following information:
Objectives
The Zero Resource Model (ZRM) provides the following function:
- Enables full resource support (create/read/update/delete) with minimal manual coding
- Simplifies the support of common formats, including feeds
- Enables rapid development of client (AJAX) applications that interact with ZRM-based resources
- Encourages RESTful application design principles
ZRM uses a simple REST model to enable developers to focus on the application and not persistence details. ZRM does not introduce a general purpose ORM. For example, the ZRM data model is geared toward REST, not an arbitrary POJO model, by asserting fundamental persistence assumptions.
Supported databases
ZRM supports the following databases:
- DB2 v9.5: Z, Linux, Unix, Windows
- Derby v10.3.2.1 Network and Embedded
- MySQL 5
- Oracle 10g
- Microsoft SQL Server 2005
Getting started
Configuring ZRM includes the following activities:
- Database configuration
- Resource model declaration
- Resource event handler configuration
You can find an example ZRM application in the employee.resource.demo module in the samples. You can also follow the Zero Resource Model quick start tutorial or the Using Zero Resource Model to develop a data-to-Web application (zero.suggest.demo) tutorial to build a simple application.
Resource model declaration
The Resource model declaration is a collection of resource definitions. It contains the definitions for fields, constraints, and filtered collections among other optional configuration. The resource definitions are datastore independent and can be shared across applications. They can be created and manipulated either declaratively or programmatically or using HTTP. The following example shows a basic model declartion:
// Resource Model declaration
// File: /app/models/employees.json
{
"fields" : {
"name": {"type": "string", "max_length":50},
"birthdate": {"type": "date"},
"state": {"type": "string", "format":"region"},
"phone": {"type": "string", "format":"phone"}
}
}
Programmatic Model API
The Model API enables accessing resources using collection-like constructs. In addition to the basic CRUD programming model, ZRM introduces a list-function, the LCRUD model. The list function enables listing and filtering collection members using simple conditions (for example, published_between: ['01-01-2003'..'12-31-2007']). A filter can be applied to a collection to create a new collection that is a subset of the original. The filter conditions can be passed in dynamically, therefore eliminating the need for writing a different service for each subset of the data that the client might need.
The following example shows some of the retrieval APIs using Groovy:
import zero.resource.TypeCollection
...
def employees = TypeCollection.retrieve('employees')
def allEmployees = employees.list()
def employee = employees.retrieve(1)
def someEmployees = employees.list(firstname__contains: 'e')
HTTP REST API
The Zero Resource Model HTTP REST API is symmetrical to the Model API. The default result rendering is for JSON but other formats, like Atom, can also be specified.
The following example shows automatic valid URIs to RESTful resources:
http://localhost:8081/resources/employees http://localhost:8081/resources/employees/1 http://localhost:8081/resources/employees?firstname__contains=e
The HTTP access is not enabled by default. It can be enabled by introducing a resource event handler that, in its simplest form, delegates the HTTP requests to a system-provided ZRM handler with only one line of application code.
CLI Tasks
The zero.resource module contributes several Command Line Interface (CLI)
tasks (ZRM CLI tasks) for the lifecycle development of ZRM data models. Through these tasks you
can create necessary database artifacts, load data into the database, and reset
the database to a predictable state.