Friday, September 17, 2010

Ramaze and Mongo(DB and mapper): Day 2

I've read on most documents before, that when using the NoSQL world it is still better to use it with RDBMS. Correct me if I'm wrong with this one, is it better to use them together or use them apart. If you have documents or links discussing this one, leave them in the comments, I'd love to read them. Also if so,
I'd like to know more is how should I separate them? Like what tables/collections would/should be this NoSQL or SQL?

So I tried testing with MongoDB and Sequel to make them play together and it was quite easy. If you have been using Ramaze before with Sequel, just do the same thing as before then just add configurations for the Mongo (which I also forgot to write configurations on my first part).

Configuration
with both MongoDB and Sequel
require 'rubygems'
require 'ramaze'
require 'yaml'

require 'bson'
require 'mongo_mapper'
require 'sequel'
require 'erubis'

MongoMapper.database = 'mongoplay'

Ramaze.options.layouts = '/view/layout'
Ramaze.options.mode = :dev

Ramaze.options.roots = [__DIR__] # Make sure that Ramaze knows where you are

Sequel.mysql(
  'mongoplay',
  :user => 'uname',
  :password => 'pword',
  :host => localhost
)

# Acquire controllers and models and other utilities
Ramaze.acquire 'util/*'
Ramaze.acquire 'model/base.rb', 'model/*'
Ramaze.acquire 'controller/base.rb', 'controller/*'
Ramaze.acquire 'config/routes.rb'


In the previous post on this topic, I was using a messaging/posting system and in here, I added a users table on a MySQL so we could tie some posts to a user if ever they wanted to register (I won't discuss here how to register/authenticate the user for now but you could use this link if so ever, that's what I used before). So the same model would apply ordinarily. I was using the user helper here.

User model
class User < Sequel::Model
  def self.register(email, password)
    User.create :email => email, :password => encrypt(password, salt), :salt => Digest::SHA1.hexdigest("--#{Time.now.to_f}--#{email}--")
  end

...


And now to insert posts and tie them to a certain user. You can also define the key in the message model or don't and you'd still insert them the same like so:
...
  if logged_in?
    Message.create :name => user.username, :body => request[:body], :user_id => user.id
  else
    Message.create :name => request[:name], :body => request[:body]
  end
...

And I just basically did the same with the comments section.

Now, now, to pull all the messages that a certain user has made:
class UsersController < Ramaze::Controller
  map '/users'

  def index(id)
    @msgs = Message.all :user_id => id.to_i
  end
end

If ever you were using a different model, just query first in the SQL table then query in the Mongo or vice-versa. :D

1 comment:

  1. > is it better to use them together or use them apart

    I agree with those who believe that most people who go the NoSQL route due to performance concerns don't warrant it. While there *are* big-name projects that make use of NoSQL tech, they're more of the exceptions that prove the rule.

    Personally, I'd rather have safe data than be able to process it quickly. But that's not to say that the traditional RDBMSs are laggards: my biggest client processes a few thousands of hits per second, and we're nowhere near hitting the DB's ceiling.

    There've been plenty of discussions on the Intartubes regarding this exact same topic, and I've waded through my share of HN threads debating the respective points of NoSQL and RDBMS. I've thought about it myself, weighed everything I knew versus everything I wanted to accomplish. I'm pretty well-versed with most of the sides (yes, there are more than two) in this "argument", though I'm unable to regurgitate every viewpoint on command.

    I'd daresay I can answer your question for you.

    The answer, as it usually is with tech, is: It depends.

    ReplyDelete