Options
All
  • Public
  • Public/Protected
  • All
Menu

Class CouchbasePersistence<T>

Abstract persistence component that stores data in Couchbase and is based using Couchbaseose 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

  • bucket: (optional) Couchbase bucket 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:
    • auto_create: (optional) automatically create missing bucket (default: false)
    • auto_index: (optional) automatically create primary index (default: false)
    • flush_enabled: (optional) bucket flush enabled (default: false)
    • bucket_type: (optional) bucket type (default: couchbase)
    • ram_quota: (optional) RAM quota in MB (default: 100)

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 MyCouchbasePersistence extends CouchbasePersistence<MyData> {

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

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 MyCouchbasePersistence();
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 bucket: string

      (optional) a bucket name.

    • Optional collection: string

      (optional) a collection name.

    Returns CouchbasePersistence

Properties

Protected _bucket

_bucket: any

The Couchbase bucket object.

Protected _bucketName

_bucketName: string

The Couchbase bucket name.

Protected _cluster

_cluster: any

The Couchbase cluster object.

Protected _collectionName

_collectionName: string

Protected _connection

_connection: CouchbaseConnection

The Couchbase connection component.

Protected _dependencyResolver

_dependencyResolver: DependencyResolver = new DependencyResolver(CouchbasePersistence._defaultConfig)

The dependency resolver.

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

The logger.

Protected _maxPageSize

_maxPageSize: number = 100

Protected _options

_options: ConfigParams = new ConfigParams()

The configuration options.

Protected _query

_query: any

The Couchbase N1qlQuery object.

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 generateBucketId

  • generateBucketId(value: any): string
  • Generates unique id for specific collection in the bucket

    Parameters

    • value: any

      a public unique id.

    Returns string

    a unique bucket id.

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 query string after WHERE clause

    • 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 query string after WHERE clause

    • paging: PagingParams

      (optional) paging parameters

    • sort: any

      (optional) sorting string after ORDER BY clause

    • select: any

      (optional) projection string after SELECT clause

    • 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