How i create indices in LokiJS DB?

412 Views Asked by At

At the creation time we can directly pass property name but how i create indices in a existing db ?

const db = {
  patent: {
    disableMeta: true,
    indices: ['update_time'],
    unique: ['app_no'],
  },
};

const _db = new loki(path.resolve(dump, 'db'), {
  autoload: true,
  autoloadCallback() {
    _
      .keys(db)
      .forEach(k => {
        const exists = _db.getCollection(k);

        if (exists === null) db[k] = _db.addCollection(k, db[k]);
        else db[k] = exists;
      });

    main();
  },
  autosave: true,
  autosaveInterval: 1000 * 10,
  verbose: true,
});

This is my code here i'm defining update_time as index at creation time, how i add one or more indices after creation ?

1

There are 1 best solutions below

0
On

You pass options to the addCollection method, like { indices: ['field1'], unique: ['field2']}, check the docs for indexing options. Indices is for a binary search index, unique to enforce uniqueness of fields.

If you mean after you created a collection, use ensureIndex.