| 10 | * Includes adding, deleting, and retrieving metadata associated with nodes. |
| 11 | */ |
| 12 | export class MetadataManager extends IManager implements IMetadataManager { |
| 13 | /** |
| 14 | * Adds metadata to existing nodes. |
| 15 | */ |
| 16 | async addMetadata(metadata: MetadataAddition[]): Promise<MetadataResult[]> { |
| 17 | try { |
| 18 | this.emit('beforeAddMetadata', {metadata}); |
| 19 | |
| 20 | const graph = await this.storage.loadGraph(); |
| 21 | const results: MetadataResult[] = []; |
| 22 | |
| 23 | for (const item of metadata) { |
| 24 | GraphValidator.validateNodeExists(graph, item.nodeName); |
| 25 | const node = graph.nodes.find(e => e.name === item.nodeName); |
| 26 | |
| 27 | if (!Array.isArray(node!.metadata)) { |
| 28 | node!.metadata = []; |
| 29 | } |
| 30 | |
| 31 | const newMetadata = item.contents.filter(content => |
| 32 | !node!.metadata.includes(content) |
| 33 | ); |
| 34 | |
| 35 | node!.metadata.push(...newMetadata); |
| 36 | results.push({ |
| 37 | nodeName: item.nodeName, |
| 38 | addedMetadata: newMetadata |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | await this.storage.saveGraph(graph); |
| 43 | |
| 44 | this.emit('afterAddMetadata', {results}); |
| 45 | return results; |
| 46 | } catch (error) { |
| 47 | const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; |
| 48 | throw new Error(errorMessage); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Deletes metadata from nodes. |
| 54 | */ |
| 55 | async deleteMetadata(deletions: MetadataDeletion[]): Promise<void> { |
| 56 | try { |
| 57 | this.emit('beforeDeleteMetadata', {deletions}); |
| 58 | |
| 59 | const graph = await this.storage.loadGraph(); |
| 60 | let deletedCount = 0; |
| 61 | |
| 62 | for (const deletion of deletions) { |
| 63 | GraphValidator.validateNodeExists(graph, deletion.nodeName); |
| 64 | const node = graph.nodes.find(e => e.name === deletion.nodeName); |
| 65 | |
| 66 | if (node) { |
| 67 | const initialMetadataCount = node.metadata.length; |
| 68 | node.metadata = node.metadata.filter(o => |
| 69 | !deletion.metadata.includes(o) |
nothing calls this directly
no outgoing calls
no test coverage detected