Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Loki

Hierarchy

Index

Type aliases

Static Environment

Environment: "NATIVESCRIPT" | "NODEJS" | "CORDOVA" | "BROWSER" | "MEMORY"

Static LoadDatabaseOptions

Static PersistenceMethod

PersistenceMethod: "fs-storage" | "local-storage" | "indexed-storage" | "memory-storage" | "adapter"

Static SerializationMethod

SerializationMethod: "normal" | "pretty" | "destructured"

Constructors

constructor

  • Constructs the main database class.

    Parameters

    • Default value filename: string = "loki.db"

      name of the file to be saved to

    • Default value options: Options = {}

    Returns Loki

Properties

Protected _asyncListeners

_asyncListeners: boolean = false

Determines whether or not the callbacks associated with each event should happen in an async fashion or not. Default is false, which means events are synchronous

_collections

_collections: Collection[]

Protected _events

_events: object

A map, with each property being an array of callbacks.

filename

filename: string

Methods

addCollection

  • addCollection<TData, TNested>(name: string, options?: Options<TData, TNested>): Collection<TData, TNested>
  • Adds a collection to the database.

    Type parameters

    • TData: any

    • TNested: any

    Parameters

    • name: string

      name of collection to add

    • Default value options: Options<TData, TNested> = {}

    Returns Collection<TData, TNested>

    a reference to the collection which was just added

addListener

  • addListener(eventName: string | string[], listener: Function): Function

clearChanges

  • clearChanges(): void
  • (Changes API) : clears all the changes in all collections.

    Returns void

close

  • close(): Promise<void>
  • Emits the close event. In autosave scenarios, if the database is dirty, this will save and disable timer. Does not actually destroy the db.

    Returns Promise<void>

    a Promise that resolves after closing the database succeeded

copy

  • Copies 'this' database into a new Loki instance. Object references are shared to make lightweight.

    Parameters

    Returns Loki

deleteDatabase

  • deleteDatabase(): Promise<void>
  • Handles deleting a database from the underlying storage adapter

    Returns Promise<void>

    a Promise that resolves after the database is deleted

deserializeCollection

  • Collection level utility function to deserializes a destructured collection.

    Type parameters

    • T: any

    Parameters

    • destructuredSource: string | string[]

      destructured representation of collection to inflate

    • Default value options: DeserializeCollectionOptions = {}

      used to describe format of destructuredSource input

    Returns Doc<T>[]

    an array of documents to attach to collection.data.

deserializeDestructured

  • Database level destructured JSON deserialization routine to minimize memory overhead. Internally, Loki supports destructuring via loki "serializationMethod' option and the optional LokiPartitioningAdapter class. It is also available if you wish to do your own structured persistence or data exchange.

    Parameters

    • destructuredSource: string | string[]

      destructured json or array to deserialize from

    • Default value options: SerializeDestructuredOptions = {}

      source format options

    Returns any

    An object representation of the deserialized database, not yet applied to 'this' db or document array

Protected emit

  • emit(eventName: string, ...data: any[]): void
  • Emits a particular event with the option of passing optional parameters which are going to be processed by the callback provided signatures match (i.e. if passing emit(event, arg0, arg1) the listener should take two parameters)

    Parameters

    • eventName: string

      the name of the event

    • Rest ...data: any[]

      optional object passed with the event

    Returns void

generateChangesNotification

  • generateChangesNotification(arrayOfCollectionNames?: string[]): Change[]
  • (Changes API) : takes all the changes stored in each collection and creates a single array for the entire database. If an array of names of collections is passed then only the included collections will be tracked.

    see

    private method _createChange() in Collection

    Parameters

    • Optional arrayOfCollectionNames: string[]

    Returns Change[]

    array of changes

getCollection

  • getCollection<TData, TNested>(name: string): Collection<TData, TNested>
  • Retrieves reference to a collection by name.

    Type parameters

    • TData: any

    • TNested: any

    Parameters

    • name: string

      name of collection to look up

    Returns Collection<TData, TNested>

    Reference to collection in database by that name, or null if not found

initializePersistence

  • configures options related to database persistence.

    Parameters

    Returns Promise<void>

    a Promise that resolves after initialization and (if enabled) autoloading the database

listCollections

  • listCollections(): object[]

loadCollection

loadDatabase

  • Handles manually loading from an adapter storage (such as fs-storage) This method utilizes loki configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided). To avoid contention with any throttledSaves, we will drain the save queue first.

    If you are configured with autosave, you do not need to call this method yourself.

    Parameters

    Returns Promise<void>

    a Promise that resolves after the database is loaded

loadJSON

  • Inflates a loki database from a serialized JSON string

    Parameters

    • serializedDb: string | string[]

      a serialized loki database string

    • Optional options: DeserializeOptions

      apply or override collection level settings

    Returns void

loadJSONObject

on

  • on(eventName: string | string[], listener: Function): Function
  • Adds a listener to the queue of callbacks associated to an event

    Parameters

    • eventName: string | string[]

      the name(s) of the event(s) to listen to

    • listener: Function

      callback function of listener to attach

    Returns Function

    the index of the callback in the array of listeners for a particular event

removeCollection

  • removeCollection(collectionName: string): void
  • Removes a collection from the database.

    Parameters

    • collectionName: string

      name of collection to remove

    Returns void

removeListener

  • removeListener(eventName: string | string[], listener: Function): void
  • Removes the listener at position 'index' from the event 'eventName'

    Parameters

    • eventName: string | string[]

      the name(s) of the event(s) which the listener is attached to

    • listener: Function

      the listener callback function to remove from emitter

    Returns void

renameCollection

  • renameCollection<TData, TNested>(oldName: string, newName: string): Collection<TData, TNested>
  • Renames an existing loki collection

    Type parameters

    • TData: any

    • TNested: any

    Parameters

    • oldName: string

      name of collection to rename

    • newName: string

      new name of collection

    Returns Collection<TData, TNested>

    reference to the newly renamed collection

saveDatabase

  • saveDatabase(): Promise<void>
  • Handles manually saving to an adapter storage (such as fs-storage) This method utilizes loki configuration options (if provided) to determine which persistence method to use, or environment detection (if configuration was not provided).

    If you are configured with autosave, you do not need to call this method yourself.

    Returns Promise<void>

    a Promise that resolves after the database is persisted

serialize

  • Serialize database to a string which can be loaded via {@link Loki#loadJSON}

    Parameters

    Returns string | string[]

    Stringified representation of the loki database.

serializeChanges

  • serializeChanges(collectionNamesArray?: string[]): string
  • (Changes API) - stringify changes for network transmission

    Parameters

    • Optional collectionNamesArray: string[]

    Returns string

    string representation of the changes

serializeCollection

  • serializeCollection(options?: object): string | string[]
  • Collection level utility method to serialize a collection in a 'destructured' format

    Parameters

    • Default value options: object = {}

      used to determine output of method

    Returns string | string[]

    A custom, restructured aggregation of independent serializations for a single collection.

serializeDestructured

  • Database level destructured JSON serialization routine to allow alternate serialization methods. Internally, Loki supports destructuring via loki "serializationMethod' option and the optional LokiPartitioningAdapter class. It is also available if you wish to do your own structured persistence or data exchange.

    Parameters

    Returns string | string[]

    A custom, restructured aggregation of independent serializations.

throttledSaveDrain

  • Wait for throttledSaves to complete and invoke your callback when drained or duration is met.

    Parameters

    Returns Promise<void>

    a Promise that resolves when save queue is drained, it is passed a sucess parameter value

toJSON

Generated using TypeDoc