LokiDB icon indicating copy to clipboard operation
LokiDB copied to clipboard

Examples needed

Open ajmas opened this issue 5 years ago • 3 comments

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/LokiJS-Forge/LokiDB/blob/master/CONTRIBUTING.md#question

I looked in the following places for documentation on how to use LokiDB:

  • https://www.npmjs.com/package/@lokidb/loki
  • https://github.com/LokiJS-Forge/LokiDB
  • https://lokijs-forge.github.io/LokiDB/

While the API is documented, there are no examples for anyone wanting to start with LokiDB. Maybe LokiJS could be a fall back, but it is not clear. I am not seeing a wiki either.

Can anyone indicate some examples that are suitable starting points?

ajmas avatar Jul 21 '20 20:07 ajmas

In the meantime, I am trying to piece an example together that uses IndexedDB (based on examples and example in LokiJS):

import Loki from '@lokidb/loki';
import LokidIndexedAdapter from '@lokidb/indexed-storage';

async initDB () {
    const adapter = new LokidIndexedAdapter();

    const options = {
      env: 'BROWSER',
      serializationMethod: 'normal'
    };

    const db = new Loki('test.db', options);

    const persistenceOptions = {
      adapter: adapter,
      autosave: false,
      autosaveInterval: 1000,
      autoload: false,
      throttledSaves: false,
      persistenceMethod: 'indexed-storage'
    };

    await db.initializePersistence(persistenceOptions);

    let entries = await this.db.getCollection('entries');
    if (entries === null) {
      entries = await this.db.addCollection('entries');
    }

    return db;
}

function mounted() {
  try {
    const db = await initDB();

    const entries = db.getCollection('entries');
    entries.insert({
     id: 321,
      phrase: 'hello world'
    });
 
  } catch (error) {
    console.log(error);
  }
}

This results in an error:

Error: Unknown persistence method.
    at Loki.initializePersistence (lokidb.loki.js?633e:4952)

ENV

  • Browser: Google Chrome 84.0.4147.85 (Official Build) beta (64-bit)
  • OS: macOS 10.15.5
  • @lokidb/indexed-storage: 2.0.0-beta.9
  • @lokidb/loki: 2.0.0-beta.

ajmas avatar Jul 21 '20 20:07 ajmas

Please have a look at the tests: https://github.com/LokiJS-Forge/LokiDB/blob/master/packages/indexed-storage/spec/web/indexed_storage.spec.ts

Viatorus avatar Jul 23 '20 07:07 Viatorus

@Viatorus thank you. Would you consider an initial example in the Read Me or as a new page in the docs?

Also, can they be await/async or do they need to be promise.then()?

ajmas avatar Jul 23 '20 12:07 ajmas