Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MongoDbPersistence

Abstract persistence component that stores data in MongoDB and is based using Mongoose object relational mapping.

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._collection or this._model 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: 5 sec)
    • auto_reconnect: (optional) enable auto reconnection (default: true)
    • max_page_size: (optional) maximum page size (default: 100)
    • 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", new MyDataMongoDbSchema());
}

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" }
    });
});

Hierarchy

Implements

  • IReferenceable
  • IConfigurable
  • IOpenable
  • ICleanable

Index

Constructors

constructor

Properties

Protected _collection

_collection: string

The MongoDB colleciton object.

Protected _connection

_connection: any

The MongoDB connection object.

Protected _connectionResolver

_connectionResolver: MongoDbConnectionResolver = new MongoDbConnectionResolver()

The connection resolver.

Protected _database

_database: string

The MongoDB database name.

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

The logger.

Protected _model

_model: any

The Mongoose model object.

Protected _options

_options: ConfigParams = new ConfigParams()

The configuration options.

Protected _schema

_schema: Schema

The Mongoose schema.

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.

isOpen

  • isOpen(): boolean

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

Generated using TypeDoc