Skip to content

Unit of work #2

@Ptico

Description

@Ptico

Idea

Create unit of work implementation with identity map and smart bulk insert sessions. It will allow us to write clean, idiomatic code and don't care about performance.

Dml.session do
  company = Company.new(name: 'Simpsons Corp')

  %w(Marge Homer Bart Lisa).each do |name|
    user = User.new(name: name, company: company)
    UsersRepository.insert(user)
  end

  CompaniesRepository.insert(company)
end # trigger insert/update

will generate

INSERT INTO companies (name) VALUES ('Simpsons Corp'); -- id = 42
INSERT INTO users (name, company_id) VALUES ('Marge', 42), ('Homer', 42) ...

And

Dml.session do
  UsersRepository.preload(user_ids) # Will retrieve all records immediately

  user_ids.each do |id|
    UsersRepository.fetch(id) # will fetch record from the Identity Map
  end

  locations.each do |location|
    location.user # if preloaded will fetch record from Identity Map
  end
end

Roadmap

Relation mapper

  • Basic n..1 and 1..1 associations (Create basic associations #3)
  • Basic relation object
  • Relations container
  • Relations DSL
  • Topological sorting for associations

Session

  • Identity map
  • Delayed persistance storage
  • Session object

Entity

  • Association attribute

Repository

Things to decide

  • How to comfortably and efficiently preload entity 1..n associated entities
  • How to comfortably and efficiently preload collection associated entities (all and exact)
  • How to manage self-references

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions