/** * Implement out of band data augmentation operations */ import type { KvKey } from "@silverbulletmd/silverbullet/type/datastore"; import type { DataStore } from "./datastore.ts"; /** * Implements out of band augmentation of entries in a DataStore * Example use cases: store "lastOpened" for pages and "lastRun" for commands */ export class Augmenter { constructor( private ds: DataStore, private augmentationNamespace: KvKey, ) { } /** * Augments objects additional attributes pulled from a data source */ async augmentObjectArray( objects: any[], keyField: string, ): Promise { const objectMap = new Map(); // create a lookup map based on objects for (const obj of objects) { objectMap.set(obj[keyField], obj); } await this.augmentObjectMap(objectMap); } async augmentObjectMap(objectMap: Map) { // Now augment for await ( const augmentation of this.ds.query({ prefix: this.augmentationNamespace, }) ) { const obj = objectMap.get( augmentation.key[this.augmentationNamespace.length], ); if (obj) { // Copy over properties from augmentation Object.assign(obj, augmentation.value); } } } setAugmentation( key: string, augmentation: Record, ): Promise { return this.ds.set([...this.augmentationNamespace, key], augmentation); } }