UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

41 lines (40 loc) 1.76 kB
import { ThroughDBProvider } from "./ThroughDBProvider.js"; /** * Database provider that keeps a log of any written changes to its `.changes` property. * - This is useful if you want to hook into a database to build out audit logging functionality. */ export class ChangesDBProvider extends ThroughDBProvider { get changes() { return this._changes; } _changes = []; async addItem(collection, data) { const id = await super.addItem(collection, data); this._changes.push({ action: "add", collection: collection.name, id, data }); return id; } async setItem(collection, id, data) { await super.setItem(collection, id, data); this._changes.push({ action: "set", collection: collection.name, id, data }); } async updateItem(collection, id, updates) { await super.updateItem(collection, id, updates); this._changes.push({ action: "update", collection: collection.name, id, updates }); } async deleteItem(collection, id) { await super.deleteItem(collection, id); this._changes.push({ action: "delete", collection: collection.name, id }); } async setQuery(collection, query, data) { await super.setQuery(collection, query, data); this._changes.push({ action: "set", collection: collection.name, query, data }); } async updateQuery(collection, query, updates) { await super.updateQuery(collection, query, updates); this._changes.push({ action: "update", collection: collection.name, query, updates }); } async deleteQuery(collection, query) { await super.deleteQuery(collection, query); this._changes.push({ action: "delete", collection: collection.name, query }); } }