anlek / mongify

Mongify allows you to map your data from a sql database and into a mongodb document database.
http://github.com/anlek/mongify
MIT License
317 stars 82 forks source link

Cannot create key for embedded document #134

Closed stoyanmkd closed 7 years ago

stoyanmkd commented 7 years ago

It's my translation file.

table "user" do
    column "idUser", :key, :as => :integer
    column "User_GUID", :string
    column "User_ID", :string
    column "Server", :string
    column "System", :string
end

table "device", :embed_in => 'user', :on => 'idUser' do
    column "idDevice", :key, :as => :integer
    column "Identifier", :string
    column "applicationID", :string
    column "idUser", :integer, :references => "user"
end

table "message" do
    column "idNotification", :key, :as => :integer
    column "idDevice", :integer, :references => "device"
    column "Message", :string
    column "idStatus", :integer
    column "Type", :string
    column "Datetime", :datetime
end

I would like to embed "device" table in "user" table, but still to keep the reference from "device" to "message". When I process this translation file, the key for the 'device' disappears in the converted Mongo database. Any idea how to fix it?

Best Regards, Stoyan

anlek commented 7 years ago

@stoyanmkd you can use the before_save method to save the original id (which would be under row.pre_mongify_id). Check out the doc regarding before save.

Basically, you're looking at doing something like this:

table "device", :embed_in => 'user', :on => 'idUser' do
    column "idDevice", :key, :as => :integer
    column "Identifier", :string
    column "applicationID", :string
    column "idUser", :integer, :references => "user"
        before_save do |row, user_row|
            row.original_id = row.pre_mongify_id
        end
end