Skip to main content
In this tutorial, we’ll build a complete Todo app using Modelence. You’ll learn how to:
  • Create MongoDB stores with TypeScript schemas
  • Build modules with queries and mutations
  • Create React components that interact with your backend
This tutorial assumes you’ve already created a Modelence project and completed the setup. If you haven’t done so, please complete those steps first.

Step 1: Create a Todo Store

Stores in Modelence are MongoDB collections with built-in TypeScript support, schema and helper methods. They help you to:
  • Define type-safe schemas for your data
  • Handle CRUD operations with MongoDB
  • Add custom methods to your documents
  • Configure indexes for better performance

Set up the project structure

The recommended approach in Modelence is to group code by modules/domains into separate directories. For our Todo app, create an src/server/todo directory and add a db.ts file:
src/server/todo/db.ts

Using the Store

Once defined, you can use your Store object to perform operations on your collection:

Working with Documents

Stores provide a comprehensive set of methods for working with MongoDB documents, including finding, inserting, updating, and deleting records. All methods are fully typed with TypeScript. See the Store API Reference for a complete list of available methods and their usage.
Stores automatically handle MongoDB connection management, collection provisioning and index creation. Just define your Store and start using it - Modelence takes care of the rest.

Step 2: Create a Todo Module

Modules are the core building blocks of a Modelence application. They help you organize your application’s functionality into cohesive units that can contain queries, mutations, stores, cron jobs and configurations. Create a new file at src/server/todo/index.ts:
src/server/todo/index.ts

Include the Module

Now, add the Module to your main server file at src/server/app.ts:
src/server/app.ts
As soon as your app starts, Modelence will:
  • Provision the dbTodos store in MongoDB
  • Make the queries and mutations available for calling

Step 3: Create the Frontend

Modelence is frontend-agnostic, so you are free to use any routing library you like. We will use React Router for this example, which is what’s included in the default Modelence starter.

Add a new route

Edit src/client/routes.ts to add a new route for our todos:
src/client/routes.ts

Create the TodosPage component

Create a new component at src/client/TodosPage.tsx:
src/client/TodosPage.tsx

Complete Example

Want to see the full working code? Check it out on GitHub, along with other examples:

Complete Todo App Example

See the complete source code for this tutorial on GitHub, including all files and additional features.