Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Get all configured repositories #268

Closed
lcguida opened this issue Jun 8, 2014 · 4 comments
Closed

Get all configured repositories #268

lcguida opened this issue Jun 8, 2014 · 4 comments

Comments

@lcguida
Copy link

lcguida commented Jun 8, 2014

Hi.

I'm wondering if there's any way to get all repositories configured with DataMapper.

For example, supose I have two repositories configured:

DataMapper.setup(:default, options_hash)
DataMapper.setup(:another, another_options_hash)

I would like to execute the same query across all repositories:

method_to_retrieve_repositories.each do |repository|
    repository { Query }
end

Is there a way to acomplish that ?

@knowtheory
Copy link
Member

Yep. DataMapper::Repository.adapters is a hash mapping the symbols for your repositories to their adapters.

So if you want, you could do something like

DataMapper::Repository.adapters.keys.map do |key| 
  DataMapper.repository(key) do
    # execute some queries
  end
end

Give that a shot and see how it does for you

@lcguida
Copy link
Author

lcguida commented Jun 9, 2014

Yes. It works.

Would be wrong to access the repository directly?

DataMapper::Repository.adapters.each do |repository|
  repository { MyModel.update(:field => true) }
end

@knowtheory
Copy link
Member

Sure, why not. My preference broadly speaking is to go through the public API in case things change elsewhere.

Although it'd have to be DataMapper::Repository.adapters.each do |name, adapter|

@lcguida
Copy link
Author

lcguida commented Jun 9, 2014

Good point on the public API part. I'll keep that way.

Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants