@ocap/indexdb-memory
Version:
OCAP indexdb adapter that uses memory as backend, just for test purpose
55 lines (53 loc) • 1.6 kB
JavaScript
Object.defineProperty(exports, '__esModule', { value: true });
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
const require_package = require('../package.cjs');
let _ocap_indexdb = require("@ocap/indexdb");
let debug = require("debug");
debug = require_rolldown_runtime.__toESM(debug);
//#region src/table/base.ts
const debug$1 = (0, debug.default)(require_package.name);
var MemoryIndex = class MemoryIndex extends _ocap_indexdb.BaseIndex {
/**
* @param name table name
* @param uniqIndex primary key(s)
* @param db LokiJS database instance
*/
constructor(name$1, uniqIndex, db) {
super(name$1, uniqIndex);
if (!db) throw new Error("db is required for MemoryIndex");
this.collection = db.addCollection(name$1, {
unique: [this.primaryKey],
clone: true
});
this.markReady();
}
count(...args) {
return this.collection.count(...args);
}
_insert(row) {
debug$1(`insert ${this.name}`, row);
const id = this.generatePrimaryKey(row);
return this.collection.insert({
[this.primaryKey]: id,
...row
});
}
_get(key) {
const id = this.generatePrimaryKey(key);
return key ? this.collection.by(this.primaryKey, id) ?? null : null;
}
_update(key, updates) {
const id = this.generatePrimaryKey(key);
const doc = MemoryIndex.prototype._get.call(this, id);
if (!doc) throw new Error(`${this.name} does not exists: ${key}`);
Object.assign(doc, updates);
this.collection.update(doc);
return doc;
}
_reset() {
this.collection.removeWhere({});
}
};
var base_default = MemoryIndex;
//#endregion
exports.default = base_default;