Is it possible with datamapper to generate the models from an existing database schema? So, to do the inverse of a migration, which takes the models and generate the sql. What I want is given a database schema generate the models.
try to check https://github.com/yogo/dm-reflection or any of its forks ..
At last, I found that till now the best solution is to use dm-is-reflective plugin: https://github.com/godfat/dm-is-reflective.
It doesn't generate the code for DataMapper models reflecting an existing database schema, but its properties access methods are automatically available (as long as you keep using this plugin, of course).
Here is an example of use:
require 'data_mapper'
require 'dm-is-reflective'
DataMapper.setup(:default, "postgres://user:pwd@localhost/db")
class Table
include DataMapper::Resource
is :reflective #activate dm-is-reflective
reflect #reflects eeach property. You can be more specific (look at plugin documentation)
end
DataMapper.finalize
#Even if no field is defined, all of them are accessible
entry = Table.first(nil, {:id => 469})
print entry.anotherField
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With