@@ -67,6 +67,7 @@ const { data, insert, update, delete: deleteFn } = useCollection({
67
67
```
68
68
69
69
Returns:
70
+
70
71
- ` data ` : An array of all items in the collection
71
72
- ` state ` : A Map containing all items in the collection with their internal keys
72
73
- ` insert ` : Function to add new items to the collection
@@ -87,6 +88,7 @@ await preloadCollection({
87
88
```
88
89
89
90
Features:
91
+
90
92
1 . Returns a promise that resolves when the first sync commit is complete
91
93
2 . Shares the same collection instance with ` useCollection `
92
94
3 . Handles already-loaded collections by returning immediately
@@ -98,16 +100,16 @@ Features:
98
100
99
101
``` typescript
100
102
// Insert a single item
101
- insert ({ text: " Buy groceries" , completed: false });
103
+ insert ({ text: " Buy groceries" , completed: false })
102
104
103
105
// Insert multiple items
104
106
insert ([
105
107
{ text: " Buy groceries" , completed: false },
106
- { text: " Walk dog" , completed: false }
107
- ]);
108
+ { text: " Walk dog" , completed: false },
109
+ ])
108
110
109
111
// Insert with custom key
110
- insert ({ text: " Buy groceries" }, { key: " grocery-task" });
112
+ insert ({ text: " Buy groceries" }, { key: " grocery-task" })
111
113
```
112
114
113
115
#### Update
@@ -116,30 +118,34 @@ We use a proxy to capture updates as immutable draft optimistic updates.
116
118
117
119
``` typescript
118
120
// Update a single item
119
- update (todo , (draft ) => { draft .completed = true });
121
+ update (todo , (draft ) => {
122
+ draft .completed = true
123
+ })
120
124
121
125
// Update multiple items
122
126
update ([todo1 , todo2 ], (drafts ) => {
123
- drafts .forEach (draft => { draft .completed = true });
124
- });
127
+ drafts .forEach ((draft ) => {
128
+ draft .completed = true
129
+ })
130
+ })
125
131
126
132
// Update with metadata
127
- update (todo , { metadata: { reason: " user update" } }, (draft ) => {
128
- draft .text = " Updated text" ;
129
- });
133
+ update (todo , { metadata: { reason: " user update" } }, (draft ) => {
134
+ draft .text = " Updated text"
135
+ })
130
136
```
131
137
132
138
#### Delete
133
139
134
140
``` typescript
135
141
// Delete a single item
136
- delete ( todo );
142
+ delete todo
137
143
138
144
// Delete multiple items
139
- delete ( [todo1 , todo2 ]);
145
+ delete [todo1 , todo2 ]
140
146
141
147
// Delete with metadata
142
- delete (todo , { metadata: { reason: " completed" } });
148
+ delete (todo , { metadata: { reason: " completed" } })
143
149
```
144
150
145
151
### Schema Validation
@@ -148,11 +154,15 @@ Collections can optionally include a [standard schema](https://github.com/standa
148
154
149
155
``` typescript
150
156
const todoCollection = useCollection ({
151
- id: ' todos' ,
152
- sync: { /* sync config */ },
153
- mutationFn: { /* mutation functions */ },
154
- schema: todoSchema // Standard schema interface
155
- });
157
+ id: " todos" ,
158
+ sync: {
159
+ /* sync config */
160
+ },
161
+ mutationFn: {
162
+ /* mutation functions */
163
+ },
164
+ schema: todoSchema , // Standard schema interface
165
+ })
156
166
```
157
167
158
168
## Transaction Management
@@ -163,6 +173,7 @@ The library includes a robust transaction management system:
163
173
- ` TransactionStore ` : Provides persistent storage for transactions using IndexedDB
164
174
165
175
Transactions progress through several states:
176
+
166
177
1 . ` pending ` : Initial state when a transaction is created
167
178
2 . ` persisting ` : Transaction is being persisted to the backend
168
179
3 . ` completed ` : Transaction has been successfully persisted
@@ -241,31 +252,31 @@ export async function loader() {
241
252
// Example usage in a component
242
253
function TodoList() {
243
254
const { data, insert, update, delete : remove } = useCollection (todosConfig );
244
-
255
+
245
256
const addTodo = () => {
246
257
insert ({ title: ' New todo' , completed: false });
247
258
};
248
-
259
+
249
260
const toggleTodo = (todo ) => {
250
261
update (todo , (draft ) => {
251
262
draft .completed = ! draft .completed ;
252
263
});
253
264
};
254
-
265
+
255
266
const removeTodo = (todo ) => {
256
267
remove (todo );
257
268
};
258
-
269
+
259
270
return (
260
271
<div >
261
272
< button onClick = {addTodo }> Add Todo < / button >
262
273
<ul >
263
274
{data .map (todo = > (
264
275
< li key = {todo.id }>
265
- < input
266
- type = " checkbox"
267
- checked = {todo.completed }
268
- onChange = {() => toggleTodo(todo )}
276
+ < input
277
+ type = " checkbox"
278
+ checked = {todo.completed }
279
+ onChange = {() => toggleTodo(todo )}
269
280
/ >
270
281
{todo .title }
271
282
< button onClick = {() => removeTodo(todo )}> Delete < / button >
0 commit comments