Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MongoDbPersistence<T>

Abstract persistence component that stores data in MongoDB using plain driver.

This is the most basic persistence component that is only able to store data items of any type. Specific CRUD operations over the data items must be implemented in child classes by accessing this._db or this._collection properties.

Configuration parameters

  • collection: (optional) MongoDB collection name
  • connection(s):
    • discovery_key: (optional) a key to retrieve the connection from IDiscovery
    • host: host name or IP address
    • port: port number (default: 27017)
    • uri: resource URI or connection string with all parameters in it
  • credential(s):
    • store_key: (optional) a key to retrieve the credentials from ICredentialStore
    • username: (optional) user name
    • password: (optional) user password
  • options:
    • max_pool_size: (optional) maximum connection pool size (default: 2)
    • keep_alive: (optional) enable connection keep alive (default: true)
    • connect_timeout: (optional) connection timeout in milliseconds (default: 5000)
    • socket_timeout: (optional) socket timeout in milliseconds (default: 360000)
    • auto_reconnect: (optional) enable auto reconnection (default: true)
    • reconnect_interval: (optional) reconnection interval in milliseconds (default: 1000)
    • max_page_size: (optional) maximum page size (default: 100)
    • replica_set: (optional) name of replica set
    • ssl: (optional) enable SSL connection (default: false)
    • auth_source: (optional) authentication source
    • debug: (optional) enable debug output (default: false).

References

  • *:logger:*:*:1.0 (optional) ILogger components to pass log messages
  • *:discovery:*:*:1.0 (optional) IDiscovery services
  • *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials

Example

class MyMongoDbPersistence extends MongoDbPersistence<MyData> {

  public constructor() {
      base("mydata");
  }

  public getByName(correlationId: string, name: string, callback: (err, item) => void): void {
    let criteria = { name: name };
    this._model.findOne(criteria, callback);
  });

  public set(correlatonId: string, item: MyData, callback: (err) => void): void {
    let criteria = { name: item.name };
    let options = { upsert: true, new: true };
    this._model.findOneAndUpdate(criteria, item, options, callback);
  }

}

let persistence = new MyMongoDbPersistence();
persistence.configure(ConfigParams.fromTuples(
    "host", "localhost",
    "port", 27017
));

persitence.open("123", (err) => {
     ...
});

persistence.set("123", { name: "ABC" }, (err) => {
    persistence.getByName("123", "ABC", (err, item) => {
        console.log(item);                   // Result: { name: "ABC" }
    });
});

Type parameters

  • T

Hierarchy

Implements

  • any
  • any
  • any
  • any
  • any

Index

Constructors

constructor

  • Creates a new instance of the persistence component.

    Parameters

    • Optional collection: string

      (optional) a collection name.

    Returns MongoDbPersistence

Properties

Protected _client

_client: any

The MongoDB connection object.

Protected _collection

_collection: any

The MongoDb collection object.

Protected _collectionName

_collectionName: string

The MongoDB colleciton object.

Protected _connection

_connection: MongoDbConnection

The MongoDB connection component.

Protected _databaseName

_databaseName: string

The MongoDB database name.

Protected _db

_db: any

The MongoDb database object.

Protected _dependencyResolver

_dependencyResolver: DependencyResolver = new DependencyResolver(MongoDbPersistence._defaultConfig)

The dependency resolver.

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

The logger.

Protected _maxPageSize

_maxPageSize: number = 100

Methods

clear

  • clear(correlationId: string, callback?: function): void
  • Clears component state.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

close

  • close(correlationId: string, callback?: function): void
  • Closes component and frees used resources.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

configure

  • configure(config: ConfigParams): void
  • Configures component by passing configuration parameters.

    Parameters

    • config: ConfigParams

      configuration parameters to be set.

    Returns void

Protected convertFromPublic

  • convertFromPublic(value: any): any
  • Convert object value from public to internal format.

    Parameters

    • value: any

      an object in public format to convert.

    Returns any

    converted object in internal format.

