If you’re diving into Total.js development, you’ve chosen a robust, modern framework that’s packed with powerful features for building fast, scalable applications. At the heart of Total.js are two essential concepts that will guide much of your backend logic: schemas and actions.
Schemas and actions might seem complex at first, but once you understand how they work together, they’ll become some of your favorite tools in the framework. In this guide, I’ll break down these concepts, show you
how to set them up, and walk you through practical examples that will empower you to create an organized, flexible codebase.
Let's get started!
In Total.js, schemas and actions play distinct roles that complement each other:
Think of schemas as structured “folders” where you organize your data logic, while actions are the “tools” that operate on that data. You’ll be able to call actions as needed, whether they belong to a schema or stand alone, and they’re especially useful for handling backend operations like processing user input or interacting with a database.
A schema in Total.js is more than just a data structure. It’s a powerful way to group related actions that handle specific tasks for a particular type of data. Here’s a simple example of creating a schema for Users
.
In your total.js application create a folder schemas
and then create a file users.js
inside to paste the following:
In this example, we created a schema called Users
with three actions:
name
and email
).By organizing these actions within the Users
schema, you create a cohesive set of operations for handling user data.
To make these schema actions accessible via HTTP requests, you can link them to routes in Total.js. This will allow your frontend or API clients to call these actions. Just create a controller file controllers/api.js
to put the following:
In this example:
GET
request to /api/users/
will call the list
action.POST
request to /api/users/insert
will invoke the insert
action.GET
request to /api/users/{id}/
will trigger the read
action with a unique user ID passed in the URL.This structure keeps your routing and backend logic organized and straightforward.
While schema actions are great for data-centric tasks, sometimes you’ll need an action that performs a standalone operation, not linked to a specific schema. Standalone actions are defined independently and can be used for any custom backend functionality.
Total.js recommand to create your actions file in actions
folder. But you can put them somewhere else like (schemas,definitions,modules,etc
)
Let’s say we want to create a standalone action called find
. This action accepts query parameters for pagination and sorting without being tied to a schema.
Let us create this file in actions/default.js
and copy/paste the following:
This find
action can be called to handle any general-purpose data search, making it a flexible solution for various backend needs.
Just like schema actions, standalone actions need to be linked to routes to be accessed from your API or frontend.
With this route, a GET
request to /api/find/
will call the standalone find
action.
Total.js provides the ACTION()
method, a way to call both schema-related and standalone actions in your code.
Let’s call the Users/insert
action we created earlier using ACTION()
:
Here, we specify the Users/insert
action and pass a search query to find users with “John” in their data. You can use this approach to call any schema or standalone action, making ACTION()
a highly flexible tool.
Total.js allows you to call actions within the same schema using the $.action
method. This feature is powerful for creating complex workflows that involve multiple steps.
Let’s say we want an action in the Users
schema to first list users, then perform additional processing based on that list.
In this example, proccess
calls list
within the Users
schema. This structure is great for chaining operations within schemas and gives you full control over workflow management.
Total.js v5 allows so many other usages of NEWSCHEMA
and NEWACTION
for very specific cases. We are not going to cover them here because we are preparing another blog post for them. But if you are curious about learning them you can check this blog post
Working with schemas and actions in Total.js can elevate your application’s structure and efficiency. Here are some key points to keep in mind:
ACTION()
for flexible action calls, whether they’re schema-based or standalone.With these foundations in place, you’re ready to start building robust, modular backend logic in Total.js! Experiment with different schema and action configurations, and don’t hesitate to explore Total.js’s extensive documentation as you continue to master this flexible framework.
Happy coding!