@travetto/asset-mongo

1.1.1 • Public • Published

MongoDB Asset Source

Mongo/GridFS backend for the travetto asset module

Install: @travetto/asset-mongo

npm install @travetto/asset-mongo

This provides a mongodb implementation of the AssetSource which is a backend for the Asset module.

Code: Mongo backend wiring

import { InjectableFactory } from '@travetto/di';
import { MongoAssetSource, MongoAssetConfig } from '@travetto/asset-mongo';

class AppConfig {
  @InjectableFactory()
  static getSource(cfg: MongoAssetConfig) {
    return new MongoAssetSource(cfg);
  }
}

There is a default configuration that you can easily use, with some sensible defaults.

Code: Mongo configuration

import { Config } from '@travetto/config';

/**
 * Mongo configuration as asset source
 */
@Config('mongo.asset')
export class MongoAssetConfig {
  hosts: string = 'localhost';  // List of hosts, comma separated
  namespace = 'app'; // Database namespace
  port = 27017;
  options = {}; // connection options

  /**
   * Compute connection URL
   */
  get url() {
    const hosts = this.hosts.split(',').map(h => `${h}:${this.port}`).join(',');
    const opts = Object.entries(this.options).map(([k, v]) => `${k}=${v}`).join('&');
    return `mongodb://${hosts}/${this.namespace}?${opts}`;
  }
}

Additionally, you can see that the class is registered with the @Config annotation, and so these values can be overridden using the standard Configuration resolution paths.

/@travetto/asset-mongo/

    Package Sidebar

    Install

    npm i @travetto/asset-mongo

    Homepage

    travetto.io

    Weekly Downloads

    5

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    7.21 kB

    Total Files

    6

    Last publish

    Collaborators

    • arcsine