# Vue.js Integration Guide Complete guide to integrating Brainy with Vue.js applications, covering Vue 3, Nuxt.js, Composition API, Options API, and advanced patterns. ## ๐Ÿš€ Quick Start ### Installation ```bash npm create vue@latest my-brainy-app cd my-brainy-app npm install npm install @soulcraft/brainy ``` ### Basic Setup ```javascript // src/composables/useBrainy.js import { ref, onMounted } from 'vue' import { Brainy } from '@soulcraft/brainy' const brain = ref(null) const isReady = ref(false) const error = ref(null) export function useBrainy() { onMounted(async () => { try { brain.value = new Brainy({ storage: { type: 'opfs' } // Browser storage }) await brain.value.init() isReady.value = true } catch (err) { error.value = err.message console.error('Brainy initialization failed:', err) } }) return { brain: readonly(brain), isReady: readonly(isReady), error: readonly(error) } } ``` ## ๐Ÿงฉ Composition API ### Search Component ```vue ``` ### Data Management ```vue ``` ## ๐Ÿ—๏ธ Options API ### Search Component (Options API) ```vue ``` ## ๐Ÿ”Œ Vue Plugin ### Global Brainy Plugin ```javascript // src/plugins/brainy.js import { Brainy } from '@soulcraft/brainy' export default { install(app, options = {}) { const defaultOptions = { storage: { type: 'opfs' }, ...options } const brain = new Brainy(defaultOptions) // Make brain available globally app.config.globalProperties.$brain = brain app.provide('brain', brain) // Auto-initialize brain.init().catch(error => { console.error('Brainy plugin initialization failed:', error) }) // Add global method app.config.globalProperties.$searchBrain = async (query, options) => { return await brain.find(query, options) } } } ``` ### Plugin Usage ```javascript // src/main.js import { createApp } from 'vue' import App from './App.vue' import BrainyPlugin from './plugins/brainy' const app = createApp(App) app.use(BrainyPlugin, { storage: { type: 'opfs' } }) app.mount('#app') ``` ```vue ``` ## ๐Ÿฐ Nuxt.js Integration ### Nuxt Plugin ```javascript // plugins/brainy.client.js import { Brainy } from '@soulcraft/brainy' export default defineNuxtPlugin(async () => { const brain = new Brainy({ storage: { type: 'opfs' } }) await brain.init() return { provide: { brain } } }) ``` ### Nuxt Composable ```javascript // composables/useBrainy.js export const useBrainy = () => { const { $brain } = useNuxtApp() const search = async (query, options = {}) => { return await $brain.find(query, options) } const add = async (data, type, metadata) => { return await $brain.add({ data, type, metadata }) } const stats = async () => { return await $brain.stats() } return { brain: $brain, search, add, stats } } ``` ### Nuxt Page Example ```vue ``` ### Nuxt Configuration ```javascript // nuxt.config.ts export default defineNuxtConfig({ ssr: false, // Disable SSR for browser-only features // Or use client-side hydration nitro: { experimental: { wasm: true } }, vite: { define: { global: 'globalThis' } } }) ``` ## ๐Ÿ› ๏ธ Advanced Patterns ### Global State Management with Pinia ```javascript // stores/brainy.js import { defineStore } from 'pinia' import { Brainy } from '@soulcraft/brainy' export const useBrainyStore = defineStore('brainy', () => { const brain = ref(null) const isReady = ref(false) const error = ref(null) const stats = ref(null) const init = async (options = {}) => { try { brain.value = new Brainy(options) await brain.value.init() isReady.value = true await loadStats() } catch (err) { error.value = err.message console.error('Brainy initialization failed:', err) } } const search = async (query, options = {}) => { if (!isReady.value) throw new Error('Brain not ready') return await brain.value.find(query, options) } const add = async (data, type, metadata) => { if (!isReady.value) throw new Error('Brain not ready') const id = await brain.value.add({ data, type, metadata }) await loadStats() // Refresh stats return id } const loadStats = async () => { if (!isReady.value) return try { stats.value = await brain.value.stats() } catch (err) { console.error('Failed to load stats:', err) } } return { brain: readonly(brain), isReady: readonly(isReady), error: readonly(error), stats: readonly(stats), init, search, add, loadStats } }) ``` ### Real-time Search Component ```vue ``` ## ๐Ÿ“Š Performance Optimization ### Lazy Loading ```vue ``` ### Virtual Scrolling for Large Results ```vue ``` ## ๐Ÿงช Testing ### Component Testing with Vitest ```javascript // tests/components/Search.test.js import { mount } from '@vue/test-utils' import { describe, it, expect, vi, beforeEach } from 'vitest' import Search from '../src/components/Search.vue' // Mock Brainy vi.mock('@soulcraft/brainy', () => ({ Brainy: vi.fn().mockImplementation(() => ({ init: vi.fn().mockResolvedValue(undefined), find: vi.fn().mockResolvedValue([ { id: '1', data: 'Test result', score: 0.9 } ]) })) })) describe('Search Component', () => { let wrapper beforeEach(() => { wrapper = mount(Search) }) it('renders search input', () => { expect(wrapper.find('input').exists()).toBe(true) }) it('shows loading state initially', () => { expect(wrapper.text()).toContain('Initializing AI') }) it('performs search when input changes', async () => { // Wait for brain to initialize await wrapper.vm.$nextTick() const input = wrapper.find('input') await input.setValue('test query') await input.trigger('input') // Wait for debounced search await new Promise(resolve => setTimeout(resolve, 350)) expect(wrapper.text()).toContain('Test result') }) }) ``` ### E2E Testing with Playwright ```javascript // tests/e2e/search.spec.js import { test, expect } from '@playwright/test' test('search functionality works', async ({ page }) => { await page.goto('/') // Wait for brain to initialize await page.waitForSelector('[data-testid="search-input"]') // Perform search await page.fill('[data-testid="search-input"]', 'test query') // Wait for results await page.waitForSelector('[data-testid="search-results"]') // Check results const results = await page.locator('[data-testid="result-item"]') await expect(results).toHaveCountGreaterThan(0) }) ``` ## ๐Ÿš€ Production Tips ### Bundle Optimization ```javascript // vite.config.js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' export default defineConfig({ plugins: [vue()], define: { global: 'globalThis' }, optimizeDeps: { include: ['@soulcraft/brainy'] }, build: { rollupOptions: { output: { manualChunks: { 'brainy': ['@soulcraft/brainy'] } } } } }) ``` ### Error Handling ```javascript // src/composables/useBrainyWithErrorHandling.js import { ref, onMounted } from 'vue' import { Brainy } from '@soulcraft/brainy' export function useBrainyWithErrorHandling() { const brain = ref(null) const isReady = ref(false) const error = ref(null) const retryCount = ref(0) const init = async () => { try { brain.value = new Brainy() await brain.value.init() isReady.value = true error.value = null retryCount.value = 0 } catch (err) { error.value = err.message console.error('Brainy initialization failed:', err) // Retry logic if (retryCount.value < 3) { retryCount.value++ setTimeout(init, 2000 * retryCount.value) } } } onMounted(init) return { brain: readonly(brain), isReady: readonly(isReady), error: readonly(error), retry: init } } ``` ## ๐ŸŽฏ Complete Example Here's a complete Vue 3 application structure: ``` my-brainy-vue-app/ โ”œโ”€โ”€ src/ โ”‚ โ”œโ”€โ”€ components/ โ”‚ โ”‚ โ”œโ”€โ”€ Search.vue โ”‚ โ”‚ โ”œโ”€โ”€ DataManager.vue โ”‚ โ”‚ โ””โ”€โ”€ RealTimeSearch.vue โ”‚ โ”œโ”€โ”€ composables/ โ”‚ โ”‚ โ”œโ”€โ”€ useBrainy.js โ”‚ โ”‚ โ””โ”€โ”€ useBrainyWithErrorHandling.js โ”‚ โ”œโ”€โ”€ stores/ โ”‚ โ”‚ โ””โ”€โ”€ brainy.js โ”‚ โ”œโ”€โ”€ utils/ โ”‚ โ”‚ โ””โ”€โ”€ debounce.js โ”‚ โ”œโ”€โ”€ App.vue โ”‚ โ””โ”€โ”€ main.js โ”œโ”€โ”€ tests/ โ”‚ โ”œโ”€โ”€ components/ โ”‚ โ””โ”€โ”€ e2e/ โ”œโ”€โ”€ vite.config.js โ””โ”€โ”€ package.json ``` This provides a complete, production-ready Vue.js application with comprehensive Brainy integration. ## ๐Ÿ“š Next Steps - [Framework Integration Guide](framework-integration.md) - Multi-framework patterns - [Production Deployment](../deployment/CLOUD_DEPLOYMENT_GUIDE.md) - Deploy to production - [API Reference](../api/README.md) - Complete API documentation - [Examples Repository](https://github.com/soulcraftlabs/brainy-examples) - More examples