**feat: add write-only mode support and example usage**
- Introduced `writeOnly` mode in `BrainyData` allowing optimized data ingestion by skipping index loading and disabling search operations. - Enhanced `BrainyDataConfig` to include `writeOnly` support and validate compatibility with `readOnly` mode. - Implemented error handling for search attempts during `writeOnly` mode. - Updated the README with detailed usage examples for database modes, including `writeOnly` and `readOnly`. - Added `examples/write-only-mode.js` to demonstrate practical applications of the `writeOnly` mode. **Purpose**: Optimize memory usage and startup time for data ingestion scenarios by enabling write-only mode with comprehensive documentation and examples.
This commit is contained in:
parent
d70d0946cf
commit
5d82db12e2
3 changed files with 3696 additions and 3426 deletions
31
README.md
31
README.md
|
|
@ -598,6 +598,37 @@ await db.deleteVerb(verbId)
|
|||
|
||||
## Advanced Configuration
|
||||
|
||||
### Database Modes
|
||||
|
||||
Brainy supports special operational modes that restrict certain operations:
|
||||
|
||||
```typescript
|
||||
import { BrainyData } from '@soulcraft/brainy'
|
||||
|
||||
// Create and initialize the database
|
||||
const db = new BrainyData()
|
||||
await db.init()
|
||||
|
||||
// Set the database to read-only mode (prevents write operations)
|
||||
db.setReadOnly(true)
|
||||
|
||||
// Check if the database is in read-only mode
|
||||
const isReadOnly = db.isReadOnly() // Returns true
|
||||
|
||||
// Set the database to write-only mode (prevents search operations)
|
||||
db.setWriteOnly(true)
|
||||
|
||||
// Check if the database is in write-only mode
|
||||
const isWriteOnly = db.isWriteOnly() // Returns true
|
||||
|
||||
// Reset to normal mode (allows both read and write operations)
|
||||
db.setReadOnly(false)
|
||||
db.setWriteOnly(false)
|
||||
```
|
||||
|
||||
- **Read-Only Mode**: When enabled, prevents all write operations (add, update, delete). Useful for deployment scenarios where you want to prevent modifications to the database.
|
||||
- **Write-Only Mode**: When enabled, prevents all search operations. Useful for initial data loading or when you want to optimize for write performance.
|
||||
|
||||
### Embedding
|
||||
|
||||
```typescript
|
||||
|
|
|
|||
68
examples/write-only-mode.js
Normal file
68
examples/write-only-mode.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* Example demonstrating the write-only mode in BrainyData
|
||||
*
|
||||
* This mode is useful for data ingestion scenarios where you only need to insert data
|
||||
* and don't need to perform any search operations. It significantly reduces memory usage
|
||||
* by not loading the index into memory.
|
||||
*/
|
||||
|
||||
import { BrainyData } from '../dist/index.js'
|
||||
|
||||
// eslint-disable-next-line no-undef
|
||||
const console = globalThis.console
|
||||
|
||||
async function demonstrateWriteOnlyMode() {
|
||||
console.log('Demonstrating write-only mode in BrainyData')
|
||||
|
||||
// Create a BrainyData instance in write-only mode
|
||||
const db = new BrainyData({
|
||||
writeOnly: true,
|
||||
storage: {
|
||||
// Use file system storage for persistence
|
||||
forceFileSystemStorage: true
|
||||
}
|
||||
})
|
||||
|
||||
// Initialize the database
|
||||
// This will skip loading the index into memory, saving memory and startup time
|
||||
await db.init()
|
||||
|
||||
console.log('Database initialized in write-only mode')
|
||||
console.log('Is write-only:', db.isWriteOnly()) // Should print true
|
||||
|
||||
// Add some data
|
||||
// This works normally even in write-only mode
|
||||
const id1 = await db.add('This is a test document', { title: 'Test Document 1' })
|
||||
const id2 = await db.add('Another test document with different content', { title: 'Test Document 2' })
|
||||
|
||||
console.log(`Added documents with IDs: ${id1}, ${id2}`)
|
||||
|
||||
// Try to search (this will throw an error in write-only mode)
|
||||
try {
|
||||
await db.search('test', 5)
|
||||
} catch (error) {
|
||||
console.log('Search error (expected):', error.message)
|
||||
}
|
||||
|
||||
// Switch to normal mode to perform searches
|
||||
console.log('Switching to normal mode...')
|
||||
db.setWriteOnly(false)
|
||||
|
||||
// Re-initialize to load the index
|
||||
await db.init()
|
||||
|
||||
console.log('Database reinitialized in normal mode')
|
||||
console.log('Is write-only:', db.isWriteOnly()) // Should print false
|
||||
|
||||
// Now search works
|
||||
const results = await db.search('test', 5)
|
||||
console.log('Search results:', results.map(r => ({ id: r.id, score: r.score, title: r.metadata?.title })))
|
||||
|
||||
// Clean up
|
||||
await db.shutDown()
|
||||
}
|
||||
|
||||
// Run the example
|
||||
demonstrateWriteOnlyMode().catch(error => {
|
||||
console.error('Error in write-only mode example:', error)
|
||||
})
|
||||
7023
src/brainyData.ts
7023
src/brainyData.ts
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue