My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
UsingTheDatastore  
modeled after http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html
Tutorial, appengine_0.0.6
Updated Dec 10, 2009 by brian.gi...@gmail.com

Using the Datastore

Storing data in a scalable web application can be tricky. A user could be interacting with any of dozens of web servers at a given time, and the user's next request could go to a different web server than the one that handled the previous request. All web servers need to be interacting with data that is also spread out across dozens of machines, possibly in different locations around the world.

Thanks to Google App Engine, you don't have to worry about any of that. App Engine's infrastructure takes care of all of the distribution, replication and load balancing of data behind a simple API—and you get a powerful query engine and transactions as well.

A Complete Example Using the Datastore

Here is a new version of guestbook.rb that stores greetings in the datastore. The rest of this page discusses the new pieces.

require 'sinatra'
require 'dm-core'
require 'appengine-apis/users'

DataMapper.setup(:default, "appengine://auto")

helpers do
  include Rack::Utils
  alias_method :h, :escape_html
end

class Greeting
  include DataMapper::Resource
  property :id, Serial # required for DataMapper
  property :author, User # <-- NOTE: This stores the whole object, not the id
  property :content, Text
  property :date, Time, :default => lambda { |r, p| Time.now } # must be a Proc  
end

get '/' do
  greetings = Greeting.all(:order => [:date.desc], :limit => 10)

  response = greetings.inject('<html><body>') do |html, greeting|
    html <<
    "<p>On #{greeting.date}, " <<
    "<b>#{greeting.author.nil? ? 'an anonymous person' : greeting.author.nickname}</b> wrote:</p>" <<
    "<blockquote>#{h greeting.content}</blockquote>"
  end

  response <<
     '<form action="/sign" method="post">
        <div><textarea name="content" rows="3" cols="60"></textarea></div>
        <div><input type="submit" value="Sign Guestbook"></div>
      </form>
    </body>
  </html>'
end

post '/sign' do
  greeting = Greeting.new(:content => params[:content])

  if AppEngine::Users.current_user
    greeting.author = AppEngine::Users.current_user
  end

  greeting.save
  redirect '/'
end

Replace guestbook.rb with this, then reload http://localhost:8080/ in your browser. Post a few messages to verify that messages get stored and displayed correctly.

Next...

We now have a working guest book application that authenticates users using Google accounts, lets them submit messages, and displays messages other users have left. Because App Engine handles scaling automatically, we will not need to revisit this code as our application gets popular.

This latest version mixes HTML content with the code for the handler. This will make it difficult to change the appearance of the application, especially as our application gets bigger and more complex. Let's use templates to manage the appearance, and introduce static files for a CSS stylesheet.

Continue to UsingTemplates.


Sign in to add a comment
Powered by Google Project Hosting