5.3.8 TableMapping and FieldMapping

A TableMapping describes the mapping of a domain object in the application to a table stored in the database. A default table mapping is one which maps each column in a table to a field of the same name.

TableMapping = {
  String table                  :  "" ,
  String database               :  "" ,
  boolean mapAllColumns         : true,
  Array fields                  : null

The table and data members are the names of the table and database, respectively. mapAllColumns, if true, creates a default FieldMapping for all columns not listed in fields, such that that all columns not explicitly mapped are given a default mapping to a field of the same name. fields holds an array of FieldMapping objects;this can also be a single FieldMapping.

A FieldMapping describes a single field in a domain object. There is no public constructor for this object; you can create a FieldMapping using TableMapping.mapField(), or you can use FieldMapping literals can be used directly in the TableMapping constructor.

FieldMapping = {
  String fieldName     :  "" ,
  String columnName    :  "" ,
  Boolean persistent    : true,
  Converter converter     : null

fieldName and columnName are the names of the field and the column where this field are stored, respectively, in the domain object. If persistent is true (the default), the field is stored in the database. converter specifies a Converter class, if any, to use with this field (defaults to null). };

The TableMapping constructor can take either the name of a table (possibly qualified with the database name) or a TableMapping literal.

TableMapping mapField(String fieldName, [String columnName], [Converter converter], [Boolean persistent])

Create a field mapping for a named field of a mapped object. The only mandatory parmeter is fieldName, which provides the name a field in a JavaScript application object. The remaining parameters are optional, and may appear in any order. The cyrrent TableMapping object is returned.

columnName specifies the name of the database column that maps to this object field. If omitted, columnName defaults to the same value as fieldName. A converter can be used to supply a Converter class that performs custom conversion between JavaScript and database data types. The default is null. persistent specifies whether the field is persisted to the database, and defaults to true.


If persistent is false, then the columnName and converter parameters may not be used.

TableMapping applyToClass(Function constuctor)

Attach a TableMapping to a constructor for mapped objects. After this is done, any object created from the constructor will qualify as a mapped instance, which several forms of the relevant Session and Batch methods can be used.

For example, an application can construct an instance that is only partly complete, then use Session.load() to populate it with all mapped fields from the database. After the application modifies the instance, Session.save() saves it back. Similarly, Session.find() can take the mapped constructor, retrieve an object based on keys, and then use the constructor to create a fully-fledged domain object.