{"id":3665,"date":"2011-04-15T00:40:14","date_gmt":"2011-04-14T19:10:14","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3665"},"modified":"2016-12-19T15:07:33","modified_gmt":"2016-12-19T09:37:33","slug":"grails-fixture-a-great-way-to-bootstrap-your-data","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-fixture-a-great-way-to-bootstrap-your-data\/","title":{"rendered":"Grails fixture, a great way to bootstrap your data"},"content":{"rendered":"<p>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<\/p>\n<p>[java]<br \/>\nBook myBook=new Book(name:&#8217;MyBook&#8217;, publication:&#8217;lorem&#8217;,price:&#8217;200&#8242;)<br \/>\nAuthor author1=new Author(name:&#8217;Partho&#8217;,country:&#8217;India&#8217;)<br \/>\nauthor.addToBooks(myBook)<br \/>\n[\/java]<\/p>\n<p>and so on.<\/p>\n<p>But one of my colleagues(thank you Himanshu \ud83d\ude42 ) 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<\/p>\n<p>[java]<br \/>\nmyBook(Book){<br \/>\n     name=&#8221;MyBook&amp;quot;;<br \/>\n    publication=&amp;quot;lorem&amp;quot;;<br \/>\n   price=&amp;quot;200&amp;quot;;<br \/>\n}<\/p>\n<p>partho(Author){<br \/>\nname=&amp;quot;Partho&amp;quot;;<br \/>\ncountry=&amp;quot;India&amp;quot;;<br \/>\nbooks=[myBook];\/\/That&#8217;s how you create relationships in it.<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>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<\/p>\n<p>[java]<br \/>\nfixtureLoader.load(&amp;quot;MyFixtureFile&amp;quot;)<br \/>\n[\/java]<\/p>\n<p>which helps you use a fixture file for bootstrapping.<\/p>\n<p>For more information, please check out the fixture documentation over here and you can download the plugin from <a href=\"http:\/\/www.grails.org\/plugin\/fixtures\">here<\/a>. 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<\/p>\n<p>Regards,<br \/>\nPartho Ghosh<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [java] Book myBook=new Book(name:&#8217;MyBook&#8217;, publication:&#8217;lorem&#8217;,price:&#8217;200&#8242;) Author author1=new Author(name:&#8217;Partho&#8217;,country:&#8217;India&#8217;) author.addToBooks(myBook) [\/java] and so on. But [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3665"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3665"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3665\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}