Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

datamappify

> 数据库
Open source

Compose, decouple and manage domain logic and data persistence separately. Works particularly great for composing form objects!

333 stars0 likes3 views
WebsiteGitHub

About

Compose, decouple and manage domain logic and data persistence separately. Works particularly great for composing form objects!

Datamappify is no longer being maintained. It started off with a noble goal, unfortunately due to it being on the critical path of our project, we have decided not to continue developing it given the lack of development time from me.

Feel free to read the README and browse the code, I still believe in the solutions for this particular domain.

For a more active albeit still young project, check out Lotus::Model.


Datamappify

Compose, decouple and manage domain logic and data persistence separately. Works particularly great for composing form objects!

Overview

The typical Rails (and ActiveRecord) way of building applications is great for small to medium sized projects, but when projects grow larger and more complex, your models too become larger and more complex - it is not uncommon to have god classes such as a User model.

Datamappify tries to solve two common problems in web applications:

  1. The coupling between domain logic and data persistence.
  2. The coupling between forms and models.

Datamappify is loosely based on the Repository Pattern and Entity Aggregation, and is built on top of Virtus and existing ORMs (ActiveRecord and Sequel, etc).

There are three main design goals:

  1. To utilise the powerfulness of existing ORMs so that using Datamappify doesn't interrupt too much of your current workflow. For example, Devise would still work if you use it with a UserAccount ActiveRecord model that is attached to a User entity managed by Datamappify.
  2. To have a flexible entity model that works great with dealing with form data. For example, SimpleForm would still work with nested attributes from different ORM models if you map entity attributes smartly in your repositories managed by Datamappify.
  3. To have a set of data providers to encapsulate the handling of how the data is persisted. This is especially useful for dealing with external data sources such as a web service. For example, by calling UserRepository.save(user), certain attributes of the user entity are now persisted on a remote web service. Better yet, dirty tracking and lazy loading are supported out of the box!

Datamappify consists of three components:

  • Entity contains models behaviour, think an ActiveRecord model with the persistence specifics removed.
  • Repository is responsible for data retrieval and persistence, e.g. find, save and destroy, etc.
  • Data as the name suggests, holds your model data. It contains ORM objects (e.g. ActiveRecord models).

Below is a high level and somewhat simplified overview of Datamappify's architecture.

Note: Datamappify is NOT affiliated with the Datamapper project.

Built-in ORMs for Persistence

You may implement your own data provider and criteria, but Datamappify comes with build-in support for the following ORMS:

  • ActiveRecord
  • Sequel

Requirements

  • ruby 2.0+
  • ActiveModel 4.0+

Installation

Add this line to your application's Gemfile:

gem 'datamappify'

Usage

Entity

Entity uses Virtus DSL for defining attributes and ActiveModel::Validations DSL for validations.

The cool thing about Virtus is that all your attributes get coercion for free!

Below is an example of a User entity, with inline comments on how some of the DSLs work.

…

Entity inheritance

Inheritance is supported for entities, for example:

…

Repository inheritance

Inheritance is supported for repositories when your data structure is based on STI (Single Table Inheritance), for example:

ruby
class AdminUserRepository  'User#expiry_date'
end

In the above example, both repositories deal with the ActiveRecord::User data model.

Override mapped data models

Datamappify repository by default creates the underlying data model classes for you. For example:

ruby
map_attribute :driver_license, :to => 'UserData::DriverLicense#number'

In the above example, a Datamppify::Data::Record::ActiveRecord::UserDriverLicense ActiveRecord model will be created. If you would like to customise the data model class, you may do so by creating one either under the default namespace or under the Datamappify::Data::Record::NameOfDataProvider namespace:

ruby
module UserData
  class DriverLicense  'Fred', :driver_license => 'AABBCCDD')
Match
ruby
users = UserRepository.match(:first_name => 'Fre%', :driver_license => '%bbcc%')
Advanced

You may compose search criteria via the criteria method.

ruby
users = UserRepository.criteria(
  :where => {
    :first_name => 'Fred'
  },
  :order => {
    :last_name => :asc
  },
  :limit => [10, 20]
)

Currently implemented criteria options:

  • where(Hash)
  • match(Hash)
  • order(Hash)
  • limit(Array)

Note: it does not currently support searching attributes from different data providers.

Saving/updating entities

Accepts an entity.

