CRUD Operations in Gaelyk- Part 1- Create Operation

We have seen here and here on creating simple mashup using Gaelyk. In this series of articles lets see how we can implement CRUD operations in Gaelyk.

CRUD Operations in Gaelyk would involve:

  • Create an entity
  • Read an entity
  • Update an entity
  • Delete an entity

In this article we will look at creating an entity in the datastore.

Create of CRUD operations

Google App Engine supports both NoSQL and SQL based database. The NoSQL counterpart uses BigTable datastore. And we would be using the same for our CRUD operations.

The data in BigTable is held in Entity where each entity has some fields, associated types and values for each field. It also adds a Key to each data row in the entity.

We will look at the details of the datastore as we proceed further, but lets setup the Gaelyk project first. The easiest way is to download the template project from here and extract it to some directory. Lets call this directory as PROJECT_HOME. One can run the command gradlew gaeRun to deploy the template project into the embedded Jetty server and access it from http://localhost:8080/.

In this series of articles we would be focused on a Book entity which has the following fields: isbn, title, authors, publisher, number of pages. To create an entity and store in the database we need to create a form for data entry, let go ahead and create a newBook.gtpl in PROJECT_HOME/src/main/webapp/WEB-INF/pages folder:

<% include '/WEB-INF/includes/header.gtpl' %>
<form action="/book" method="POST">
  <div class="row">
    <div class="span5">
      <label for="isbn"/>ISBN</label>
      <input id="isbn" type="text"

      <label for="title"/>Title</label>
      <input id="title" type="text"

      <label for="authors"/>Author(s)</label>
      <input id="authors" type="text"

      <label for="publisher"/>Publisher</label>
      <input id="publisher" type="text"

      <label for="numberOfPages"/>Number of Pages</label>
      <input id="numberOfPages" type="text"
  <div class="row">
    <div class="span4">
    <input type="submit" value="Save">
    <input type="reset" value="Reset">

<% include '/WEB-INF/includes/footer.gtpl' %>

The template project uses bootstrap and therefor the styling aspects would be taken care! Now that we have a form to enter the data, we would need to setup up the route to access this page, that way we will have clean urls and not ending with .gtpl or .groovy and so on. The routes.groovy file handles all the routing information and is present in PROJECT_HOME/src/main/webapp/WEB-INF folder. Lets add the following entry to the routes.groovy file

get "/book/new", forward: "/WEB-INF/pages/newBook.gtpl"

the above code says that when there is a GET request for ‘/book/new’, forward the request to the newBook.gtpl which renders the html markup for the page. On running the application locally using gradlew gaeRun and navigating to http://localhost:8080/book/new you should be able to see your page as something like:

Wait! we havent still configured the routes for Save operations, lets go ahead of do that. Lets create a groovy script addBook.groovy in the PROJECT_HOME/src/main/webapp/WEB-INF/groovy folder where all the groovy scripts(groovlets) are to be added.

def entity = new Entity("book")
entity << params
request.operation = "saved"
request.savedTitle = params.title

forward '/WEB-INF/pages/success.gtpl'

params is a map which contains the data submitted by the form as key-value pairs. One can inspect the data by logging params.isbn or params.title or any of the params.fieldName data on to the console. Just in case someone wants to try use params.title in the above groovy script.

entity << params

the above code appends the key-value pairs in the params object to the entity object we have created and then save the contents on the entity object to the datastore. After saving the entity we forward to another page- success.gtpl which is created in PROJECT_HOME/src/main/webapp/WEB-INF/pages folder.

<% include '/WEB-INF/includes/header.gtpl' %>
<div class="row">
    <p>Successfully <%=  request.operation %> the
       book <%= request.savedTitle %></p>
    <p><a href="/book/new">Add another book</a></p>

<% include '/WEB-INF/includes/footer.gtpl' %>

We are yet to setup the route information for the groovy script we created to add the books. So go back to routes.groovy and add the following to the routes.groovy

post "/book", forward: "/addBook.groovy"

which says, that any POST requests for /book should be forwarded to addBook.groovy script/groovlet. So save all the files and reload the URL http://localhost:8080/book/new and add some entries to the form and click on “Save” button and you should be able to see the “success” page.

Google App Engine provides a way to access the entities/data stored in the datastores by navigating to: http://localhost:8080/_ah/admin/datastore.

also read:

In the next article lets see how we can list all the entities we have stored in the datastore.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!