Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I separate finalizing into different databases in DataMapper?

Currently in my Sinatra + DataMapper app, I have:

require 'data_mapper'

DataMapper.setup(:default,  "sqlite3://#{Dir.pwd}/main.db")
DataMapper.setup(:comments, "sqlite3://#{Dir.pwd}/comments.db")

class Recording
    include DataMapper::Resource

    # ...

    belongs_to :user
    has n, :comments
end

class User
    include DataMapper::Resource

    # ...

    has n, :recordings
end

class Audience
    include DataMapper::Resource

    # ...
end

# -------- ITS OWN DATABASE --------
class Comment
    include DataMapper::Resource

    #...

    belongs_to :recording
end

I want the Comments class to go separately from the others into comments.db. I was looking around, and I saw something like this (and to which I have formatted to my situation):

# -------- ITS OWN DATABASE --------
repository(:comments) do 
    class Comment
        include DataMapper::Resource

        #...

        belongs_to :recording
    end
end

Would this work out as planned, or is there a proper way to do this?

like image 663
Imnotanerd Avatar asked Mar 05 '26 15:03

Imnotanerd


1 Answers

We override the #default_repository_name method on our models to do this:

class Comment
  include DataMapper::Resource

  def self.default_repository_name
    :comments
  end
end
like image 85
d11wtq Avatar answered Mar 07 '26 06:03

d11wtq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!