Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 9

[ 225 ]

Grails has a number of shortcut commands that allow us to quickly build out the
objects for our model.


In this section, we will take a whistle-stop tour through GORM. You
might like to dig deeper into both GORM and Grails yourself. You can
find further online documentation for GORM at https://grails.
github.io/grails-doc/latest/guide/GORM.html.

Building a GORM model


The grails command can be used as a shortcut to carve out GORM domain
classes. We can create a domain class for Customer by issuing the following
grails command from the GroovyDSL application directory:


grails create-domain-class Customer


This will create a stub Customer.groovy file in the grails-app/domain directory,
as follows:


class Customer {

package groovydsl

class Customer {

static constraints = {
}
}

If we add some fields to this class, we can peek into the database to see how GORM
automatically creates a table for this class:


class Customer {
String firstName
String lastName
static constraints = {
}
}

Now, if we restart Grails by issuing the grails run-app command, we can
inspect the resulting table. If we are running grails in development mode as
shown in the preceding code, we can access the H2 database console with
http://localhost:8080/dbconsole.

Free download pdf