@@ -253,28 +253,40 @@ root.render(
253253)
254254```
255255
256- 3 . In your ` App ` component, you can now use hooks like ` usePendingCursorOperation ` or ` usePendingIDSpecificOperation ` to get your documents like so :
256+ 3 . In your ` App ` component, you can now use the DQL hooks ` useQuery ` and ` useExecuteQuery ` to interact with your documents:
257257
258258``` tsx
259- import { usePendingCursorOperation , useMutations } from ' @dittolive/react-ditto'
259+ import { useQuery , useExecuteQuery } from ' @dittolive/react-ditto'
260+
261+ interface Task {
262+ _id: string
263+ text: string
264+ isCompleted: boolean
265+ }
260266
261267export default function App() {
262- const { documents } = usePendingCursorOperation ({
263- collection: ' tasks' ,
264- })
268+ // Query documents using DQL
269+ const { documents } = useQuery <Task >(' SELECT * FROM tasks WHERE isCompleted = false' )
265270
266- const { removeByID, upsert } = useMutations ({ collection: ' tasks' })
271+ // Execute DQL mutations
272+ const [upsert] = useExecuteQuery <void , { value: Partial <Task > }>(
273+ ' INSERT INTO tasks DOCUMENTS (:value) ON ID CONFLICT DO UPDATE'
274+ )
275+
276+ const [removeByID] = useExecuteQuery <void , { id: string }>(
277+ ' UPDATE tasks SET isDeleted = true WHERE _id = :id'
278+ )
267279
268280 return (
269281 <>
270- <button onClick = { () => upsert ({ value: { text: ' Hello' } })} >
282+ <button onClick = { () => upsert ({ value: { text: ' Hello' , isCompleted: false } })} >
271283 Add Task
272284 </button >
273285 <ul >
274286 { documents .map ((doc ) => (
275287 <li key = { doc ._id } >
276- { JSON . stringify ( doc .value ) }
277- <button onClick = { () => removeByID ({ _id : doc .id })} >remove</button >
288+ { doc .text }
289+ <button onClick = { () => removeByID ({ id : doc ._id })} >remove</button >
278290 </li >
279291 ))}
280292 </ul >
@@ -283,28 +295,53 @@ export default function App() {
283295}
284296```
285297
286- Alternatively, you can also choose to go with the lazy variants of these hooks (` useLazyPendingCursorOperation ` and ` useLazyPendingIDSpecificOperation ` ), in order to launch queries on the data store as a response to a user event:
298+ The ` useQuery ` hook automatically subscribes to changes and updates your component when the query results change. The ` useExecuteQuery ` hook returns a function that executes DQL mutations when called. It can also be used to lazily execute non-mutating queries in responce to user actions.
299+
300+ For more complex scenarios, you can also pass parameters to your queries:
287301
288302``` tsx
289- import { useLazyPendingCursorOperation } from ' @dittolive/react-ditto'
303+ import { useQuery , useExecuteQuery } from ' @dittolive/react-ditto'
290304
291305export default function App() {
292- const { documents, exec } = useLazyPendingCursorOperation ()
306+ const [filter, setFilter] = useState <' all' | ' completed' | ' active' >(' all' )
307+
308+ // Query with parameters
309+ const { documents } = useQuery <Task >(
310+ ' SELECT * FROM tasks WHERE (:filter = "all" OR isCompleted = :showCompleted)' ,
311+ {
312+ args: {
313+ filter ,
314+ showCompleted: filter === ' completed'
315+ }
316+ }
317+ )
293318
294- if (! documents ?.length ) {
295- return (
296- <button onClick = { () => exec ({ collection: ' tasks' })} >
297- Click to load!
298- </button >
299- )
300- }
319+ // Update task completion status
320+ const [setCompleted] = useExecuteQuery <void , { id: string , isCompleted: boolean }>(
321+ ' UPDATE tasks SET isCompleted = :isCompleted WHERE _id = :id'
322+ )
301323
302324 return (
303- <ul >
304- { documents .map ((doc ) => (
305- <li key = { doc ._id } >{ JSON .stringify (doc .value )} </li >
306- ))}
307- </ul >
325+ <div >
326+ <select value = { filter } onChange = { (e ) => setFilter (e .target .value as any )} >
327+ <option value = " all" >All</option >
328+ <option value = " completed" >Completed</option >
329+ <option value = " active" >Active</option >
330+ </select >
331+
332+ <ul >
333+ { documents .map ((doc ) => (
334+ <li key = { doc ._id } >
335+ <input
336+ type = " checkbox"
337+ checked = { doc .isCompleted }
338+ onChange = { (e ) => setCompleted ({ id: doc ._id , isCompleted: e .target .checked })}
339+ />
340+ { doc .text }
341+ </li >
342+ ))}
343+ </ul >
344+ </div >
308345 )
309346}
310347```
0 commit comments