update()

Update an existing entity's data, metadata, or both.

Signature

brain.update(id: string, updates: UpdateOptions): Promise<void>

Parameters

Property Type Required Description
id string Yes The entity ID to update
data string No New content (re-generates embedding)
metadata object No Metadata to merge or replace

Returns

A Promise that resolves when the update is complete.

Examples

Update Data

update-data.js
// Add an entity
const id = await brain.add({
  data: "TypeScript 4.9",
  metadata: { version: 4.9 }
})

// Update the data (re-generates embedding)
await brain.update(id, {
  data: "TypeScript 5.0 with decorators"
})

// Verify the update
const entity = await brain.get(id)
console.log(entity.data) // "TypeScript 5.0 with decorators"

Update Metadata

update-metadata.js
// Add an entity with metadata
const id = await brain.add({
  data: "Project documentation",
  metadata: {
    status: "draft",
    author: "Alice",
    version: 1
  }
})

// Update just the metadata (merges with existing)
await brain.update(id, {
  metadata: {
    status: "published",
    version: 2,
    publishedAt: new Date().toISOString()
  }
})

// Result: author still "Alice", status is "published", version is 2

Update Both

update-both.js
// Update data and metadata together
await brain.update(id, {
  data: "Updated content with new information",
  metadata: {
    lastModified: new Date().toISOString(),
    modifiedBy: "Bob"
  }
})

Increment Values

increment.js
// Get current value, increment, update
const entity = await brain.get(id)
const currentViews = entity.metadata.views || 0

await brain.update(id, {
  metadata: {
    views: currentViews + 1
  }
})

Add to Arrays

array-update.js
// Get current tags, add new one
const entity = await brain.get(id)
const currentTags = entity.metadata.tags || []

await brain.update(id, {
  metadata: {
    tags: [...currentTags, "new-tag"]
  }
})

Notes

See Also

Next: remove() →