Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Two Different Adapters in One Model

I want to use mongo to store a global feed's data but I want to cache the most recent posts in memory with redis. Is there a way to set up one model to use two adapters? If there isn't I think I'm going to try to have one controller use two models.

like image 812
Teeknow Avatar asked Mar 13 '14 17:03

Teeknow


1 Answers

It's an experimental feature, but you can have a single model use multiple connections, and the connections can use different adapters:

In a Sails v0.9.x model, with mongo and redis defined in /config/adapters.js:

module.exports = {

    adapter: ['mongo', 'redis']
    attributes: {...}

}

In a Sails v0.10.x model, with mongo and redis defined in /config/connections.js:

module.exports = {

    connections: ['mongo', 'redis']
    attributes: {...}

}

However, you can't specify that some attributes use one connection, and other attributes use another. If more than one adapter is involved, and the adapters have identical methods, then they will be merged together. This would be the case for using sails-mongo and sails-redis in the same model; both adapters have find, create, update and destroy, so your model would end up using only one of the adapters' methods (sails-redis, in the example above).

What's the use of multiple adapters then? It's a way to add external API methods to a model. For example, if you had a File model representing a file living on Amazon S3, you could use a sails-s3 adapter with upload and download methods, alongside sails-mongo to store metadata about the file.

Caching is also a feature which is in active development, but in the meantime your best bet would be to use a separate model for the cache, as you suggested.

like image 169
sgress454 Avatar answered Oct 14 '22 03:10

sgress454