Skip to content

Constructor Options

Clemens Damke edited this page Oct 7, 2015 · 2 revisions

Constructor Options for Database and Collections

Loki constructor options

  • autosave : whether or not to make Loki periodically save data (bool)
  • autosaveInterval : the interval in milliseconds for a periodic save
  • autoload : load a db automatically from a file or localstorage/ indexedDB (bool)
  • autoloadCallback : callback after load (function)
  • autosaveCallback : callback after autosave (function)
  • persistenceMethod : the persistence method ('adapter'|null)
  • persistenceAdapter : the instance of a persistence adapter used for saving data

For more information on persistence go here

Collections constructor

Collections now take a name parameter and an options object. The possible parameters in the options are:

  • indices: array of fields to be indexed
  • transactional: is the collection transactional (a fallback mechanism in case things go wrong during operations)
  • asyncListeners: event listeners can be made sync or async
  • disableChangesApi: enable/disable the Changes API wiki page
  • cloneObjects: you can force LokiJS to create clone copies of objects instead of using the original objects
  • autoupdate : enable/disable the autoupdate feature, default = false (bool)

Added clone object option to specify loki should clone inserted documents

By default, when you insert and object into a LokiJS database, it refers to your object reference rather than create its own copy. If you prefer LokiJS clone its own copies you can specify collection level clone option.

(need to move below text to another page, perhaps an 'function reference' page)

Compound sort for sorting on multiple columns

For chained multisort, some examples include :

rs.compoundsort(['age', 'name']); // to sort by age and then name (both ascending)
rs.compoundsort(['age', ['name', true]); // to sort by age (ascending) and then by name (descending)

For DynamicView view, some examples include :

dv.applySortCriteria(['age', 'name']); // to sort by age and then name (both ascending)
dv.applySortCriteria(['age', ['name', true]); // to sort by age (ascending) and then by name (descending)
dv.applySortCriteria(['age', true], ['name', true]); // to sort by age (descending) and then by name (descending)