Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66ac393

Browse files
committed
docs: add section on working with reactive parameters in queries
1 parent 82386c6 commit 66ac393

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,46 @@ Each model gets these mutation hooks:
154154

155155
## Advanced Features
156156

157+
### Working with Reactive Parameters
158+
159+
Pinia Colada automatically tracks reactive dependencies in your queries. When using reactive values (refs, computed), wrap your query arguments in a getter function:
160+
161+
```typescript
162+
import { ref, computed } from 'vue'
163+
164+
const userId = ref('123')
165+
const includeDeleted = ref(false)
166+
167+
// ✅ Correct: Use a getter function
168+
const { data: posts } = queries.post.useFindMany(() => ({
169+
where: {
170+
authorId: userId.value, // Unwrap refs inside the getter
171+
deletedAt: includeDeleted.value ? undefined : null
172+
},
173+
}))
174+
175+
// When userId or includeDeleted changes, the query automatically re-runs!
176+
```
177+
178+
**Why use a getter function?**
179+
180+
The getter function `() => ({...})` allows Pinia Colada to track when your reactive values change and automatically re-fetch the query. Inside the getter, unwrap refs with `.value`.
181+
182+
**Alternative patterns:**
183+
184+
```typescript
185+
// Using computed (also works)
186+
const queryArgs = computed(() => ({
187+
where: { authorId: userId.value }
188+
}))
189+
const { data } = queries.post.useFindMany(queryArgs)
190+
191+
// Static queries (no reactivity needed)
192+
const { data } = queries.post.useFindMany({
193+
where: { published: true } // No getter needed for static values
194+
})
195+
```
196+
157197
### Optimistic Updates
158198

159199
Optimistic updates allow the UI to update immediately before the server responds:

0 commit comments

Comments
 (0)