Thorin.Interface.Store

You can write your own Thorin stores by implementing thorin.Interface.Store and calling thorin.addStore()

Since Thorin stores are designed as an abstraction layer between your application and your databases, a set of minimal functionality is required to be implemented. The store interface also extends the native EventEmitter class, transforming your store into an event emitter by default.

The configuration of a store should be placed under the store key of the application's root configuration object and under the store's name property. A small example is available below.

'use strict';
// config/app.js file
module.exports = {
   "store.redisDb": {  // the "redisDb" key is the store's name property
      port: 12345
   }
};
Interface functionality
Properties
  • typestring the type (or code) of your store, eg: sql, redis.
  • namestring the name of the store instance when constructed.
constructor()
It is mandatory that you call the super() constructor from your class, so that it can initiate any other dependencies of the store class.
static publicName() : string
Returns the publicly available name of your store, eg SQL Store or Data Store
setName(name)
Manually set the name of a store's instance. By default, it will change the name property.
  • namestring the name of the store instance
stop(done)
The function is called when the app is shutting down. By default, it does nothing. However, if your store must handle connection termination, you should override it.
  • donefunction the callback function to call once the store has stopped
Example
'use strict';
class ThorinRedisStore extends thorin.Interface.Store {
   static publicName() { return 'Redis' }
   // Constructor
   constructor(){
      super(); // this will set the name of the store.
      this.type = "redis";
      this.connection = null;
   }
   
   // Called when the store's configuration is available.
   init(config) {
      this.config = config;
   }
   
   // Called when the application is booting.
   // We should connect to the redis store, handle reconnect, etc.
   run(done) {
      // do async stuff
      done();
   }
   
   // Terminate the connection, flush anything and callback.
   stop(done) {
      // disconnect from the server.
      done();
   }
}

thorin.addStore(ThorinRedisStore, 'redisDb');
Do you have a question or is something missing?

You can always create a new issue on GitHub or contact one of the core founders by chat.