(Quick Reference)

3 Mapping Domain Classes to DynamoDB - Reference Documentation

Authors: Roman Stepanenko

Version: 0.1

3 Mapping Domain Classes to DynamoDB

Mapping to AWS Tables

The way GORM for DynamoDB works is to map each domain class to a AWS DynamoDB domain. For example, given a domain class such as:

class Person {
    String id
    String firstName
    String lastName
    static hasMany = [pets:Pet]

static mapWith = "dynamodb" }

The plugin will map Person class to a DynamoDB table called "Person". By default the table name will be the class name, however it can be explicitly specified:

class Person {
    String id
    String firstName
    String lastName
    static hasMany = [pets:Pet]

static mapWith = "dynamodb" static mapping = { table 'PEOPLE' } }

In this case Person class will be mapped to 'PEOPLE' DynamoDB table.

Please note: if you specified a table name prefix in the configuration, all table names will be prefixed . For example, if you specified

grails {
    dynamodb {
        accessKey = '...'
        secretKey = '...'
        tableNamePrefix = 'DEV_' //optional, used when the same AWS account is shared between more than one environment
    }
}
then resulting table name will be DEV_Person for first example and DEV_PEOPLE for second example.

Mapping to AWS Attributes

By default, each java property will be mapped as an identically named AWS DynamoDB attribute. For example, given a domain class such as:

class Person {
    String id
    String firstName
    String lastName

static mapWith = "dynamodb" }

will result in the following attribute names in 'Person' domain:

  • firstName
  • lastName

id field is always mapped to itemName() for the record representing this domain class instance.

It is possible to specify custom AWS attribute names for each java attribute:

class Person {
    String id
    String firstName
    String lastName

static mapWith = "dynamodb" static mapping = { firstName key:'FIRST_NAME' lastName key:'LAST_NAME' } }

3.1 IdentityGeneration

The plugin works only with String identifiers. Assignment and generation of ids is done automatically, however the String id field must be currently explicitly declared in the domain class:

class Person {
    String id
    String firstName
    String lastName

static mapWith = "dynamodb" }

By default, generated ids are generated with java.lang.UUID. It is also possible to use hilo numeric value generator (please note that the id field must still be declared as String):

class Person {
    String id
    String firstName
    String lastName

static mapWith = "dynamodb"

static mapping = { id_generator type: 'hilo', maxLo: 500 } }

3.2 Provisioned Throughput

DynamoDB gives you complete control of the performance characteristics for each table via read and write throughput provisioning. Complete details can be found at Amazon DynamoDB documentation

Developer can specify read or write (or both) throughput on a per-domain class basis using the following syntax:

class Person {
    String id
    String firstName
    String lastName

static mapWith = "dynamodb" static mapping = { throughput read:4, write:6 //optional, if not specified default values will be used } }

The implementation of plugin will use specified values, or will fall back to default read and write throughput values specified in the configuration options.