Fixes critical scalability bottleneck where metadata was stored in non-sharded
directories, causing performance degradation at scale (1M+ entities).
Changes:
- Add UUID-based sharding to metadata operations in S3Compatible, FileSystem, and OpFS storage
- Implement complete UUID-based sharding for OpFS storage (nouns, verbs, metadata)
- Update pagination methods to iterate through all 256 UUID-based shards
- Add integration tests verifying sharding behavior across storage adapters
Impact:
- Metadata now scales to millions of entities without directory bottlenecks
- All storage adapters now use consistent UUID-based sharding (256 buckets: 00-ff)
- Improves GCS/S3/R2/OpFS performance at scale
- Path format: entities/{type}/{subtype}/{shard}/{id}.json
Breaking change: Requires data migration for existing S3/GCS/R2/OpFS deployments.
See .strategy/UNIFIED-UUID-SHARDING.md for migration guidance.
31 KiB
🧠 Brainy 3.0 Complete API Reference
The neural database that thinks - Complete API documentation for all public methods
Table of Contents
- Quick Start
- Core API
- Batch Operations
- Search & Discovery
- Security API
- Configuration API
- Data Management API
- Query API
- Neural API
- NLP API
- Streaming Pipeline API
- Type Definitions
Quick Start
import { Brainy, NounType, VerbType } from '@soulcraft/brainy'
// Initialize
const brain = new Brainy({
storage: { type: 'memory' },
model: { type: 'fast', precision: 'Q8' }
})
await brain.init()
// Add entities
const id = await brain.add({
data: 'John Smith is a software engineer',
type: NounType.Person,
metadata: { role: 'engineer' }
})
// Search
const results = await brain.find('engineers')
// Clean up
await brain.close()
Core API
new Brainy(config?: BrainyConfig)
Creates a new Brainy instance.
Parameters:
config- Optional configuration object
Returns: Brainy instance
Example:
const brain = new Brainy({
storage: { type: 'filesystem', options: { path: './data' } },
model: { type: 'accurate', precision: 'FP32' },
cache: { maxSize: 5000, ttl: 600000 }
})
async init(): Promise<void>
Initializes the brain, loading models and preparing storage.
Must be called before any other operations.
Example:
await brain.init()
async add(params: AddParams): Promise<string>
Adds a new entity to the brain.
Parameters:
data(required) - Content to embed and storetype(required) - NounType classificationmetadata- Custom metadata objectid- Custom ID (auto-generated if not provided)vector- Pre-computed embedding vectorservice- Service name for multi-tenancywriteOnly- Skip validation for high-speed ingestion
Returns: Entity ID
Example:
const id = await brain.add({
data: 'Important meeting notes from Q4 planning',
type: NounType.Document,
metadata: {
date: '2024-01-15',
author: 'John Smith',
tags: ['planning', 'Q4']
}
})
async get(id: string): Promise<Entity | null>
Retrieves an entity by ID.
Parameters:
id- Entity ID
Returns: Entity object or null if not found
Example:
const entity = await brain.get('uuid-1234')
if (entity) {
console.log(entity.type) // NounType.Document
console.log(entity.metadata) // { date: '2024-01-15', ... }
}
async update(params: UpdateParams): Promise<void>
Updates an existing entity.
Parameters:
id(required) - Entity ID to updatedata- New content (will re-embed)type- New type classificationmetadata- New or partial metadatamerge- Merge metadata (true) or replace (false), default: truevector- New embedding vector
Example:
await brain.update({
id: 'uuid-1234',
metadata: { status: 'reviewed' },
merge: true // Keeps existing metadata, adds status
})
async delete(id: string): Promise<void>
Deletes an entity and all its relationships.
Parameters:
id- Entity ID to delete
Example:
await brain.delete('uuid-1234')
async relate(params: RelateParams): Promise<string>
Creates a relationship between two entities.
Parameters:
from(required) - Source entity IDto(required) - Target entity IDtype(required) - VerbType enum valueweight- Relationship strength (0-1), default: 1metadata- Relationship metadatabidirectional- Create reverse relationshipservice- Service name for multi-tenancywriteOnly- Skip validation
Validation:
fromandtomust be different (no self-referential relationships)typemust be a valid VerbType enum valueweightif provided must be between 0 and 1
Returns: Relationship ID
Example:
const relationId = await brain.relate({
from: 'person-123',
to: 'org-456',
type: VerbType.WorksWith,
weight: 0.95,
metadata: { since: '2020-01-01' },
bidirectional: true
})
async unrelate(id: string): Promise<void>
Removes a relationship.
Parameters:
id- Relationship ID
Example:
await brain.unrelate('relation-789')
async getRelations(params?: GetRelationsParams): Promise<Relation[]>
Gets relationships for entities.
Parameters:
from- Source entity IDto- Target entity IDtype- Filter by VerbType(s)limit- Maximum results (default: 100)offset- Pagination offsetservice- Filter by service
Example:
// Get all relationships from an entity
const relations = await brain.getRelations({
from: 'person-123',
type: [VerbType.WorksWith, VerbType.ReportsTo],
limit: 50
})
async close(): Promise<void>
Shuts down the brain, cleaning up resources.
Example:
await brain.close()
Batch Operations
async addMany(params: AddManyParams): Promise<BatchResult>
Adds multiple entities in batch.
Parameters:
items(required) - Array of AddParamsparallel- Process in parallel (default: true)chunkSize- Batch size (default: 100)continueOnError- Continue if some failonProgress- Progress callback
Returns: BatchResult with successful/failed counts
Example:
const result = await brain.addMany({
items: [
{ data: 'Doc 1', type: NounType.Document },
{ data: 'Doc 2', type: NounType.Document },
{ data: 'Doc 3', type: NounType.Document }
],
parallel: true,
onProgress: (done, total) => console.log(`${done}/${total}`)
})
console.log(`Added: ${result.successful.length}`)
console.log(`Failed: ${result.failed.length}`)
async updateMany(params: UpdateManyParams): Promise<BatchResult>
Updates multiple entities in batch.
Parameters:
updates(required) - Array of UpdateParamsparallel- Process in parallelcontinueOnError- Continue on failuresonProgress- Progress callback
Example:
const result = await brain.updateMany({
updates: ids.map(id => ({
id,
metadata: { processed: true },
merge: true
})),
parallel: true
})
async deleteMany(params: DeleteManyParams): Promise<BatchResult>
Deletes multiple entities.
Parameters:
ids- Specific IDs to deletetype- Delete all of a typewhere- Delete by metadata filterlimit- Maximum to delete (safety limit)onProgress- Progress callback
Example:
// Delete specific IDs
await brain.deleteMany({
ids: ['id1', 'id2', 'id3']
})
// Delete by type
await brain.deleteMany({
type: NounType.Document,
where: { status: 'draft' },
limit: 100
})
async relateMany(params: RelateManyParams): Promise<BatchResult>
Creates multiple relationships in batch.
Parameters:
relations(required) - Array of RelateParamsparallel- Process in parallelcontinueOnError- Continue on failuresonProgress- Progress callback
Example:
const result = await brain.relateMany({
relations: [
{ from: 'a', to: 'b', type: VerbType.RelatedTo },
{ from: 'b', to: 'c', type: VerbType.DependsOn },
{ from: 'c', to: 'a', type: VerbType.References }
]
})
Search & Discovery
async find(query: string | FindParams): Promise<Result[]>
Universal search with Triple Intelligence fusion.
Parameters:
query- Natural language query or structured paramsvector- Direct vector searchtype- Filter by NounType(s)where- Metadata filtersconnected- Graph constraintsnear- Proximity searchfusion- Fusion strategy and weightslimit- Maximum resultsoffset- Pagination offsetexplain- Include score explanationservice- Filter by servicewriteOnly- Skip validation
Returns: Array of Result objects with scores
Example:
// Natural language search
const results = await brain.find('recent product launches')
// Structured search with fusion
const results = await brain.find({
query: 'machine learning',
type: [NounType.Document, NounType.Project],
where: { year: 2024 },
connected: {
to: 'research-dept',
via: VerbType.CreatedBy
},
fusion: {
strategy: 'adaptive',
weights: { vector: 0.5, graph: 0.3, field: 0.2 }
},
limit: 20,
explain: true
})
async similar(params: SimilarParams): Promise<Result[]>
Finds similar entities using vector similarity.
Parameters:
to(required) - Entity ID, Entity object, or Vectorlimit- Maximum results (default: 10)threshold- Minimum similarity (0-1)type- Filter by type(s)where- Metadata filters
Example:
const similar = await brain.similar({
to: 'doc-123',
limit: 5,
threshold: 0.8,
type: NounType.Document
})
async insights(): Promise<InsightsResult>
Gets statistics and insights about the data.
Returns:
entities- Total entity countrelationships- Total relationship counttypes- Count by NounTypeservices- List of servicesdensity- Relationships per entity
Example:
const insights = await brain.insights()
console.log(`Entities: ${insights.entities}`)
console.log(`Graph density: ${insights.density.toFixed(2)}`)
async suggest(params?: SuggestParams): Promise<Suggestions>
AI-powered suggestions based on current data.
Parameters:
context- Context for suggestionstype- Filter by type(s)limit- Maximum suggestions per categoryservice- Filter by service
Returns:
queries- Suggested search queriesconnections- Suggested relationshipsinsights- Data insightspatterns- Detected patterns
Example:
const suggestions = await brain.suggest({
context: 'product development',
limit: 5
})
// Use suggestions
for (const query of suggestions.queries) {
console.log(`Try searching: ${query}`)
}
Security API
Access via: const security = await brain.security()
async encrypt(data: string): Promise<string>
Encrypts data using AES-256-CBC.
Example:
const encrypted = await security.encrypt('sensitive data')
async decrypt(encryptedData: string): Promise<string>
Decrypts previously encrypted data.
Example:
const decrypted = await security.decrypt(encrypted)
async hash(data: string, algorithm?: 'sha256'|'sha512'): Promise<string>
Creates cryptographic hash.
Example:
const hash = await security.hash('password', 'sha512')
async compare(data: string, hash: string): Promise<boolean>
Compares data against hash (constant-time).
Example:
const matches = await security.compare('password', hash)
async generateToken(bytes?: number): Promise<string>
Generates secure random token.
Example:
const token = await security.generateToken(32)
async deriveKey(password: string, salt?: string): Promise<{key: string, salt: string}>
Derives key from password using PBKDF2-like iterations.
Example:
const { key, salt } = await security.deriveKey('userPassword')
async sign(data: string, secret?: string): Promise<string>
Signs data with HMAC-SHA256.
Example:
const signature = await security.sign(data, secret)
async verify(data: string, signature: string, secret: string): Promise<boolean>
Verifies HMAC signature.
Example:
const valid = await security.verify(data, signature, secret)
Configuration API
Access via: const config = await brain.config()
async set(params): Promise<void>
Sets configuration value.
Parameters:
key(required) - Configuration keyvalue(required) - Value to storeencrypt- Encrypt value
Example:
await config.set({
key: 'api.key',
value: 'secret-key-123',
encrypt: true
})
async get(params): Promise<any>
Gets configuration value.
Parameters:
key(required) - Configuration keydecrypt- Decrypt if encrypteddefaultValue- Default if not found
Example:
const apiKey = await config.get({
key: 'api.key',
decrypt: true,
defaultValue: 'default-key'
})
async delete(key: string): Promise<void>
Deletes configuration key.
async list(): Promise<string[]>
Lists all configuration keys.
async has(key: string): Promise<boolean>
Checks if key exists.
async clear(): Promise<void>
Clears all configuration.
async export(): Promise<Record<string, ConfigEntry>>
Exports all configuration.
async import(config: Record<string, ConfigEntry>): Promise<void>
Imports configuration.
Data Management API
Access via: const data = await brain.data()
async backup(options?: BackupOptions): Promise<BackupData>
Creates backup of all data.
Parameters:
includeVectors- Include embeddings (default: true)compress- Compress backup (default: false)
Example:
const backup = await data.backup({
includeVectors: true,
compress: true
})
// Save backup to file
fs.writeFileSync('backup.json', JSON.stringify(backup))
async restore(params): Promise<void>
Restores from backup.
Parameters:
backup(required) - Backup datamerge- Merge with existing dataoverwrite- Overwrite conflictsvalidate- Validate backup format
Example:
const backup = JSON.parse(fs.readFileSync('backup.json'))
await data.restore({
backup,
merge: false,
overwrite: true
})
async clear(params): Promise<void>
Clears specified data types.
Parameters:
entities- Clear entitiesrelations- Clear relationshipsconfig- Clear configuration
Example:
await data.clear({
entities: true,
relations: true
})
async import(params): Promise<ImportResult>
Imports data from various formats with automatic detection.
Parameters:
data(required) - Data to import (Buffer, string, array, or file path)format- 'auto' | 'json' | 'csv' | 'excel' | 'pdf' | 'yaml' | 'text' (default: 'auto')mapping- Field mapping configurationbatchSize- Import batch size (default: 50)validate- Validate items before importrelationships- Extract relationships automatically (default: true)
CSV-specific options:
csvDelimiter- Column delimiter (auto-detected if not specified)csvHeaders- First row contains headers (default: true)encoding- Character encoding (auto-detected if not specified)
Excel-specific options:
excelSheets- Sheet names array or 'all' for all sheets
PDF-specific options:
pdfExtractTables- Extract tables from PDFs (default: true)pdfPreserveLayout- Preserve text layout (default: true)
Examples:
// Import CSV file with auto-detection
const csvResult = await brain.import('customers.csv')
// Auto-detects: format, encoding, delimiter, field types
// Import Excel workbook
const excelResult = await brain.import('sales-data.xlsx', {
excelSheets: ['Q1', 'Q2'] // Import specific sheets
})
// Import PDF with table extraction
const pdfResult = await brain.import('report.pdf', {
pdfExtractTables: true
})
// Import data array
const dataResult = await brain.import([
{ name: 'Alice', role: 'Engineer' },
{ name: 'Bob', role: 'Designer' }
], {
batchSize: 100,
relationships: true // Auto-extract relationships
})
// Import with custom CSV delimiter
const tsvResult = await brain.import('data.tsv', {
format: 'csv',
csvDelimiter: '\t'
})
Returns:
{
success: boolean
imported: number // Number of items successfully imported
failed: number // Number of items that failed
entityIds: string[] // IDs of created entities
metadata: {
format: string // Detected format
encoding?: string // Detected encoding (CSV)
delimiter?: string // Detected delimiter (CSV)
sheets?: string[] // Processed sheets (Excel)
pageCount?: number // Number of pages (PDF)
}
}
async export(params?: ExportOptions): Promise<any>
Exports data in various formats.
Parameters:
format- 'json' | 'csv' | 'parquet'filter- Filter optionsincludeVectors- Include embeddings
Example:
const exported = await data.export({
format: 'csv',
where: { type: NounType.Document },
includeVectors: false
})
getStats(): StatsResult
Gets complete statistics about entities and relationships. All stats are O(1) pre-calculated - updated when entities/relationships are added/removed.
Returns:
{
entities: {
total: number // Total entity count
byType: Record<string, number> // Entity count by type
}
relationships: {
totalRelationships: number // Total relationship/edge count
relationshipsByType: Record<string, number> // Relationship count by type
uniqueSourceNodes: number // Number of unique source entities
uniqueTargetNodes: number // Number of unique target entities
totalNodes: number // Total unique entities in relationships
}
density: number // Relationships per entity ratio
}
Example:
const stats = brain.getStats()
// Total counts (O(1) operations)
const totalNouns = stats.entities.total
const totalVerbs = stats.relationships.totalRelationships
const totalRelations = stats.relationships.totalRelationships // alias
// Counts by type (O(1) operations)
const nounTypes = stats.entities.byType
const verbTypes = stats.relationships.relationshipsByType
// Graph metrics
console.log(`Entities: ${totalNouns}`)
console.log(`Relationships: ${totalVerbs}`)
console.log(`Density: ${stats.density.toFixed(2)}`)
console.log(`Types:`, Object.keys(nounTypes))
Performance:
- ✅ All counts pre-calculated in memory
- ✅ O(1) access time
- ✅ Updated automatically on add/remove
- ✅ No expensive full scans required
Note: For more granular counting operations, see the brain.counts API below.
Import API
createImportManager(brain: Brainy): ImportManager
Creates an import manager for intelligent data import.
Example:
import { Brainy, createImportManager } from '@soulcraft/brainy'
const brain = new Brainy({ preset: 'memory' })
await brain.init()
const importer = createImportManager(brain)
await importer.init()
async ImportManager.import(source, options?): Promise<ImportResult>
Import data from various sources with AI-powered entity detection.
Parameters:
source- String, Buffer, array, or object to importoptions.source- 'data' | 'file' | 'url' | 'auto' (default: 'auto')options.format- 'json' | 'csv' | 'text' | 'yaml' | 'auto' (default: 'auto')options.batchSize- Batch size for processing (default: 50)options.autoDetect- Auto-detect entity types (default: true)options.typeHint- Suggested entity typeoptions.extractRelationships- Extract relationships (default: true)options.csvDelimiter- CSV delimiter characteroptions.csvHeaders- CSV has headers (default: true)options.parallel- Process in parallel (default: true)options.maxConcurrency- Max concurrent operations
Returns:
{
success: boolean
nouns: string[] // IDs of imported entities
verbs: string[] // IDs of created relationships
errors: string[]
stats: {
total: number
imported: number
failed: number
relationships: number
}
}
Examples:
// Import from CSV file
const result = await importer.importFile('./data.csv', {
format: 'csv',
extractRelationships: true
})
// Import from URL
const result = await importer.importUrl('https://api.example.com/data.json')
// Import array of objects
const data = [
{ name: 'Alice', role: 'Developer' },
{ name: 'Bob', role: 'Designer' }
]
const result = await importer.import(data, {
typeHint: NounType.Person,
extractRelationships: true
})
console.log(`Imported ${result.stats.imported}/${result.stats.total} items`)
console.log(`Created ${result.stats.relationships} relationships`)
Features:
- 🧠 AI Entity Detection - Auto-detect entity types from data
- 🔗 Relationship Extraction - Discover connections automatically
- 📊 Multi-Format Support - CSV, JSON, YAML, text files
- 🚀 Batch Processing - Efficient handling of large datasets
- 💡 Neural Analysis - Confidence scoring and insights
- 🎯 Smart CSV Parsing - Auto-delimiter detection
- 🌐 URL Imports - Direct import from web sources
Query API
Access via: brain.query
async entities(params?): Promise<PaginatedResult<Entity>>
Query entities with pagination.
Parameters:
type- Filter by typewhere- Metadata filterslimit- Page sizecursor- Pagination cursorservice- Filter by service
Example:
const result = await brain.query.entities({
type: NounType.Document,
where: { status: 'published' },
limit: 50
})
// Get next page
if (result.nextCursor) {
const nextPage = await brain.query.entities({
cursor: result.nextCursor
})
}
async relations(params?): Promise<PaginatedResult<Relation>>
Query relationships with pagination.
Parameters:
from- Source entityto- Target entitytype- Relationship typelimit- Page sizecursor- Pagination cursor
Neural API
Access via: brain.neural()
Similarity Operations
async similar(a, b, options?): Promise<SimilarityResult>
Calculates similarity between items.
Example:
const similarity = await neural.similar('doc1', 'doc2', {
explain: true
})
Clustering Operations
async clusters(options?): Promise<SemanticCluster[]>
General purpose clustering.
Parameters:
algorithm- 'hierarchical' | 'kmeans' | 'dbscan' | 'spectral'k- Number of clusters (for kmeans)threshold- Distance thresholdminPoints- Minimum points per cluster
Example:
const clusters = await neural.clusters({
algorithm: 'kmeans',
k: 5
})
async clustersByDomain(params): Promise<SemanticCluster[]>
Domain-specific clustering.
Example:
const clusters = await neural.clustersByDomain({
domain: 'technology',
field: 'category',
maxClusters: 10
})
Outlier Detection
async outliers(options?): Promise<OutlierResult[]>
Detects anomalous entities.
Parameters:
method- 'isolation' | 'lof' | 'statistical' | 'autoencoder'threshold- Outlier threshold (default: 2.5 std deviations)returnScores- Return anomaly scores
Example:
const outliers = await neural.outliers({
method: 'isolation',
threshold: 3.0,
returnScores: true
})
Visualization
async visualize(options?): Promise<VisualizationData>
Generates visualization data for entities.
Parameters:
layout- 'force' | 'circular' | 'hierarchical' | 'random'dimensions- 2D or 3DincludeEdges- Include relationships
Example:
const vizData = await neural.visualize({
layout: 'force',
dimensions: 3,
includeEdges: true
})
NLP API
Access via: brain.nlp()
async processNaturalQuery(query: string): Promise<TripleQuery>
Converts natural language to structured query.
Example:
const structured = await nlp.processNaturalQuery(
"Find all documents about AI created last month"
)
// Returns structured query with type, time filters, etc.
async extract(text: string, options?): Promise<ExtractedEntity[]>
Extracts entities from text using neural matching to NounTypes.
Parameters:
types- Target NounTypes to extractconfidence- Minimum confidence (0-1)includeVectors- Include embeddingsneuralMatching- Use neural type matching (default: true)
Returns: Array of extracted entities with proper NounType classification
Example:
const entities = await nlp.extract(
"John Smith from Microsoft visited New York on Jan 15",
{
types: [NounType.Person, NounType.Organization, NounType.Location],
confidence: 0.7,
neuralMatching: true
}
)
// Returns:
// [
// { text: "John Smith", type: NounType.Person, confidence: 0.92 },
// { text: "Microsoft", type: NounType.Organization, confidence: 0.88 },
// { text: "New York", type: NounType.Location, confidence: 0.85 }
// ]
async sentiment(text: string, options?): Promise<SentimentResult>
Analyzes text sentiment.
Parameters:
granularity- 'document' | 'sentence' | 'aspect'aspects- Aspects to analyze
Returns:
overall- Document sentiment (-1 to 1)sentences- Sentence-level sentimentaspects- Aspect-based sentiment
Example:
const sentiment = await nlp.sentiment(
"The product quality is excellent but the price is too high",
{
granularity: 'aspect',
aspects: ['quality', 'price']
}
)
// Returns:
// overall: { score: 0.2, label: 'mixed' }
// aspects: {
// quality: { score: 0.9, magnitude: 0.8 },
// price: { score: -0.7, magnitude: 0.7 }
// }
Streaming Pipeline API
Access via: brain.stream()
Source Operations
source(generator): Pipeline
Sets data source.
Example:
async function* dataGenerator() {
for (let i = 0; i < 100; i++) {
yield { id: i, data: `Item ${i}` }
}
}
brain.stream()
.source(dataGenerator())
.map(item => item.data)
.sink(console.log)
.run()
Transform Operations
map(fn): Pipeline
Transforms each item.
flatMap(fn): Pipeline
Maps and flattens arrays.
filter(predicate): Pipeline
Filters items.
tap(fn): Pipeline
Side effects without modification.
retry(fn, maxRetries?, backoff?): Pipeline
Retries failed operations.
Batching & Windowing
batch(size, timeoutMs?): Pipeline
Groups items into batches.
window(size, type?): Pipeline
Creates sliding or tumbling windows.
buffer(size, strategy?): Pipeline
Buffers with backpressure handling.
Sink Operations
sink(handler): Pipeline
Custom sink handler.
toBrainy(options?): Pipeline
Sinks data to Brainy.
Example:
brain.stream()
.source(dataSource)
.map(transform)
.batch(100)
.toBrainy({
type: NounType.Document,
metadata: { source: 'stream' }
})
.run()
Execution
run(options?): Promise<void>
Executes the pipeline.
Parameters:
workers- Number of workers or 'auto'monitoring- Enable monitoringmaxThroughput- Rate limitingbackpressure- 'drop' | 'buffer' | 'pause'errorHandler- Error callback
Type Definitions
Entity Types (NounType)
31 types including:
Person,Organization,LocationDocument,File,Message,ContentProduct,Service,ResourceEvent,Task,Project,ProcessUser,Role,StateConcept,Topic,Hypothesis- And more...
Relationship Types (VerbType)
40 types including:
RelatedTo,Contains,PartOfCreates,Modifies,TransformsDependsOn,Requires,UsesOwns,BelongsTo,MemberOfSupervises,ReportsTo,WorksWith- And more...
Core Interfaces
interface Entity<T = any> {
id: string
vector: Vector
type: NounType
metadata?: T
service?: string
createdAt: number
updatedAt?: number
}
interface Relation<T = any> {
id: string
from: string
to: string
type: VerbType
weight?: number
metadata?: T
service?: string
createdAt: number
}
interface Result<T = any> {
id: string
score: number
entity: Entity<T>
explanation?: ScoreExplanation
}
Performance Characteristics
Speed Benchmarks
- Add entity: ~5-10ms (with embedding)
- Vector search: ~1-5ms for 1M entities
- Graph traversal: ~10-20ms (2-hop)
- Batch operations: 1000+ items/second
Scalability
- Entities: Tested to 10M+
- Relationships: Tested to 100M+
- Concurrent operations: 1000+ parallel
- Memory usage: ~1GB per million entities
Storage Requirements
- Per entity: ~2KB (with vector)
- Per relationship: ~200 bytes
- Indexes: ~20% overhead
Best Practices
1. Always Initialize
const brain = new Brainy()
await brain.init() // Required before operations
2. Use Proper Types
// ✅ Good - specific type
await brain.add({ data: 'John', type: NounType.Person })
// ❌ Bad - generic type
await brain.add({ data: 'John', type: NounType.Thing })
3. Batch When Possible
// ✅ Good - batch operation
await brain.addMany({ items: documents })
// ❌ Bad - individual adds in loop
for (const doc of documents) {
await brain.add(doc) // Slow!
}
4. Use Write-Only for Speed
// For high-speed ingestion
await brain.add({
data: content,
type: NounType.Document,
writeOnly: true // Skip validation
})
5. Clean Up Resources
try {
// Your operations
} finally {
await brain.close() // Always close
}
Input Validation
Brainy uses a zero-config validation system that automatically adapts to your system resources:
Auto-Configured Limits
limitparameter maximum: Based on available memory (e.g., 8GB RAM = 80K max limit)- Query string length: Auto-scaled based on memory
- Vector dimensions: Must be exactly 384 for all-MiniLM-L6-v2 model
Common Validation Rules
- Pagination:
limitandoffsetmust be non-negative - Thresholds: Values like
weightandthresholdmust be between 0 and 1 - Mutual Exclusion: Cannot use both
queryandvectorin same request - Type Safety:
NounTypeandVerbTypemust be valid enum values - Self-Reference: Cannot create relationships from an entity to itself
Performance Auto-Tuning
The validation system monitors query performance and adjusts limits automatically:
- Fast queries with large results → increases limits
- Slow queries → reduces limits to maintain performance
Error Handling
All methods validate parameters and throw descriptive errors:
try {
await brain.add({ data: '', type: NounType.Document })
} catch (error) {
// Error: "must provide either data or vector"
}
try {
await brain.find({ limit: -1 })
} catch (error) {
// Error: "limit must be non-negative"
}
try {
await brain.update({ id: 'xyz', metadata: null, merge: false })
} catch (error) {
// Error: "must specify at least one field to update"
// Note: Use metadata: {} to clear, not null
}
Migration from v2
Old (v2.15)
brain.add({ data, type: NounType.Document, metadata })
brain.find({ query, limit: 10 })
brain.insights()
New (v3.0)
brain.add({ data, type: NounType.Document, metadata })
brain.find({ query, limit: 10 })
brain.insights()
Support
- GitHub: https://github.com/soulcraft/brainy
- NPM: https://www.npmjs.com/package/@soulcraft/brainy
- Discord: https://discord.gg/brainy
Brainy v3.0 - The Neural Database That Thinks