How to define custom table mapping by Config.groovy entry in Grails

251 Views Asked by At

I'm developing a plugin that needs a specific index configuration in table mapping.

static mapping = {
    myProperty index:'myProperty_Idx'
}

Is it some way to let plugin users decide if they want to use or not this mapping via Config.groovy file?

1

There are 1 best solutions below

0
On

I believe you can read the config variables right from your mapping block.

So this line in the app consuming your plugin's Config.groovy

grails.myplugin.useIndexForFoo = true

Should allow you to have a configurable domain class, such as

class Foo
{
    String myString

    static mapping = {
        if (Holders.config?.grails?.myplugin?.useIndexForFoo)
            myString index: "myString_idx"
    }
}

Note I have used Holders rather than injecting grailsApplication bean because mapping config is static - do not know if this is optimal or not