Storage

Storage is a factory that allows for easily accessing multiple storage types. This could be a local file storage location, a cloud file storage location or even a mongodb file storage. As long as the correct driver is installed and setup you can access any file system.

@horsepower/storage comes built with three storage drivers.

  • file – for accessing the current machines file system (or remote machine using http)
  • mongo – for accessing a mongodb file system using GridFS
  • s3 – for accessing an s3 cloud file system

Configuration

Storage is configured within config/storage.js. Within the file there is an option labeled disks which is an object containing a list of disks that can be used where the key is the name of the disk.

const { applicationPath, storagePath, env } = require('@horsepower/server')

module.exports = {
  // This is the default driver that will be used if one isn't manually mounted
  default: env('STORAGE_DRIVER', 'local'),

  disks: {
    // This is a driver that links to "storage/app"
    local: {
      driver: 'file',
      root: storagePath('app')
    },
    // This is a driver that links to "public"
    public: {
      driver: 'file',
      root: applicationPath('public')
    }
  }
}

Mongo Storage Configuration

Mongodb is a built in driver that allows you to connect to your mongo server and use it as a file system using GridFS. The configuration utilizes the options setting in the config to connect to a mongo server. The only required option is db which is the database on the server to store the files.

module.exports = {
  disks: {
    mongo: {
      driver: 'mongo',
      root: '/',
      options: {
        db: 'my-database', // (required)
        user: 'root', // (optional) defaults to: ''
        pass: 'abc123', // (optional) defaults to: ''
        host: '123.123.123.123', // (optional) defaults to: 'localhost'
        port: 27017 // (optional) defaults to: 27017
      }
    }
  }
}

S3 Storage Configuration

The S3 driver is a built in driver that allows you to connect to S3. The configuration utilizes the options setting in the config to connect to the S3 server.

module.exports = {
  disks: {
    amazonS3: {
      driver: 's3',
      root: '/',
      bucket: 'my-bucket'
      options: {
        accessKeyId: 'xxxxxxxxxxxxxxxxxxxx',
        secretAccessKey: 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
        // See the connection settings:
        // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
      }
    }
  }
}

Usage

Once all your storage drivers have been setup, storage can then be easily loaded by mounting a disk and executing the task that you would like. You can update your config settings and the driver will start working with the new settings without having to change code.

Lets use this example css file located at public/master.css

body {
  color: red;
  background: green;
}

We can then mount('public') and call load('master.css') to load the file.

const { Storage } = require('@horsepower/storage')

module.exports.main = async function (client) {
  // See the configuration above for the location of "public".
  let text = await Storage.mount('public').load('master.css')
  console.log(text)
}

It is also possible to omit mount() if you want to use the default driver. In this case, since the css is in the public directory, it will not be able to load it unless we move the file to the location of mount('local') since local is the default driver.

const { Storage } = require('@horsepower/storage')

module.exports.main = async function (client) {
  // This uses "local" as the driver because it is set as the default.
  // If no file has been saved to the drivers location called "master.css",
  // then this file will not load anything.
  let text = await Storage.load('master.css')
  console.log(text)
}