Skip to content

Commit 0fa12ca

Browse files
committed
docs: more!
1 parent 20e07c3 commit 0fa12ca

File tree

3 files changed

+94
-3
lines changed

3 files changed

+94
-3
lines changed

docs/guide/data/query.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ The `queryFirst` and `queryMany` composables return an object with the following
1414

1515
- `refresh`: a function that can be called to refresh the data.
1616

17+
The composables also return a promise so they can be used with async setup.
18+
19+
```vue
20+
<script setup>
21+
const store = useStore()
22+
// Plays nice with Nuxt SSR!
23+
const { data: todos } = await store.Todo.queryMany()
24+
</script>
25+
```
26+
1727
### Query first
1828

1929
Composable function that reads the first item that matches the key or the filter in the cache and fetches it if not found.
@@ -166,3 +176,80 @@ const { data: todos } = store.Todo.queryMany({
166176
* `fetch-only` means that the query will only fetch the data from the adapter plugins. The data will be stored in the cache when the query is resolved.
167177
* `cache-only` means that the query will only fetch the data from the cache.
168178
* `no-cache` means that the query will not use the cache and only fetch the data from the adapter plugins. No data will be stored in the cache.
179+
180+
## Items list
181+
182+
Usually we write list item components that are used to display a list of items. It is recommended to only pass the key to the list item component and then use `queryFirst` to fetch the data in the list item component. By default the fetch policy is `cache-first`, so the data will be red from the cache in the item component and no unnecessary requests will be made for each items.
183+
184+
The benefits are that the data is co-located with the component that uses it and there is no need to specify the types of the item prop again (usually a simple `id: string | number` is enough). The item component is also easier to potentially reuse in totally different contexts.
185+
186+
::: code-group
187+
188+
```vue [TodoList.vue]
189+
<script lang="ts" setup>
190+
const store = useStore()
191+
const { data: todos } = await store.Todo.queryMany()
192+
</script>
193+
194+
<template>
195+
<!-- Only pass the id here -->
196+
<TodoItem
197+
v-for="{ id } in todos"
198+
:id
199+
:key="id"
200+
/>
201+
</template>
202+
```
203+
204+
```vue [TodoItem.vue]
205+
<script lang="ts" setup>
206+
const props = defineProps<{
207+
// Easy to type prop
208+
id: string
209+
}>()
210+
211+
const store = useStore()
212+
// No additional request is made here, the data is read from the cache
213+
const { data: todo } = await store.Todo.queryFirst(props.id)
214+
// Enjoy inferred types here too!
215+
</script>
216+
```
217+
218+
:::
219+
220+
## Co-locating queries
221+
222+
The best of both worlds! You can co-locate the queries with the components that use them, while still enjoying the benefits of a centralized store thanks to the cache -- letting it deduplicating and synchronizing all components. This is a powerful pattern that improves the independence of components and thus their maintainability.
223+
224+
::: code-group
225+
226+
```vue [AppHeader.vue]
227+
<script lang="ts" setup>
228+
const store = useStore()
229+
// The query is co-located with the component, not in a store somewhere else
230+
const { data: user } = await store.User.queryFirst('user-id')
231+
</script>
232+
233+
<template>
234+
<div>
235+
<h1>{{ user.name }}</h1>
236+
</div>
237+
</template>
238+
```
239+
240+
```vue [UserProfile.vue]
241+
<script lang="ts" setup>
242+
const store = useStore()
243+
// The centralized cache ensures that the data is up to date here too
244+
const { data: user } = await store.User.queryFirst('user-id')
245+
</script>
246+
247+
<template>
248+
<div>
249+
<h1>{{ user.name }}</h1>
250+
<p>{{ user.email }}</p>
251+
</div>
252+
</template>
253+
```
254+
255+
:::

docs/guide/getting-started.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ rstore is a data store allowing you to handle all data in your application.
44

55
Define a data model and then run queries or execute mutations (create, update and delete) on your data.
66

7-
Its main features are:
7+
**FEATURES**
88

99
- **Normalized reactive cache** to ensure all components are up-to-date
10+
- **Co-locate queries** within the components that need them
1011
- **Fully adaptable** with plugins to fetch from any source (REST, GraphQL...)
1112
- **Scale down** to small prototypes and **scale up** to big enterprise apps
1213
- Query API designed for **local-first** and **realtime**
14+
- **Form API** to handle form state and validation
1315
- **TypeScript support** with full autocomplete
16+
- **Nuxt module** with devtools
1417

1518
[Learn more](./learn-more.md)
1619

docs/guide/learn-more.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ The reactive normalized cache in rstore is a key feature that ensures your appli
4646

4747
The cache is also reactive, meaning reading from the cache in a `computed` will always keep the components updated. In fact, each time you use `store.Todo.queryMany()` you get a computed ref that reads from the cache.
4848

49-
::: details
50-
5149
The reactive normalized cache in rstore offers several benefits.
5250
- Consistency is maintained by normalizing the data, ensuring a single source of truth for each item, which prevents duplication and inconsistencies.
5351
- Reactivity is another advantage, as the cache automatically updates any part of your application that depends on the data whenever changes occur, keeping your UI in sync with the latest state.
5452
- Efficiency is achieved through the structured format of normalized data, allowing for quicker and less overhead-intensive queries and updates.
53+
- Co-locating the data requirements with the components that use them is a powerful pattern. By using the `queryMany` and `queryFirst` composables, you can easily fetch and display data in your components without worrying about deduplicating or synchronizing with other components.
54+
55+
::: details Example of cache
5556

5657
Here is what the cache can look like:
5758

0 commit comments

Comments
 (0)