Manipulate an existing database.

47 views
Skip to first unread message

Lin Jen-Shin

unread,
Jul 27, 2008, 5:40:40 AM7/27/08
to DataMapper, god...@godfat.org
Greetings,

DataMapper looks very promising for me, so I am thinking of
using it in the near future. I hate separate my domain objects into
two parts in Rails, writing migration and switching to ActiveRecord,
vice versa, is very annoying to me.

But there's a very convenient feature to me in ActiveRecord,
that is ActiveRecord automatically mapping all fields in a table.
It makes me easily control an existing database without any domain
object.

For example,

require 'active_record'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'db/development.sqlite3'
)

clsas User < ActiveRecord::Base
end

User.find 1
=> #<User id: 1, account: "admin", created_at: "2008-05-18
20:08:37", etc.>

Some people would use database admin such as phpMyAdmin to
accomplish this kind of task, but I prefer anything in Ruby,
calling Ruby function, manipulating data without SQL and
any domain object. (i.e. I didn't have to load up entire environment.)

In DataMapper, I didn't find an easy way to accomplish this.
I am sorry if there's one but I didn't find it, please point out,
many thanks. In short, I would like to do this in DataMapper:

class User
include DataMapper::Resource
mapping :account, :created_at
end

or

class User
include DataMapper::Resource
mapping All
end

class User
include DataMapper::ResourceAll
end

or

class User
include DataMapper::Resource
mapping *storage_fields
end

The above User.storage_fields should return an Array,
telling all the fields in the table, e.g.
[:account, :created_at, :etc]
or a Hash includes data type, e.g. {:account => String,
:created_at => DateTime}
then mapping *storage_fields should change to:

mapping *storage_fields.each_key.to_a

If it's possible, a feature returning the database schema as well:

DataMapper.repository.storages
# => [:users, :posts, :etc]

DataMapper.repository.storages_and_fields
# => {:users => {:account => String},
:posts => {:title => String, :content => Text}}

or returning DataObject::Field, DataObject::Storage, etc.

DataMapper.repository.storage
# => [#<DataObject::Storage @name='users' @fields=
[#<DataObject::Field @name='account' @type=String>]>]

If you feel this kind of feature is indeed needed or not bad for
adding it, I could try to provide a patch for it. Though I didn't
read the source code deeply, not knowning it's easy or not.

sincerely,

Michael Klishin

unread,
Jul 27, 2008, 6:33:39 AM7/27/08
to datam...@googlegroups.com
2008/7/27 Lin Jen-Shin <god...@gmail.com>:

> If you feel this kind of feature is indeed needed or not bad for
> adding it, I could try to provide a patch for it. Though I didn't
> read the source code deeply, not knowning it's easy or not.
>
> sincerely,

I think it would be useful and a good fit for a plugin. Should not be
too hard to implement, too. So go for it ;)
--
MK

Lin Jen-Shin

unread,
Jul 27, 2008, 12:30:38 PM7/27/08
to DataMapper, godfat 真常
On Jul 27, 6:33 pm, "Michael Klishin" <michael.s.klis...@gmail.com>
wrote:
> I think it would be useful and a good fit for a plugin. Should not be
> too hard to implement, too. So go for it ;)
> --
> MK

I was not familiar with SQL, and I found that it was database
dependent.
Anyway, I have just created a project *dm-mapping* for testing the
thought.

http://github.com/godfat/dm-mapping

Though currently only sqlite3 is supported,
feel free to fork it or use it in somewhere.
(sql codes were borrowed from activerecord 2.1)

Here's the updated thought of usage:

== SYNOPSIS:

require 'dm-core'

# setup first, or dm-mapping can't determine which adapter it should
load up
DataMapper.setup :default, 'sqlite3://db/development.sqlite3'

require 'dm-mapping'

class User
include DataMapper::Resource
mapping /*/
mapping DataMapper::Mapping::All
mapping /_at$/, /^salt_/
mapping :account, :email
mapping *storage_fields
end

DataMapper.repository.storages
# => ['users']

User.storage_fields
# => ['account', 'email', 'created_at', 'salt_first', 'salt_second']

DataMapper.repository.fields('users')
# => ['account', 'email', 'created_at', 'salt_first', 'salt_second']

DataMapper.repository.storages_and_fields
# => {'users' => ['account', 'email', 'created_at', 'salt_first',
'salt_second']}


Thanks for your listening,
Reply all
Reply to author
Forward
0 new messages