Skip to content

Commit be08544

Browse files
committed
docs: collection hooks
1 parent 25de30b commit be08544

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

docs/guide/schema/collection.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,155 @@ const store = await createStore({
8888
The [currying](https://en.wikipedia.org/wiki/Currying) is necessary to specify the type of the item while still letting TypeScript infer the type of the collection. This is a limitation of TypeScript, and [it might improve in the future](https://github.com/microsoft/TypeScript/issues/26242).
8989
:::
9090

91+
## Collection hooks
92+
93+
You can define hooks on the collection that will be called at different stages of the item lifecycle in the `hooks` option. The available hooks are:
94+
95+
- `fetchFirst`: fetch a single item by its key or by other parameters
96+
- `fetchMany`: fetch multiple items
97+
- `create`: create a new item
98+
- `update`: update an existing item
99+
- `delete`: delete an item
100+
101+
::: tip
102+
Instead of defining the hooks in the collection, you can also create a plugin to handle the fetching logic for many collections at once and with a larger choice of hooks (see [Plugins](../plugin/setup.md)).
103+
:::
104+
105+
Each hook receives a payload object with the following properties:
106+
- `fetchFirst`:
107+
- `key` (optional): the key of the item to fetch
108+
- `params` (optional): additional parameters for the fetch
109+
- `include` (optional): dictionnary of related items to include (see [Relations](./relations.md))
110+
- `fetchMany`:
111+
- `params` (optional): additional parameters for the fetch (if available)
112+
- `include` (optional): dictionnary of related items to include (see [Relations](./relations.md))
113+
- `create`:
114+
- `item`: the item to create
115+
- `update`:
116+
- `key`: the key of the item to update
117+
- `item`: the partial item to update
118+
- `delete`:
119+
- `key`: the key of the item to delete
120+
121+
::: code-group
122+
123+
```js [todos.js]
124+
export const todoCollection = defineCollection({
125+
name: 'todos',
126+
hooks: {
127+
async fetchFirst({ key, params, include }) {
128+
// Fetch the item from the server
129+
const response = await fetch(`/api/todos/${key}`)
130+
const data = await response.json()
131+
return data
132+
},
133+
async fetchMany({ params, include }) {
134+
// Fetch the items from the server
135+
const response = await fetch('/api/todos')
136+
const data = await response.json()
137+
return data
138+
},
139+
async create({ item }) {
140+
// Create the item on the server
141+
const response = await fetch('/api/todos', {
142+
method: 'POST',
143+
body: JSON.stringify(item),
144+
headers: { 'Content-Type': 'application/json' },
145+
})
146+
const data = await response.json()
147+
return data
148+
},
149+
async update({ key, item }) {
150+
// Update the item on the server
151+
const response = await fetch(`/api/todos/${key}`, {
152+
method: 'PUT',
153+
body: JSON.stringify(item),
154+
headers: { 'Content-Type': 'application/json' },
155+
})
156+
const data = await response.json()
157+
return data
158+
},
159+
async delete({ key }) {
160+
// Delete the item on the server
161+
await fetch(`/api/todos/${key}`, {
162+
method: 'DELETE',
163+
})
164+
},
165+
},
166+
})
167+
```
168+
169+
```ts [todos.ts]
170+
export const todoCollection = withItemType<TodoType>().defineCollection({
171+
name: 'todos',
172+
hooks: {
173+
async fetchFirst({ key, params, include }) {
174+
// Fetch the item from the server
175+
const response = await fetch(`/api/todos/${key}`)
176+
const data = await response.json()
177+
return data
178+
},
179+
async fetchMany({ params, include }) {
180+
// Fetch the items from the server
181+
const response = await fetch('/api/todos')
182+
const data = await response.json()
183+
return data
184+
},
185+
async create({ item }) {
186+
// Create the item on the server
187+
const response = await fetch('/api/todos', {
188+
method: 'POST',
189+
body: JSON.stringify(item),
190+
headers: { 'Content-Type': 'application/json' },
191+
})
192+
const data = await response.json()
193+
return data
194+
},
195+
async update({ key, item }) {
196+
// Update the item on the server
197+
const response = await fetch(`/api/todos/${key}`, {
198+
method: 'PUT',
199+
body: JSON.stringify(item),
200+
headers: { 'Content-Type': 'application/json' },
201+
})
202+
const data = await response.json()
203+
return data
204+
},
205+
async delete({ key }) {
206+
// Delete the item on the server
207+
await fetch(`/api/todos/${key}`, {
208+
method: 'DELETE',
209+
})
210+
},
211+
},
212+
})
213+
```
214+
215+
```ts [Nuxt]
216+
export const todoCollection = withItemType<TodoType>().defineCollection({
217+
name: 'todos',
218+
hooks: {
219+
fetchFirst: async ({ key, params }) => key
220+
? $fetch(`/api/todos/${key}`, { query: params })
221+
: (await $fetch('/api/todos', { query: params }))[0],
222+
fetchMany: async ({ params }) => $fetch('/api/todos', { query: params }),
223+
create: async ({ item }) => $fetch('/api/todos', {
224+
method: 'POST',
225+
body: item,
226+
}),
227+
update: async ({ key, item }) => $fetch(`/api/todos/${key}`, {
228+
method: 'PATCH',
229+
body: item,
230+
}),
231+
delete: async ({ key }) => $fetch(`/api/todos/${key}`, {
232+
method: 'DELETE',
233+
}),
234+
},
235+
})
236+
```
237+
238+
:::
239+
91240
## Item Key
92241

93242
rstore uses a normalized cache to store the data. This means that each item is stored in a flat structure, and the key is used to identify the item in the cache.

0 commit comments

Comments
 (0)