2026-01-27 15:38:21 -08:00
# VFS Initialization Guide
2025-09-26 14:27:46 -07:00
## Quick Start
2025-11-02 10:58:52 -08:00
The Brainy VFS is automatically initialized during `brain.init()` . No separate initialization needed!
2025-09-26 14:27:46 -07:00
```javascript
import { Brainy } from '@soulcraft/brainy '
2025-11-02 10:58:52 -08:00
// Create and initialize Brainy
2025-09-26 14:27:46 -07:00
const brain = new Brainy({
storage: { type: 'filesystem', path: './data' }
})
2025-11-02 10:58:52 -08:00
await brain.init() // VFS is auto-initialized here!
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
// Use VFS immediately - it's a property, not a method!
await brain.vfs.writeFile('/test.txt', 'Hello World')
const files = await brain.vfs.readdir('/')
2025-09-26 14:27:46 -07:00
```
2025-11-02 10:58:52 -08:00
## What Changed in v5.1.0?
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
### Before (v4.x and early v5.0.0):
2025-09-26 14:27:46 -07:00
```javascript
2025-11-02 10:58:52 -08:00
const brain = new Brainy(...)
await brain.init()
const vfs = brain.vfs() // ❌ Method call
await vfs.init() // ❌ Separate initialization
await vfs.writeFile(...)
2025-09-26 14:27:46 -07:00
```
2026-01-27 15:38:21 -08:00
### After:
2025-09-26 14:27:46 -07:00
```javascript
2025-11-02 10:58:52 -08:00
const brain = new Brainy(...)
await brain.init() // VFS auto-initialized!
await brain.vfs.writeFile(...) // ✅ Property access, just works!
2025-09-26 14:27:46 -07:00
```
2025-11-02 10:58:52 -08:00
### Key Changes:
1. ** `vfs()` → `vfs` **: Method call becomes property access
2. **Auto-initialization** : VFS initialized during `brain.init()`
3. **Zero complexity** : No separate `vfs.init()` call needed
4. **Consistent pattern** : VFS treated like any other brain API
## Migration from v4.x/v5.0.0
### Old Pattern (DEPRECATED):
2025-09-26 14:27:46 -07:00
```javascript
const vfs = brain.vfs()
2025-11-02 10:58:52 -08:00
await vfs.init()
await vfs.writeFile('/test.txt', 'data')
2025-09-26 14:27:46 -07:00
```
2026-01-27 15:38:21 -08:00
### New Pattern:
2025-11-02 10:58:52 -08:00
```javascript
// Just remove the () and init() call
await brain.vfs.writeFile('/test.txt', 'data')
```
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
## Why Auto-Initialization?
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
VFS stores files as entities and relationships in the same graph as everything else. There's no reason to treat it differently! Auto-initialization:
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
- ✅ **Simpler API** : One less step to remember
- ✅ **Fewer errors** : Can't forget to initialize
- ✅ **More intuitive** : Property access feels natural
- ✅ **Consistent** : Matches how other brain APIs work
2025-09-26 14:27:46 -07:00
## Complete Example
```javascript
import { Brainy } from '@soulcraft/brainy '
2025-11-02 10:58:52 -08:00
async function useVFS() {
2025-09-26 14:27:46 -07:00
// Initialize Brainy
const brain = new Brainy({
storage: {
type: 'filesystem', // or 'memory', 's3', 'r2'
path: './brainy-data'
}
})
2025-11-02 10:58:52 -08:00
await brain.init() // VFS ready after this!
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
// Use VFS immediately
await brain.vfs.writeFile('/readme.txt', 'Welcome to VFS!')
await brain.vfs.mkdir('/documents')
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
const files = await brain.vfs.readdir('/')
console.log('Files in root:', files.map(f => f.name))
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
const content = await brain.vfs.readFile('/readme.txt')
console.log('File content:', content.toString())
2025-09-26 14:27:46 -07:00
}
2025-11-02 10:58:52 -08:00
useVFS().catch(console.error)
2025-09-26 14:27:46 -07:00
```
## TypeScript Usage
```typescript
import { Brainy, VirtualFileSystem } from '@soulcraft/brainy '
class FileManager {
private brain: Brainy
async initialize(): Promise< void > {
this.brain = new Brainy({
storage: { type: 'filesystem' }
})
await this.brain.init()
2025-11-02 10:58:52 -08:00
// VFS is ready! No separate initialization needed
2025-09-26 14:27:46 -07:00
}
async writeFile(path: string, content: string): Promise< void > {
2025-11-02 10:58:52 -08:00
if (!this.brain) {
2025-09-26 14:27:46 -07:00
throw new Error('FileManager not initialized. Call initialize() first.')
}
2025-11-02 10:58:52 -08:00
// Use VFS as property
await this.brain.vfs.writeFile(path, content)
}
async listFiles(path: string): Promise< string [ ] > {
const entries = await this.brain.vfs.readdir(path)
return entries.map(e => e.name)
2025-09-26 14:27:46 -07:00
}
}
```
2025-11-02 10:58:52 -08:00
## Error Messages
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
If you see this error:
```
Brainy not initialized. Call init() first.
```
It means you tried to use VFS before calling `brain.init()` . Always initialize Brainy first:
2025-09-26 14:27:46 -07:00
```javascript
2025-11-02 10:58:52 -08:00
await brain.init() // Required!
await brain.vfs.writeFile(...) // Now this works
```
2025-09-26 14:27:46 -07:00
2026-06-11 08:37:26 -07:00
## Snapshot Support
2025-09-26 14:27:46 -07:00
2026-06-11 08:37:26 -07:00
VFS files are ordinary entities, so they participate fully in the 8.0 Db
API: a persisted snapshot contains every VFS file, and a snapshot opened
with `Brainy.load()` serves VFS reads at the snapshot's state:
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
```javascript
2026-06-11 08:37:26 -07:00
const brain = new Brainy({ storage: { type: 'filesystem', rootDirectory: './data' } })
2025-11-02 10:58:52 -08:00
await brain.init()
2025-09-26 14:27:46 -07:00
2026-06-11 08:37:26 -07:00
// Create files
2025-11-02 10:58:52 -08:00
await brain.vfs.writeFile('/config.json', '{"version": 1}')
2026-06-11 08:37:26 -07:00
// Snapshot the whole store (VFS files included)
const pin = brain.now()
await pin.persist('/backups/with-vfs')
await pin.release()
2025-09-26 14:27:46 -07:00
2026-06-11 08:37:26 -07:00
// Later changes never touch the snapshot
await brain.vfs.writeFile('/config.json', '{"version": 2}')
2025-09-26 14:27:46 -07:00
```
2026-06-11 08:37:26 -07:00
See [Snapshots & Time Travel ](../guides/snapshots-and-time-travel.md ).
2025-09-26 14:27:46 -07:00
## FAQ
2025-11-02 10:58:52 -08:00
### Q: Do I need to call `vfs.init()` anymore?
**A:** No! VFS is automatically initialized during `brain.init()` in v5.1.0+.
### Q: Why did the API change from `vfs()` to `vfs`?
**A:** VFS uses the same entity/relationship graph as everything else. There's no reason to treat it differently from other brain APIs.
### Q: Will my old code break?
**A:** If you're using `brain.vfs()` or `await vfs.init()` , you'll need to update to the new pattern. The migration is simple - just remove the `()` and `init()` calls.
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
### Q: Can I still configure VFS?
**A:** Yes, VFS configuration is passed through `brain.init()` config. The VFS-specific options are applied during auto-initialization.
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
### Q: Does this work with all storage adapters?
2026-06-11 08:37:26 -07:00
**A:** Yes! VFS auto-initialization works with both shipped adapters (FileSystem and Memory) and any plugin-provided storage adapter.
2025-09-26 14:27:46 -07:00
2025-11-02 10:58:52 -08:00
### Q: What if I need multiple VFS instances?
**A:** Each Brainy instance has its own VFS. Create multiple Brainy instances if you need multiple VFS instances.
2025-09-26 14:27:46 -07:00
## Related Documentation
- [VFS Quick Start ](./QUICK_START.md ) - 5-minute setup guide
- [VFS API Guide ](./VFS_API_GUIDE.md ) - Complete API reference
2025-11-02 10:58:52 -08:00
- [Common Patterns ](./COMMON_PATTERNS.md ) - Best practices and patterns
2026-06-11 08:37:26 -07:00
- [Snapshots & Time Travel ](../guides/snapshots-and-time-travel.md ) - Backups and point-in-time reads