@credo-ts/core
Version:
<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1
1 lines • 3.2 kB
Source Map (JSON)
{"version":3,"file":"Metadata.mjs","names":[],"sources":["../../src/storage/Metadata.ts"],"sourcesContent":["// Any is used to prevent frustrating TS errors if we just want to store arbitrary json data\n// biome-ignore lint/suspicious/noExplicitAny: no explanation\ntype MetadataValue = Record<string, any>\n\nexport type MetadataBase = {\n [key: string]: MetadataValue\n}\n\n/**\n * Metadata access class to get, set (create and update), add (append to a record) and delete metadata on any record.\n *\n * set will override the previous value if it already exists\n *\n * note: To add persistence to these records, you have to update the record in the correct repository\n *\n * @example\n *\n * ```ts\n * connectionRecord.metadata.set('foo', { bar: 'baz' }) connectionRepository.update(connectionRecord)\n * ```\n */\nexport class Metadata<MetadataTypes> {\n public readonly data: MetadataBase\n\n public constructor(data: MetadataBase) {\n this.data = data\n }\n\n /**\n * Gets the value by key in the metadata\n *\n * Any extension of the `BaseRecord` can implement their own typed metadata\n *\n * @param key the key to retrieve the metadata by\n * @returns the value saved in the key value pair\n * @returns null when the key could not be found\n */\n public get<Value extends MetadataValue, Key extends string = string>(\n key: Key\n ): (Key extends keyof MetadataTypes ? MetadataTypes[Key] : Value) | null {\n return (this.data[key] as Key extends keyof MetadataTypes ? MetadataTypes[Key] : Value) ?? null\n }\n\n /**\n * Will set, or override, a key-value pair on the metadata\n *\n * @param key the key to set the metadata by\n * @param value the value to set in the metadata\n */\n public set<Value extends MetadataValue, Key extends string = string>(\n key: Key,\n value: Key extends keyof MetadataTypes ? MetadataTypes[Key] : Value\n ): void {\n this.data[key] = value as MetadataValue\n }\n\n /**\n * Adds a record to a metadata key\n *\n * @param key the key to add the metadata at\n * @param value the value to add in the metadata\n */\n public add<Value extends MetadataValue, Key extends string = string>(\n key: Key,\n value: Partial<Key extends keyof MetadataTypes ? MetadataTypes[Key] : Value>\n ): void {\n this.data[key] = {\n ...this.data[key],\n ...value,\n }\n }\n\n /**\n * Retrieves all the metadata for a record\n *\n * @returns all the metadata that exists on the record\n */\n public get keys(): string[] {\n return Object.keys(this.data)\n }\n\n /**\n * Will delete the key value pair in the metadata\n *\n * @param key the key to delete the data by\n */\n public delete<Key extends string = string>(key: Key): void {\n delete this.data[key]\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAqBA,IAAa,WAAb,MAAqC;CAGnC,AAAO,YAAY,MAAoB;AACrC,OAAK,OAAO;;;;;;;;;;;CAYd,AAAO,IACL,KACuE;AACvE,SAAQ,KAAK,KAAK,QAAyE;;;;;;;;CAS7F,AAAO,IACL,KACA,OACM;AACN,OAAK,KAAK,OAAO;;;;;;;;CASnB,AAAO,IACL,KACA,OACM;AACN,OAAK,KAAK,OAAO;GACf,GAAG,KAAK,KAAK;GACb,GAAG;GACJ;;;;;;;CAQH,IAAW,OAAiB;AAC1B,SAAO,OAAO,KAAK,KAAK,KAAK;;;;;;;CAQ/B,AAAO,OAAoC,KAAgB;AACzD,SAAO,KAAK,KAAK"}