UNPKG

@ocap/indexdb-memory

Version:

OCAP indexdb adapter that uses memory as backend, just for test purpose

52 lines (50 loc) 1.37 kB
import { name } from "../package.mjs"; import { BaseIndex } from "@ocap/indexdb"; import debugFactory from "debug"; //#region src/table/base.ts const debug = debugFactory(name); var MemoryIndex = class MemoryIndex extends 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(`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 export { base_default as default };