Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IdentifiableMongoDbPersistence<T, K>

Abstract persistence component that stores data in MongoDB and implements a number of CRUD operations over data items with unique ids. The data items must implement IIdentifiable interface.

In basic scenarios child classes shall only override getPageByFilter, getListByFilter or deleteByFilter operations with specific filter function. All other operations can be used out of the box.

In complex scenarios child classes can implement additional operations by accessing this._collection and 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: 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
    • auth_user: (optional) authentication user name
    • auth_password: (optional) authentication user password
    • 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, string> {

public constructor() {
    base("mydata", new MyDataMongoDbSchema());
}

private composeFilter(filter: FilterParams): any {
    filter = filter || new FilterParams();
    let criteria = [];
    let name = filter.getAsNullableString('name');
    if (name != null)
        criteria.push({ name: name });
    return criteria.length > 0 ? { $and: criteria } : null;
}

public getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams,
    callback: (err: any, page: DataPage<MyData>) => void): void {
    base.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null, callback);
}

}

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

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

persistence.create("123", { id: "1", name: "ABC" }, (err, item) => {
    persistence.getPageByFilter(
        "123",
        FilterParams.fromTuples("name", "ABC"),
        null,
        (err, page) => {
            console.log(page.data);          // Result: { id: "1", name: "ABC" }

            persistence.deleteById("123", "1", (err, item) => {
               ...
            });
        }
    )
});

Type parameters

  • T: IIdentifiable<K>

  • K

Hierarchy

Implements

  • any
  • any
  • any
  • any
  • any
  • any
  • any
  • any

Index

Constructors

constructor

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 convertFromPublicPartial

  • convertFromPublicPartial(value: any): any
  • Converts the given object from the public partial format.

    Parameters

    • value: any

      the object to convert from the public partial format.

    Returns any

    the initial object.

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

deleteById

  • deleteById(correlationId: string, id: K, callback?: function): void
  • Deleted a data item by it's unique id.

    Parameters

    • correlationId: string
    • id: K

      an id of the item to be deleted

    • Optional callback: function

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

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

          • err: any
          • item: T

          Returns void

    Returns void

deleteByIds

  • deleteByIds(correlationId: string, ids: K[], callback?: function): void
  • Deletes multiple data items by their unique ids.

    Parameters

    • correlationId: string

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

    • ids: K[]

      ids of data items to be deleted.

    • 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

getListByIds

  • getListByIds(correlationId: string, ids: K[], callback: function): void
  • Gets a list of data items retrieved by given unique ids.

    Parameters

    • correlationId: string

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

    • ids: K[]

      ids of data items to be retrieved

    • 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

getOneById

  • getOneById(correlationId: string, id: K, callback: function): void
  • Gets a data item by its unique id.

    Parameters

    • correlationId: string

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

    • id: K

      an id of data item to be retrieved.

    • callback: function

      callback function that receives data item or error.

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

          • err: any
          • item: 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

set

  • set(correlationId: string, item: T, callback?: function): void
  • Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

    Parameters

    • correlationId: string
    • item: T

      a item to be set.

    • Optional callback: function

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

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

          • err: any
          • item: T

          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

update

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

    Parameters

    • correlationId: string
    • item: T

      an item to be updated.

    • Optional callback: function

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

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

          • err: any
          • item: T

          Returns void

    Returns void

updatePartially

  • updatePartially(correlationId: string, id: K, data: AnyValueMap, callback?: function): void
  • Updates only few selected fields in a data item.

    Parameters

    • correlationId: string
    • id: K

      an id of data item to be updated.

    • data: AnyValueMap

      a map with fields to be updated.

    • Optional callback: function

      callback function that receives updated item or error.

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

          • err: any
          • item: T

          Returns void

    Returns void

Generated using TypeDoc