MCPcopy Create free account
hub / github.com/TanStack/db / createTodoActions

Function createTodoActions

examples/electron/offline-first/src/db/todos.ts:282–326  ·  view source on GitHub ↗
(
  offline: ReturnType<typeof createTodos>['executor'],
)

Source from the content-addressed store, hash-verified

280
281// Helper to create offline actions
282export function createTodoActions(
283 offline: ReturnType<typeof createTodos>['executor'],
284) {
285 const addTodo = offline.createOfflineAction({
286 mutationFnName: 'syncTodos',
287 onMutate: (text: string) => {
288 const now = new Date().toISOString()
289 const newTodo: Todo = {
290 id: crypto.randomUUID(),
291 text: text.trim(),
292 completed: false,
293 createdAt: now,
294 updatedAt: now,
295 }
296 collection.insert(newTodo)
297 return newTodo
298 },
299 })
300
301 const toggleTodo = offline.createOfflineAction({
302 mutationFnName: 'syncTodos',
303 onMutate: (id: string) => {
304 const todo = collection.get(id)
305 if (!todo) return
306 collection.update(id, (draft) => {
307 draft.completed = !draft.completed
308 draft.updatedAt = new Date().toISOString()
309 })
310 return todo
311 },
312 })
313
314 const deleteTodo = offline.createOfflineAction({
315 mutationFnName: 'syncTodos',
316 onMutate: (id: string) => {
317 const todo = collection.get(id)
318 if (todo) {
319 collection.delete(id)
320 }
321 return todo
322 },
323 })
324
325 return { addTodo, toggleTodo, deleteTodo }
326}

Callers 1

TodoListFunction · 0.90

Calls 5

createOfflineActionMethod · 0.80
insertMethod · 0.45
getMethod · 0.45
updateMethod · 0.45
deleteMethod · 0.45

Tested by

no test coverage detected