- 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 ansrc/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.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 atsrc/server/todo/index.ts:
src/server/todo/index.ts
Include the Module
Now, add the Module to your main server file atsrc/server/app.ts:
src/server/app.ts
- Provision the
dbTodosstore 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
Editsrc/client/routes.ts to add a new route for our todos:
src/client/routes.ts
Create the TodosPage component
Create a new component atsrc/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.