Field Types

Dari stores data as a Map<String, Object> as the object’s State, using the formats described below.

Note

Class types (Boolean, Integer, Double, etc.) default to null. Primitive types default to a value. For example, a boolean field defaults to false. Therefore, primitives consume more memory than uninitialized object types.

String

The following field types are stored as a String: enumerations, URL, UUID, URI.

Number

The following field types are stored as an implementation of a Number type: Integer/int, Double/double, Long/long, Float/float, Short/short, Byte/byte.

Boolean

The following field types are stored as Boolean: Boolean/boolean.

Date

Dates are represented as java.util.Date objects in memory. In the database, dates are stored in epoch time, for example, 1490673600000.

StorageItem

StorageItems are Dari’s implementation for uploading, processing, and delivering files. This class provides abstraction functions for easily representing files as JSON objects. For example, the following snippet describes a file residing on local storage.

{
  "file": {
    "storage": "Local file storage",
    "path": "/Users/birdArtist/drawings/woodchuck.png",
    "contentType": "image/png",
    "metadata": null
  }
}

For a detailed explanation of StorageItems and working with files, see File Storage.

Locale

Use java.util.Locale for storing Locale data. The value gets stored as text, for example:

{
  "locale": "en-US"
}

Record

Any Dari object that extends Record can be persisted in the database, and can be referenced by any other Record-derived object. A Record object can be stored as a record in the database, or it can be embedded in the record of a containing object.

In the following example, the Activity JSON object references a Project object that is stored in the database, as indicated by the project keyword. The referenced object exists as a separate record in the database.

psddev.dari.test.Activity: 0000015a-dc72-dcb9-af7b-fdfac06c0000
{
 "project" : {
   "_ref" : "0000015a-dc72-dcb9-af7b-fdfac0550000",
   "_type" : "0000015a-7bb5-d284-addf-7ff7e7c00001"
 },
 "activityDate" : 1492833600000,
 "activityType" : "Download RFP response",
 "_id" : "0000015a-dc72-dcb9-af7b-fdfac06c0000",
 "_type" : "0000015a-7bb5-d284-addf-7ff7e7c00000"
}

In the next example, the Project object is embedded in the Activity object. For more information about embedded and non-embedded objects, see Relationships.

psddev.dari.test.Activity: 0000015a-d91b-d454-ad5b-ffbf95100000
{
  "project" : {
    "code" : "bethany-47-k528",
    "desc" : "Evaluate brand message",
    "_id" : "0000015a-d91b-d454-ad5b-ffbf95170000",
    "_type" : "0000015a-7bb5-d284-addf-7ff7e7c00001"
  },
  "activityDate" : 1490673600000,
  "activityType" : "Checkout",
  "_id" : "0000015a-d91b-d454-ad5b-ffbf95100000",
  "_type" : "0000015a-7bb5-d284-addf-7ff7e7c00000"
}

Note that Query, as a Record-derived class, can be used as a field in a Model class. For more information, see Query Field.

Location

Locations are stored as Dari Location objects. The Location class is a container for a two-dimensional space, defined by latitude and longitude values. A Location object cannot be saved as a database record, but can be embedded in a Record-derived object. For example, the following JSON example represents a City object, which derives from Record. City references a Location object.

{
  "name": "Seattle",
  "location": {
    "x": 47.608013,
    "y": -122.335167
  },
  "_id": "0000015a-f87a-dc82-ab7b-ff7e42050000",
  "_type": "0000015a-f857-d85a-a7de-fe570fa70000"
}

Region

Regions are stored as Dari Region objects. The Region class is a container for a three-dimensional circular space based on a spherical or Cartesian coordinate system. A Region object cannot be saved as a database record, but can be embedded in a Record-derived object. For example, the following JSON example represents a Watershed object, which derives from Record. Watershed references a list of watersources, consisting of three Region objects.

{
     "name": "Puget Sound",
     "watersources": [{
             "polygons": [
                     [
                             [
                                     [138.92975190516853, 73.02111110999999],
                                     [138.92975190516853, 23.02111111],
                                     [105.48691475483149, 23.02111111],
                                     [105.48691475483149, 73.02111110999999],
                                     [138.92975190516853, 73.02111110999999]
                             ]
                     ]
             ],
             "circles": null,
             "x": 48.02111111,
             "y": 122.20833333,
             "radius": 25.0
     }, {
             "polygons": [
                     [
                             [
                                     [139.11691099660493, 72.61666667],
                                     [139.11691099660493, 22.61666667],
                                     [105.41253344339506, 22.61666667],
                                     [105.41253344339506, 72.61666667],
                                     [139.11691099660493, 72.61666667]
                             ]
                     ]
             ],
             "circles": null,
             "x": 47.61666667,
             "y": 122.26472222,
             "radius": 25.0
     }, {
             "polygons": [
                     [
                             [
                                     [139.71832470428922, 72.10861111],
                                     [139.71832470428922, 22.10861111],
                                     [105.68778641571079, 22.10861111],
                                     [105.68778641571079, 72.10861111],
                                     [139.71832470428922, 72.10861111]
                             ]
                     ]
             ],
             "circles": null,
             "x": 47.10861111,
             "y": 122.70305556,
             "radius": 25.0
     }],
     "_id": "0000015a-fb23-d820-a37b-fb73772e0000",
     "_type": "0000015a-fb09-d55a-a75a-fb5dffc00000"
 }

Collections (List or Set)

Collections are stored as a multiple of any of the above field types. Set prevents duplicates and order is not guaranteed. List allows duplicates and order is guaranteed.

Collections are automatically handled by Dari. No extra tables or information are needed. For example, the Person class need only reference a list of Activity objects:

import com.psddev.dari.db.Record;

public class Activity extends Record {

    @Indexed private String activityType;
    private Date activityDate;

    // Getters and Setters

}
import com.psddev.dari.db.Record;

public class Person extends Record {

    private String firstName;
    @Indexed private String lastName;
    private List activities;

    // Getters and Setters

}

Note

Indexes on collections are also supported. However, performance should be considered when adding indexes to collections. Each item in a collection results in an additional row that is written to the underlying database index tables when using a SQL database backend.

Also, be careful that your collections do not grow unbounded. Large collections can slow down retrieval of data since Dari retrieves collection data even if you don’t need it.