In my current project, I was given a task to bootstrap some data for our application. Initially I was thinking of doing it in the old fashion way, that is, writing a full blown CREATE data code for data creation like
Book myBook=new Book(name:’MyBook’, publication:’lorem’,price:’200′)
Author author1=new Author(name:’Partho’,country:’India’)
and so on.
But one of my colleagues(thank you Himanshu 🙂 ) introduced me to this great plugin called fixtures for writing and creating the bootstrap data in more efficient and clean manner. It is really neat and intuitive. Fixture data is defined using a simple DSL that expresses objects, their properties and their relationships.A sample for how data is defined in fixtures is given below
books=[myBook];//That’s how you create relationships in it.
No need to create multiple functions or services for bootstrapping. Just use your fixture files in your bootstrap.groovy (process explained below) or use your code inline. For data loading, we use a bean called fixtureLoader. An example would be
which helps you use a fixture file for bootstrapping.
For more information, please check out the fixture documentation over here and you can download the plugin from here. Its a really great plugin if you want your data to be loaded from anywhere in your application (bootstrap,tests,etc.). Do share your experience or anything cool you find out while using this plugin.Ciao