UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

146 lines (145 loc) 5.99 kB
import { ANSI_FAILURE, ANSI_LEFT, ANSI_RIGHT, ANSI_SUCCESS } from "../../util/ansi.js"; import { ThroughDBProvider } from "./ThroughDBProvider.js"; /** Provider that logs operations to the console. */ export class DebugDBProvider extends ThroughDBProvider { async getItem(collection, id) { try { console.debug(`${ANSI_RIGHT} GET ITEM`, collection.name, id); const item = await super.getItem(collection, id); console.debug(`${ANSI_LEFT} GET ITEM`, collection.name, id, item); return item; } catch (reason) { console.error(`${ANSI_FAILURE} GET ITEM`, collection.name, id, reason); throw reason; } } async *getItemSequence(collection, id) { try { console.debug(`${ANSI_RIGHT} SEQUENCE ITEM`, collection.name, id); for await (const item of super.getItemSequence(collection, id)) { console.debug(`${ANSI_LEFT} SEQUENCE ITEM`, collection.name, id, item); yield item; } console.debug(`${ANSI_SUCCESS} SEQUENCE ITEM`, collection.name, id); } catch (thrown) { console.error(`${ANSI_FAILURE} SEQUENCE ITEM`, collection.name, id, thrown); } } async addItem(collection, data) { try { console.debug(`${ANSI_RIGHT} ADD ITEM`, collection.name, data); const id = await super.addItem(collection, data); console.debug(`${ANSI_SUCCESS} ADD ITEM`, collection.name, id, data); return id; } catch (reason) { console.error(`${ANSI_FAILURE} ADD ITEM`, collection.name, data, reason); throw reason; } } async setItem(collection, id, data) { try { console.debug(`${ANSI_RIGHT} SET ITEM`, collection.name, id, data); await super.setItem(collection, id, data); console.debug(`${ANSI_SUCCESS} SET ITEM`, collection.name, id, data); } catch (reason) { console.error(`${ANSI_FAILURE} SET ITEM`, collection.name, id, data, reason); throw reason; } } async updateItem(collection, id, updates) { try { console.debug(`${ANSI_RIGHT} UPDATE ITEM`, collection.name, id, updates); await super.updateItem(collection, id, updates); console.debug(`${ANSI_SUCCESS} UPDATE ITEM`, collection.name, id, updates); } catch (reason) { console.error(`${ANSI_FAILURE} UPDATE ITEM`, collection.name, id, updates, reason); throw reason; } } async deleteItem(collection, id) { try { console.debug(`${ANSI_RIGHT} DELETE`, collection.name, id); await super.deleteItem(collection, id); console.debug(`${ANSI_SUCCESS} DELETE`, collection.name, id); } catch (reason) { console.error(`${ANSI_FAILURE} DELETE`, collection.name, id, reason); throw reason; } } async countQuery(collection, query) { try { console.debug(`${ANSI_RIGHT} COUNT QUERY`, collection.name, query); const count = await super.countQuery(collection, query); console.debug(`${ANSI_LEFT} COUNT QUERY`, collection.name, query, count); return count; } catch (reason) { console.error(`${ANSI_FAILURE} COUNT QUERY`, collection.name, query, reason); throw reason; } } async getQuery(collection, query) { try { console.debug(`${ANSI_RIGHT} GET`, collection.name, query); const items = await super.getQuery(collection, query); console.debug(`${ANSI_LEFT} GET`, collection.name, query, items); return items; } catch (reason) { console.error(`${ANSI_FAILURE} GET`, collection.name, query, reason); throw reason; } } async *getQuerySequence(collection, query) { try { console.debug(`${ANSI_RIGHT} SEQUENCE QUERY`, collection.name, query); for await (const items of super.getQuerySequence(collection, query)) { console.debug(`${ANSI_LEFT} SEQUENCE QUERY`, collection.name, query, items); yield items; } console.debug(`${ANSI_SUCCESS} SEQUENCE QUERY`, collection.name, query); } catch (thrown) { console.error(`${ANSI_FAILURE} SEQUENCE QUERY`, collection.name, query, thrown); } } async setQuery(collection, query, data) { try { console.debug(`${ANSI_RIGHT} SET QUERY`, collection.name, query, data); await super.setQuery(collection, query, data); console.debug(`${ANSI_SUCCESS} SET QUERY`, collection.name, query, data); } catch (reason) { console.error(`${ANSI_FAILURE} SET QUERY`, collection.name, query, data, reason); throw reason; } } async updateQuery(collection, query, updates) { try { console.debug(`${ANSI_RIGHT} UPDATE QUERY`, collection.name, query, updates); await super.updateQuery(collection, query, updates); console.debug(`${ANSI_SUCCESS} UPDATE QUERY`, collection.name, query, updates); } catch (reason) { console.error(`${ANSI_FAILURE} UPDATE QUERY`, collection.name, query, updates, reason); throw reason; } } async deleteQuery(collection, query) { try { console.debug(`${ANSI_RIGHT} DELETE QUERY`, collection.name, query); await super.deleteQuery(collection, query); console.debug(`${ANSI_SUCCESS} DELETE QUERY`, collection.name, query); } catch (reason) { console.error(`${ANSI_FAILURE} DELETE QUERY`, collection.name, query, reason); throw reason; } } }