Skip to main content

addKnowledge

codebolt.dbmemory.addKnowledge(key: string, value: any): Promise<MemorySetResponse><MemorySetResponse>
Adds a key-value pair to the in-memory database. Supports storing various data types including strings, numbers, objects, and arrays.

Parameters

NameTypeDescription
keystringThe unique key under which to store the value. Supports namespaced keys (e.g., 'user:123', 'config:theme').
valueanyThe value to be stored. Can be string, number, boolean, object, array, null, or undefined.

Returns:

 Promise<MemorySetResponse><MemorySetResponse>
A promise that resolves with the response from the memory set operation including success status and type.

Description

The addKnowledge function stores data in an in-memory database using a key-value structure. It supports all JavaScript data types and can be used for caching user data, configuration settings, session information, and project state.

Usage

const result = await codebolt.dbmemory.addKnowledge(key, value);

Examples

// Store user information
const setResult = await codebolt.dbmemory.addKnowledge('user:123', {
name: 'John Doe',
age: 30,
role: 'developer'
});

console.log('Response type:', setResult?.type);
console.log('Success:', !!setResult?.success);
console.log('Key stored: user:123');

Response Format

{
type: 'string', // Response type identifier
success: boolean // Indicates if the operation was successful
}

Updating Existing Data

You can update existing keys by calling addKnowledge again with the same key:

// Initial data
await codebolt.dbmemory.addKnowledge('user:123', {
name: 'John Doe',
age: 30,
role: 'developer'
});

// Update the data
const updatedUser = {
name: 'John Doe',
age: 31, // Updated age
role: 'senior developer', // Updated role
lastLogin: new Date().toISOString()
};

await codebolt.dbmemory.addKnowledge('user:123', updatedUser);
console.log('✅ Knowledge updated successfully');

Key Naming Conventions

Use namespaced keys for better organization:

  • User data: user:123, user:profile:456
  • Configuration: config:theme, config:language
  • Session data: session:current, session:123
  • Project data: project:config, project:state
  • Counters: counter:visits, counter:errors
  • Tags/Categories: tags:project, categories:blog

Supported Data Types

  • Strings: 'hello world', 'dark'
  • Numbers: 42, 3.14
  • Booleans: true, false
  • Objects: { key: 'value' }
  • Arrays: [1, 2, 3], ['a', 'b', 'c']
  • Null: null
  • Undefined: undefined

Error Handling

The function handles various data types gracefully:

// These operations are all valid
await codebolt.dbmemory.addKnowledge('test:undefined', undefined);
await codebolt.dbmemory.addKnowledge('test:null', null);
await codebolt.dbmemory.addKnowledge('test:empty', '');

Performance

The function is optimized for performance:

// Performance test - storing 10 items
const startTime = Date.now();
for (let i = 0; i < 10; i++) {
await codebolt.dbmemory.addKnowledge(`perf:item:${i}`, {
id: i,
value: `test value ${i}`,
timestamp: Date.now()
});
}
const storeTime = Date.now() - startTime;
console.log(`10 store operations: ${storeTime}ms`);
console.log(`Average store time: ${(storeTime / 10).toFixed(2)}ms`);

Use Cases

  • User Session Management: Store user preferences and session data
  • Configuration Storage: Cache application settings and configurations
  • Temporary Data: Store intermediate processing results
  • State Management: Maintain application state across operations
  • Caching: Cache frequently accessed data for performance
  • Project Context: Store project-specific information and metadata