Basic Task App
To get started right away without having to build an app from scratch, explore with the Ditto ready-to-use demo task app.
This article provides a quickstart on building and interacting with a basic task app for managing and tracking task items.
Code Overview with Comments
Following is an overview of the installation, setup, and build process with comments. For a step-by-step walkthrough:
For the full source code, see getditto > samples > nodejs > index.ts repository in GitHub.
Installing and Setting Up Ditto
Building and Running the App
Creating New Task Documents
Editing Task Documents
Replicating Data
Installing and Setting Up Ditto
Create a new directory, navigate to it, and then initialize npm
:
Install the @dittolive/ditto
and readline
package:
Open the current directory in your IDE. For example, if using Visual Studio Code:
Import and initialize Ditto:
-
Create an
index.js
file, and then import, initialize, instantiate, and start Ditto by adding the following to it. -
Replace
'YOUR_APP_ID'
and'YOUR_TOKEN'
with your access credentials.
To get your access credentials, you must create an account and register an app in the portal. For instructions, see Get Started.
Setting Up the App
Once you’ve installed and set up your environment with Ditto, create a command-line interface (CLI) along with the various placeholder logic, or stubs, for the methods you’ll use to perform various operations:
Building and Running the App
Now that you have the app’s CLI implemented, build and run the task app:
The following output CLI indicates that the demo task app is running.
Performing Reads
Now that the setup is complete, create a live query. A live query is a mechanism that allows you to track specific changes stored within the local environment, referred to as the Ditto store.
Relevant documents are stored in a local tasks
object; as these documents change, the tasks
object automatically updates to reflect their most current state that automatically updates to reflect the latest state. As data changes occur, the tasks
object updates to reflect the latest state.
Once you’ve installed and set up the Ditto SDK, create a live query that will allow us to see when changes we care about happen to our local store. We store any relevant documents in a local tasks
object and update the tasks
object as changes happen using the live query.
We’ll also implement our --list
command to return known documents.
Implement live query logic and --list
command
Run the --list
command in the console. The output should be an empty array.
Creating New Task Documents
We’ll now extend our application with the ability to create new task documents in the Ditto store using the Upsert method. We’ll do this by implementing the --create
command in our application.
Implement --create
command logic
Run the --create My First Task
command.
Run the --list
command.
By default, the document _id
is automatically generated.
You can now open the data browser in the Ditto Portal to see the changes in the Big Peer. (https://portal.ditto.live/data-browser/{APP-ID})
Editing Task Documents
We’ll now extend our application to support editing documents. To do this we’ll introduce:
- The ability to mark a file as deleted by setting the
isDeleted
field totrue
. - The ability to change the completed state by being able to
toggle
theisComplete
field
Add logic for --toggle
and --deleted
input.
Run the --list
command to get known documents. (Note: The _id
is generated and will be different)
Run the --toggle
command with the _id
of the document you want to change isCompleted
state of. In our case it’s document 64c9b6480049a0c400c95c51
.
Run the --list
command to see state update.
Run the --delete
command with the _id
of the document you want to set to deleted. In our case it’s document 64c9b6480049a0c400c95c51
.
Run the --list
command to see state update. We now have an empty list because our live query is only subscribing to documents that have "isDeleted == false"
Replicating Data
Finally, we’ll extend our application to replicate data from other locally connected peers. We’ll do this by adding a subscription with a query that matches our live query.
Add a subscription with a query that looks for documents in the tasks
collection with "isDeleted == false"