How to use lokijs - 10 common examples

To help you get started, we’ve selected a few lokijs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dadi / api / test / test-connector / index.js View on Github external
DataStore.prototype.connect = function ({database, collection}) {
  if (this._mockIsDisconnected(collection)) {
    this.readyState = STATE_DISCONNECTED

    return Promise.reject(new Error('DB_DISCONNECTED'))
  }

  const connectionKey = `${database}.db`

  if (databasePool[connectionKey]) {
    this.database = databasePool[connectionKey]
  } else {
    this.database = new Loki(connectionKey, {
      adapter: new Loki.LokiMemoryAdapter()
    })

    // For debugging
    this.database.___id = {
      name: database,
      uuid: Math.random()
    }
    this._debug('connect: new db', {

      database,
      collection
    })

    databasePool[connectionKey] = this.database
  }
github rexrainbow / phaser3-rex-notes / examples / lokijs / dynamicview.js View on Github external
create() {
        // Create the database
        var db = new loki();

        // Create a collection
        var children = db.addCollection('children');

        var view = children.addDynamicView('over3').applyFind({
            'id': {
                '$gte': 3
            }
        });

        var result = view.data();
        console.log(result);

        // Insert documents
        for (var i = 0; i < 20; i++) {
            children.insert({
github rexrainbow / phaser3-rex-notes / examples / lokijs / serialize.js View on Github external
var children = db.addCollection('children');

        // Insert documents
        for (var i = 0; i < 10; i++) {
            children.insert({
                id: i
            });
        }
        var result = children.chain().data();
        console.log(result);        

        var s = db.serialize();
        //console.log(s);

        // load s into another db
        var db2 = new loki();
        db2.loadJSON(s);

        var coll = db2.getCollection('children');
        var result = coll.chain().data();
        console.log(result);
    }
github rexrainbow / phaser3-rex-notes / examples / lokijs / serialize.js View on Github external
create() {
        // Create the database
        var db = new loki();

        // Create a collection
        var children = db.addCollection('children');

        // Insert documents
        for (var i = 0; i < 10; i++) {
            children.insert({
                id: i
            });
        }
        var result = children.chain().data();
        console.log(result);        

        var s = db.serialize();
        //console.log(s);
github rexrainbow / phaser3-rex-notes / examples / lokijs / load-csv-table.js View on Github external
create() {
        var csvString = `name,hp,mp
Rex,100,20
Alice,300,40`;

        var csvTable = Papa.parse(csvString, {
            dynamicTyping: true,
            header: true
        }).data;

        // Create the database
        var db = new loki();

        // Create a collection
        var children = db.addCollection('children');

        // insert csv-table
        children.insert(csvTable);

        var result = children.chain().data();
        console.log(result);
    }
github repetere / lowkie / lib / connect.js View on Github external
function connect(dbpath = defaultDBPath, options = {}, lowkieConfig = {}, dbconnectionname = 'default') {
  this.config = Object.assign(this.config, lowkieConfig);
  const dbname = path.resolve(dbpath);
  const adapter = (this.config.adapterType === 'file') ? { adapter: new lokiFSAdapter(dbname), } : {};
  const lokiDBOptions = Object.assign({
      autosave: true,
      autosaveInterval: 5000, // 5 seconds
    },
    adapter, options);
  const t = setImmediate(() => {
    this.connection.emit('connecting', { dbname, options, });
    clearImmediate(t);
  });
  const db = new loki(dbname, lokiDBOptions);
  const ensureAdapterFilePromise = () => {
    if (!this.config.adapterType) {
      return Promise.reject(new Error('Invalid Adapter Type'));
    } else {
      return (this.config.adapterType === 'file') ?
        new Promise((resolve, reject) => {
github Emensionyu / qm_lesson / react / react-notes / src / database / index.js View on Github external
import Loki from 'lokijs'
// db 句柄
export const db = new Loki('notes',{
  autoload:true,
  autoloadCallback:databaseInitialize,
  autosave:true,
  autosaveInterval:3000,
  persistenceMethod:'localStorage'
});
function databaseInitialize() {
  const notes = db.getCollection('notes');
  if(notes == null){
    db.addCollection('notes');
  }
}
export function loadCollection(collection){
  return new Promise((resolve)=>{
    db.loadDatabase({},()=>{
      const _collection =
github RocketChat / Rocket.Chat / packages / rocketchat-lib / server / models / _BaseCache.js View on Github external
/* eslint new-cap: 0 */

import loki from 'lokijs';
import {EventEmitter} from 'events';
import objectPath from 'object-path';

const logger = new Logger('BaseCache');

const lokiEq = loki.LokiOps.$eq;
const lokiNe = loki.LokiOps.$ne;

loki.LokiOps.$eq = function(a, b) {
	if (Array.isArray(a)) {
		return a.indexOf(b) !== -1;
	}
	return lokiEq(a, b);
};

loki.LokiOps.$ne = function(a, b) {
	if (Array.isArray(a)) {
		return a.indexOf(b) === -1;
	}
	return lokiNe(a, b);
};
github RocketChat / Rocket.Chat / packages / rocketchat-lib / server / models / _BaseCache.js View on Github external
/* eslint new-cap: 0 */

import loki from 'lokijs';
import {EventEmitter} from 'events';
import objectPath from 'object-path';

const logger = new Logger('BaseCache');

const lokiEq = loki.LokiOps.$eq;
const lokiNe = loki.LokiOps.$ne;

loki.LokiOps.$eq = function(a, b) {
	if (Array.isArray(a)) {
		return a.indexOf(b) !== -1;
	}
	return lokiEq(a, b);
};

loki.LokiOps.$ne = function(a, b) {
	if (Array.isArray(a)) {
		return a.indexOf(b) === -1;
	}
	return lokiNe(a, b);
};

const lokiIn = loki.LokiOps.$in;
loki.LokiOps.$in = function(a, b) {
	if (Array.isArray(a)) {
github rexrainbow / phaser3-rex-notes / examples / lokijs / remove.js View on Github external
create() {
        // Create the database
        var db = new loki();

        // Create a collection
        var children = db.addCollection('children');

        // Insert documents
        for (var i = 0; i < 20; i++) {
            children.insert({
                id: i,
            });
        }

        children
            .chain() // start chain functions
            .where( // pick doc which (id%2 === 1) (odd)
                function (doc) {
                    return (doc.id % 2) === 1;