Gorm default configurations in config.groovy

24 / Sep / 2010 by Uday Pratap Singh 3 comments

Recently while working on my new Project I have the requirement where each domains need to have dateCreated and lastUpdated fields. So rather than adding these fields to each of the domain class we added them to template. Now whenever we create a domain class these two fields are added to it automatically.
Next thing we want to add default constraints(nullable:true) to these two fields. We could do that in templates as well, I dont think anything wrong with this approach but my friend HIMANSHU told me a better way to do that by adding it to Config.groovy i,e;.

grails.gorm.default.constraints = {
    'dateCreated'(display: false, nullable: true)
    'lastUpdated'(display: false, nullable: true)
}

In my project we are using Multi-tenant plugin as well and we want to have an index on tenantId field to make the retrieval faster. Again we could do that in the template as well but this time I go with the COC Rule of grails and tried the following code in Config.groovy

grails.gorm.default.mapping = {
    'tenantId' column: 'tenant_id', index: 'tenant_id_idx'
}

And it worked perfectly :). There are really so many good things about GRAILS that we need learn .

Hope it helps
Uday Pratap Singh
uday@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Gregg Bolinger

    Ok, thanks for clearing that up. I’ve been meaning to look into a way to do something like static stats = true that would inject those properties, instead of having them in each domain. But part of me thinks I’d prefer to see them.

    Reply
  2. Uday Pratap Singh

    Hello Gregg, thanks for reminding me that I should be more clear about the terms I used.
    When you do grails create-domain-class, Grails gives you its own implementation of domain classes or you can say Grails have its own template which directs it how the domain class should look like. You can change how the domain class should look like. For this we need to do “grails install-template”. This command creates the templates for scaffolding code and for artifacts like controllers, domain ,taglib etc. Now you can modify these templates according to your needs. After the modification to any artifact or scaffolding code, the new artifacts or scaffolding would be generated from the updated template.
    For more information you can see http://www.grails.org/Artifact+and+Scaffolding+Templates

    I hope it will be much helpful now

    Reply
  3. Gregg Bolinger

    What do you mean you added them to a template? Do you mean an abstract domain you are inheriting? Because that’s how I’ve always done it. If not, can you please share?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *