Creating data models from an existing database

Is it possible with datamapper to generate models from an existing database schema? So, to do the reverse migration, which takes models and generates sql. I want the database schema to generate models.

+3
source share
2 answers

Finally, I found that so far the best solution is to use a dm-is-reflectiveplugin: https://github.com/godfat/dm-is-reflective .

It does not generate code for DataMapper models that reflects the existing database schema, but its methods for accessing properties are automatically available (if you continue to use this plugin, of course).

Here is a usage example:

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
+1
source

All Articles