--- name: powersync-js-vue description: PowerSync Vue and Nuxt integration — composables, plugin setup, Kysely ORM, and diagnostics panel metadata: tags: vue, nuxt, composables, useQuery, javascript, typescript, offline-first --- # PowerSync Vue & Nuxt > **Load this when** building a Vue app or Nuxt app with PowerSync. Always load `powersync-js.md` first. ## Table of Contents - [Vue](#vue) (Plugin, Composables, Watchers) - [Nuxt](#nuxt) (Module, Auto-imports, SSR) Vue-specific integration for the PowerSync JavaScript SDK. Use this reference alongside `references/sdks/powersync-js.md` when building Vue apps or Nuxt apps. | Resource | Description | |----------|-------------| | [Vue Composables Reference](https://docs.powersync.com/client-sdks/frameworks/vue.md) | Full Vue composables guide, consult for details beyond the inline examples. | | [Vue SDK API Reference](https://powersync-ja.github.io/powersync-js/vue-sdk) | Full API reference for `@powersync/vue`, consult only when the inline examples don't cover your case. | | [Nuxt Integration Guide](https://docs.powersync.com/client-sdks/frameworks/nuxt.md) | Full Nuxt setup guide. | | [Nuxt Module API Reference](https://powersync-ja.github.io/powersync-js/nuxt-sdk) | API reference for `@powersync/nuxt`. | ## Vue ### 1. Install ```bash # npm (v7+ installs peer dependencies automatically) npm install @powersync/vue@latest # pnpm (must install peer dependencies explicitly) pnpm add @powersync/vue@latest @powersync/web@latest @journeyapps/wa-sqlite@latest ``` ### 2. Plugin Setup ```ts import { createPowerSyncPlugin } from '@powersync/vue'; import { PowerSyncDatabase } from '@powersync/web'; import { AppSchema } from './AppSchema'; import { PowerSyncConnector } from './PowerSyncConnector'; import { createApp } from 'vue'; import App from './App.vue'; const db = new PowerSyncDatabase({ database: { dbFilename: 'app.db' }, schema: AppSchema }); const connector = new PowerSyncConnector(); db.connect(connector); const app = createApp(App); app.use(createPowerSyncPlugin({ database: db })); app.mount('#app'); ``` ### Composables In a standalone Vue app, composables are imported from `@powersync/vue`. In Nuxt, all composables are auto-imported — no import statement is needed. #### `useQuery` Reactive query — re-renders the component automatically when underlying data changes. ```ts import { useQuery } from '@powersync/vue'; // omit import in Nuxt const { data: lists, isLoading, isFetching, error } = useQuery( 'SELECT * FROM lists ORDER BY created_at DESC' ); ``` With parameters (parameters are reactive — can be a `ref` or `computed`): ```ts const { data: todos } = useQuery( 'SELECT * FROM todos WHERE list_id = ?', [listId] ); ``` Return shape: `{ data: Ref, isLoading: Ref, isFetching: Ref, error: Ref }`. For advanced watch query features including incremental updates and differential results, see [Live Queries / Watch Queries](https://docs.powersync.com/client-sdks/watch-queries.md). #### `useStatus` Reactive connectivity status. Same shape as the React `useStatus` hook — see `references/sdks/powersync-js.md` for the full status field reference. ```ts import { useStatus } from '@powersync/vue'; // omit import in Nuxt const status = useStatus(); // status.connected, status.hasSynced, status.lastSyncedAt, etc. ``` #### `usePowerSync` Access the `PowerSyncDatabase` instance directly for imperative operations. ```ts import { usePowerSync } from '@powersync/vue'; // omit import in Nuxt import { v4 as uuid } from 'uuid'; const powersync = usePowerSync(); await powersync.value.execute( 'INSERT INTO lists (id, created_at, name, owner_id) VALUES (?, ?, ?, ?)', [uuid(), new Date().toISOString(), 'My List', currentUserId] ); ``` `usePowerSync()` returns a `Ref` — access the database via `.value`. ### Setup Context Requirement `usePowerSync`, `useQuery`, and `useStatus` all rely on Vue's `inject()` internally and must be called at the top level of a ` ``` Calling these composables outside of a setup context throws an error. If you need to access the PowerSync database outside a component (e.g. in a store or utility module), export the database instance directly instead of using the composable. ### Example Component ```vue ``` --- ## Nuxt > Alpha: `@powersync/nuxt` is currently in Alpha. APIs and behavior may change. `@powersync/nuxt` is a Nuxt module that wraps `@powersync/vue` and re-exports all its composables. It also adds a Nuxt Devtools integration with a PowerSync diagnostics panel. PowerSync is tailored for client-side applications. Nuxt evaluates plugins server-side unless you use the `.client.ts` suffix. The PowerSync Web SDK requires browser APIs not available in Node.js — it performs no-ops in Node.js rather than throwing errors, but no data is available during SSR. Always create your PowerSync plugin as `plugins/powersync.client.ts`. ### 1. Install ```bash # npm (v7+ installs peer dependencies automatically) npm install @powersync/nuxt@latest # pnpm (must install peer dependencies explicitly) pnpm add @powersync/nuxt@latest @powersync/vue@latest @powersync/web@latest ``` ### 2. `nuxt.config.ts` Add `@powersync/nuxt` to the `modules` array and include the required Vite configuration: ```typescript export default defineNuxtConfig({ modules: ['@powersync/nuxt'], vite: { optimizeDeps: { exclude: ['@powersync/web'] }, worker: { format: 'es' } } }); ``` ### 3. Create the Plugin Create `plugins/powersync.client.ts`. The `.client.ts` suffix ensures this only runs in the browser. ```typescript import { NuxtPowerSyncDatabase, createPowerSyncPlugin } from '@powersync/nuxt'; import { AppSchema } from '~/powersync/AppSchema'; import { PowerSyncConnector } from '~/powersync/PowerSyncConnector'; export default defineNuxtPlugin({ async setup(nuxtApp) { const db = new NuxtPowerSyncDatabase({ database: { dbFilename: 'my-app.sqlite' }, schema: AppSchema }); const connector = new PowerSyncConnector(); await db.init(); await db.connect(connector); const plugin = createPowerSyncPlugin({ database: db }); nuxtApp.vueApp.use(plugin); } }); ``` ### Using Composables All `@powersync/vue` composables (`usePowerSync`, `useQuery`, `useStatus`) are auto-imported in Nuxt — no import statement is needed in components or composables. ```vue ``` The same [setup context requirement](#setup-context-requirement) as standalone Vue applies — call composables at the top level of `