Total.js Schemas are a place for handling data in your Total.js applications. This tutorial will explain Total.js Schemas more.
Tutorial series - #3 Schemas
Schemas are one of the Total.js core feature. With schemas you can control incoming data and convert them to your defined types. You can use them as main data handlers for you operations like listing, creating, updating or removing data.
Example of simple schema:
Schema Users
has two fields:
name
which must be type string and is required parameter.age
which must be type number and is not required
After executing query
method in Users
schema, you will recieve success object with value of provided name in field name
and with age (if was provided).
Data validation
Data validation will help you with converting values, setting default values, making fields required and allowing only relevant fields. If recieved value is not defined in schema it will be automatically removed.
Some of the most used data types:
String
- you can also define maximum length of stringString(25)
, everything over 25 characters will be removedNumber
- can be also float numberEmail
- has special validation to accept only valid email (e.g.xxxx@xxx.xx
)
All data types can be found in Total.js's documentation
Default values:
Most of the data types has pre-defined default values so there is no need to worry about recieveing null
or undefined
in String
for example.
Nested object:
Sometimes you want to validate nested objects inside your schema for more complex data structure. There are two ways to declare nested object inside schema. Creating new dedicated schema or using inline declaring.
Object
and you don't care about structure, you can simply use JSON
or Object
data type.Required values
To make value required you can use third argument inside schema.define()
which can be Boolean
or Function
. If you are using custom validation function make sure to return true
or false
. Fourth argument is custom error message.
Boolean
. If Boolean
field is false
but its flagged as required in schema.define
it will throw error for user. This can be useful with field for accepting terms of use.Methods
Schema methods are usually main logic of every Total.js application. They recieve formatted and relatively safe data ready to be used thanks to schema's data type definintions.
Total.js provides predefined schema methods names which are commonly used inside almost every application. You can also define your own name through addWorkflow
. Predefined names with meant but not mandatory usage:
setQuery
: List of itemssetRead
: Details of itemsetInsert
: Create new itemsetSave
: Create new item or update existing itemsetUpdate
: Update itemsetPatch
: Update existing item (partly)setRemove
: Remove item
Filter
Filter is simple validation for provided query arguments. After method function simply add string comma-separated with query argument and values:
Usage
In most cases you want use schema methods with routes -ROUTE
. After hitting endpoint schema will handle request.
Multiple execution
Executing multiple schema methods in single request is also possible. Methods are executed one by one but previous method needs to be successful - $.success()
Typing (response)
after method name will tell controller to apply response from insert
method only. All other responses will be ignored. HTTP response will be Response #2
.
Manual execution
Sometimes you want execute schema manually without route. To achive that use global function called EXEC()
:
First argument is directing EXEC()
function to requested schema method:
+
validate data from selected schema (-
will ignore require flag of fields)Posts
schema nameinsert
method name insidePosts
schema
Second argument is object with body data that will be recieved inside our schema (can be null
). Last argument is response callback with error
or response
data. If you want inhert request with headers and cookies you can pass controller instance as fourth optional argument or add customer user object:
Other posts from Total.js Platform
- 2024-11-13Benchmarking Node.js Frameworks: selecting your framework for 2025!
- 2024-11-01October report 2024
- 2024-10-22Performance Testing: Total.js vs. NestJS
- 2024-10-01September report 2024
- 2024-09-27Total.js UI Builder: #2 designing your first interface
- 2024-09-26Total.js V5: Middlewares
- 2024-09-23Beginner Guide to Total.js UI: # 05 Client-side routing
- 2024-09-23Total.js UI #4: Data Binding (Part 2 – Practical Example)
- 2024-09-20Introduction to Total.js UI Builder: A Beginner’s Guide
- 2024-09-13Total.js v5: #06 Understanding File Routing