There is also save! that raises Datamappify::Data::EntityNotSaved.

ruby
UserRepository.save(user)

Datamappify supports attribute dirty tracking - only dirty attributes will be saved.

Mark attributes as dirty

Sometimes it's useful to manually mark the whole entity, or some attributes in the entity to be dirty. In this case, you could:

ruby
UserRepository.states.mark_as_dirty(user) # marks the whole entity as dirty

UserRepository.states.find(user).changed?            #=> true
UserRepository.states.find(user).first_name_changed? #=> true
UserRepository.states.find(user).last_name_changed?  #=> true
UserRepository.states.find(user).age_changed?        #=> true

Or:

ruby
UserRepository.states.mark_as_dirty(user, :first_name, :last_name) # marks only first_name and last_name as dirty

UserRepository.states.find(user).changed?            #=> true
UserRepository.states.find(user).first_name_changed? #=> true
UserRepository.states.find(user).last_name_changed?  #=> true
UserRepository.states.find(user).age_changed?        #=> false

Destroying an entity

Accepts an entity.

There is also destroy! that raises Datamappify::Data::EntityNotDestroyed.

Note that due to the attributes mapping, any data found in mapped records are not touched. For example the corresponding ActiveRecord::User record will be destroyed, but ActiveRecord::Hobby that is associated will not.

ruby
UserRepository.destroy(user)

Initialising an entity

Accepts an entity class and returns a new entity.

This is useful for using before_init and after_init callbacks to set up the entity.

ruby
UserRepository.init(user_class) #=> user

Callbacks

Datamappify supports the following callbacks via Hooks:

  • before_init
  • before_load
  • before_find
  • before_create
  • before_update
  • before_save
  • before_destroy
  • after_init
  • after_load
  • after_find
  • after_create
  • after_update
  • after_save
  • after_destroy

Callbacks are defined in repositories, and they have access to the entity. For example:

ruby
class UserRepository
  include Datamappify::Repository

  before_create :make_me_admin
  before_create :make_me_awesome
  after_save    :make_me_smile

  private

  def make_me_admin(entity)
    # ...
  end

  def make_me_awesome(entity)
    # ...
  end

  def make_me_smile(entity)
    # ...
  end

  # ...
end

Note: Returning either nil or false from the callback will cancel all subsequent callbacks (and the action itself, if it's a before_ callback).

Association

Datamappify also supports entity association. It is experimental and it currently supports the following association types:

  • belongs_to (partially implemented)
  • has_one
  • has_many

Set up your entities and repositories:

…

Usage examples:

ruby
new_post         = Post.new(post_attributes)
another_new_post = Post.new(post_attributes)
user             = UserRepository.find(1)
user.title       = Title.new(title_attributes)
user.posts       = [new_post, another_new_post]

persisted_user   = UserRepository.save!(user)

persisted_user.title #=> associated title
persisted_user.posts #=> an array of associated posts

Nested attributes in forms

Like ActiveRecord and ActionView, Datamappify also supports nested attributes via fields_for or simple_fields_for.

ruby
# slim template

= simple_form_for @post do |f|
  = f.input :title
  = f.input :body

  = f.simple_fields_for :comment do |fp|
    = fp.input :author_name
    = fp.input :comment_body

Default configuration

You may configure Datamappify's default behaviour. In Rails you would put it in an initializer file.

ruby
Datamappify.config do |c|
  c.default_provider = :ActiveRecord
end

Built-in extensions

Datamappify ships with a few extensions to make certain tasks easier.

Kaminari

Use Criteria with page and per.

ruby
UserRepository.criteria(
  :where => {
    :gender => 'male',
    :age    => 42
  },
  :page => 1,
  :per  => 10
)

API Documentation

  • Rubygem release version
  • Github master version

More Reading

You may check out this article for more examples.

Changelog

Refer to CHANGELOG.

Todo

  • Performance tuning and query optimisation
  • Authoritative source.
  • Support for configurable primary keys and reference (foreign) keys.

Similar Projects

  • Curator
  • Edr
  • Minimapper
  • Reform

Credits

  • Fred Wu - author.
  • James Ladd for reviewing the code and giving advice on architectural decisions.
  • Locomote - where Datamappify was built.
  • And with these awesome contributors!

License

Licensed under MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyactiverecorddata-mapperdata-mappingdata-persistence

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库