@ocap/indexdb-memory
Version:
OCAP indexdb adapter that uses memory as backend, just for test purpose
44 lines (34 loc) • 1.02 kB
JavaScript
/* eslint-disable no-underscore-dangle */
const { BaseIndex } = require('@ocap/indexdb');
const Lokijs = require('lokijs');
const debug = require('debug')(require('../../package.json').name);
const db = new Lokijs('ocap-memory-indexdb.db');
class MemoryIndex extends BaseIndex {
constructor(name, uniqIndex) {
super(name);
this.uniqIndex = uniqIndex;
this.collection = db.addCollection(name, { unique: [uniqIndex], clone: true });
this.markReady();
}
count(...args) {
return this.collection.count(...args);
}
_insert(row) {
debug(`insert ${this.name}`, row);
return this.collection.insert(row);
}
_get(key) {
return this.collection.by(this.uniqIndex, key);
}
_update(key, updates) {
debug(`update ${this.name}`, { key, updates });
const doc = this.collection.by(this.uniqIndex, key);
Object.assign(doc, updates);
this.collection.update(doc);
return doc;
}
_reset() {
this.collection.removeWhere({});
}
}
module.exports = MemoryIndex;