Options
All
  • Public
  • Public/Protected
  • All
Menu

Class SqlitePersistence<T>

Abstract persistence component that stores data in SQLite 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) SQLite 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:
    • connect_timeout: (optional) number of milliseconds to wait before timing out when connecting a new client (default: 0)
    • idle_timeout: (optional) number of milliseconds a client must sit idle in the pool and not be checked out (default: 10000)
    • max_pool_size: (optional) maximum number of clients the pool should contain (default: 10)

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 MySqlitePersistence extends SqlitePersistence<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 MySqlitePersistence();
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 tableName: string

      (optional) a table name.

    Returns SqlitePersistence

Properties

Protected _client

_client: any

The SQLite connection pool object.

Protected _connection

_connection: SqliteConnection

The SQLite connection component.

Protected _databaseName

_databaseName: string

The SQLite database name.

Protected _dependencyResolver

_dependencyResolver: DependencyResolver = new DependencyResolver(SqlitePersistence._defaultConfig)

The dependency resolver.

Protected _logger

_logger: CompositeLogger = new CompositeLogger()

The logger.

Protected _maxPageSize

_maxPageSize: number = 100

Protected _tableName

_tableName: string

The SQLite table object.

Methods

Protected autoCreateObject

  • autoCreateObject(dmlStatement: string): void
  • Adds index definition to create it on opening

    Parameters

    • dmlStatement: string

      DML statement to autocreate database object

    Returns void

Protected autoCreateObjects

  • autoCreateObjects(correlationId: string, callback: function): void
  • Parameters

    • correlationId: string
    • callback: function
        • (err: any): void
        • Parameters

          • err: any

          Returns void

    Returns void

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: string, 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: string

      (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(name: string, keys: any, options?: any): void
  • Adds index definition to create it on opening

    Parameters

    • name: string
    • keys: any

      index keys (fields)

    • Optional options: any

      index options

    Returns void

Protected generateColumns

  • generateColumns(values: any): string
  • Generates a list of column names to use in SQL statements like: "column1,column2,column3"

    Parameters

    • values: any

      an array with column values or a key-value map

    Returns string

    a generated list of column names

Protected generateParameters

  • generateParameters(values: any): string
  • Generates a list of value parameters to use in SQL statements like: "$1,$2,$3"

    Parameters

    • values: any

      an array with values or a key-value map

    Returns string

    a generated list of value parameters

Protected generateSetParameters

  • generateSetParameters(values: any): string
  • Generates a list of column sets to use in UPDATE statements like: column1=$1,column2=$2

    Parameters

    • values: any

      a key-value map with columns and values

    Returns string

    a generated list of column sets

Protected generateValues

  • generateValues(values: any): any[]
  • Generates a list of column parameters

    Parameters

    • values: any

      a key-value map with columns and values

    Returns any[]

    a generated list of column values

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

Protected quoteIdentifier

  • quoteIdentifier(value: string): string
  • Parameters

    • value: string

    Returns string

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