Protected convertToPublic

  • convertToPublic(value: any): any
  • Converts object value from internal to public format.

    Parameters

    • value: any

      an object in internal format to convert.

    Returns any

    converted object in public format.

create

  • create(correlationId: string, item: T, callback?: function): void
  • Creates a data item.

    Parameters

    • correlationId: string
    • item: T

      an item to be created.

    • Optional callback: function

      (optional) callback function that receives created item or error.

        • (err: any, item: T): void
        • Parameters

          • err: any
          • item: T

          Returns void

    Returns void

deleteByFilter

  • deleteByFilter(correlationId: string, filter: any, callback?: function): void
  • Deletes data items that match to a given filter.

    This method shall be called by a public deleteByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • filter: any

      (optional) a filter JSON object.

    • Optional callback: function

      (optional) callback function that receives error or null for success.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

Protected ensureIndex

  • ensureIndex(keys: any, options?: any): void
  • Adds index definition to create it on opening

    Parameters

    • keys: any

      index keys (fields)

    • Optional options: any

      index options

    Returns void

Protected getCountByFilter

  • getCountByFilter(correlationId: string, filter: any, callback: function): void
  • Gets a number of data items retrieved by a given filter.

    This method shall be called by a public getCountByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • filter: any

      (optional) a filter JSON object

    • callback: function

      callback function that receives a data page or error.

        • (err: any, count: number): void
        • Parameters

          • err: any
          • count: number

          Returns void

    Returns void

Protected getListByFilter

  • getListByFilter(correlationId: string, filter: any, sort: any, select: any, callback: function): void
  • Gets a list of data items retrieved by a given filter and sorted according to sort parameters.

    This method shall be called by a public getListByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • filter: any

      (optional) a filter JSON object

    • sort: any

      (optional) sorting JSON object

    • select: any

      (optional) projection JSON object

    • callback: function

      callback function that receives a data list or error.

        • (err: any, items: T[]): void
        • Parameters

          • err: any
          • items: T[]

          Returns void

    Returns void

Protected getOneRandom

  • getOneRandom(correlationId: string, filter: any, callback: function): void
  • Gets a random item from items that match to a given filter.

    This method shall be called by a public getOneRandom method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • filter: any

      (optional) a filter JSON object

    • callback: function

      callback function that receives a random item or error.

        • (err: any, item: T): void
        • Parameters

          • err: any
          • item: T

          Returns void

    Returns void

Protected getPageByFilter

  • getPageByFilter(correlationId: string, filter: any, paging: PagingParams, sort: any, select: any, callback: function): void
  • Gets a page of data items retrieved by a given filter and sorted according to sort parameters.

    This method shall be called by a public getPageByFilter method from child class that receives FilterParams and converts them into a filter function.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • filter: any

      (optional) a filter JSON object

    • paging: PagingParams

      (optional) paging parameters

    • sort: any

      (optional) sorting JSON object

    • select: any

      (optional) projection JSON object

    • callback: function

      callback function that receives a data page or error.

        • (err: any, items: DataPage<T>): void
        • Parameters

          • err: any
          • items: DataPage<T>

          Returns void

    Returns void

isOpen

  • isOpen(): boolean
  • Checks if the component is opened.

    Returns boolean

    true if the component has been opened and false otherwise.

open

  • open(correlationId: string, callback?: function): void
  • Opens the component.

    Parameters

    • correlationId: string

      (optional) transaction id to trace execution through call chain.

    • Optional callback: function

      callback function that receives error or null no errors occured.

        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

setReferences

  • setReferences(references: IReferences): void
  • Sets references to dependent components.

    Parameters

    • references: IReferences

      references to locate the component dependencies.

    Returns void

unsetReferences

  • unsetReferences(): void
  • Unsets (clears) previously set references to dependent components.

    Returns void

Generated using TypeDoc