Skip to main content

3 - Create tasks

See the full source code here

3-1 Create a command

In the command line, the user can input a task they want to create using readline. We will listen to four different commands:

  1. --insert. We will parse the string without --insert and create a new task with the body.
  2. --list. We print out the tasks. Every time a task is changed, both locally and by other devices, the global tasks variable is updated from observeLocal
  3. --toggle. We will parse the string without --toggle and assume the user's input is a Task document's id. We can then find the document by its id and call .update
  4. --delete. We will parse the string without --delete and assume the user's input is a Task document's id. We can then find the document by its id and toggle the isDeleted property

import { init, Ditto, Document } from '@dittolive/ditto'import * as readline from 'readline/promises'import { stdin as input, stdout as output } from 'node:process';
let dittolet subscriptionlet liveQuerylet tasks: Document[] = []
async function main () {  await init()
  ditto = new Ditto({ type: 'onlinePlayground', appID: 'YOUR_APP_ID', token: 'YOUR_TOKEN' })  ditto.startSync()
  subscription = ditto.store.collection("tasks").find("isDeleted == false").subscribe()  liveQuery = ditto.store.collection("tasks").find("isDeleted == false").observeLocal((docs, event) => {    tasks = docs  })  let isAskedToExit = false    console.log("************* Commands *************");  console.log("--insert my new task");  console.log("   Inserts a task");  console.log("   Example: \"--insert Get Milk\"");  console.log("--toggle myTaskTd");  console.log("   Toggles the isComplete property to the opposite value");  console.log("   Example: \"--toggle 1234abc\"");  console.log("--delete myTaskTd");  console.log("   Deletes a task");  console.log("   Example: \"--delete 1234abc\"");  console.log("--list");  console.log("   List the current tasks");  console.log("--exit");  console.log("   Exits the program");  console.log("************* Commands *************");
  const rl = readline.createInterface({ input, output });  while (!isAskedToExit) {
      let answer = await rl.question('Your command:')      if (answer.startsWith("--insert")) {        let body = answer.replace("--insert ", "")        ditto.store.collection("tasks").upsert({          body,          isDeleted: false,          isCompleted: false        })      }      if (answer.startsWith("--toggle")) {        let id = answer.replace("--toggle ", "")        ditto.store.collection("tasks")        .findByID(id).update((doc) => {          let isCompleted = doc.value.isCompleted          doc.at("isCompleted").set(!isCompleted)        })      }      if (answer.startsWith("--list")) {        console.log(tasks.map((task) => task.value))      }      if (answer.startsWith("--delete")) {        let id = answer.replace("--delete ", "")        ditto.store.collection("tasks")        .findByID(id).update((doc) => {          doc.at("isDeleted").set(true)        })      }      if (answer.startsWith("--exit")) {        ditto.stopSync()        process.exit()              }  }
}
main()
New and Improved Docs

Ditto has a new documentation site at https://docs.ditto.live. This legacy site is preserved for historical reference, but its content is not guaranteed to be up to date or accurate.