mongoid / mongoid-grid_fs

A pure Mongoid/Moped implementation of the MongoDB GridFS specification.
Other
83 stars 50 forks source link

How to specify a separate gridfs host? #68

Open raulccabreu opened 7 years ago

raulccabreu commented 7 years ago

Instead of connect to the mongoid configured one (usually the used to store model documents).

This can be useful on cluster configurations with different number for files and documents hosts.

syntruth commented 6 years ago

I had to figure this out for myself as well, since the code is a bit less than clear on how to do this, but it is possible, by giving the File and Chunk model a store_in hash.

gridfs = Mongoid::GridFs
gridfs.file_model.store_in database: 'db_name_here'
gridfs.chunk_model.store_in database: 'db_name_here'

This can then be confirmed with:

gridfs.file_model.collection

In my apps file model (in this case, for ActiveDirectory user photos), I have a convenience memoized set up method:

def self.gridfs
  @gridfs ||= begin
    gridfs = Mongoid::GridFs

    gridfs.file_model.store_in database: AD.database_name
    gridfs.chunk_model.store_in database: AD.database_name

    gridfs
  end
end

So far, that seems to work, allowing me to store those files in a single database, but access them from 3 separate web-apps.