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:
Users has two fields:
name which must be type string and is required parameter.
age which must be type number and is not required
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 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 string
String(25), everything over 25 characters will be removed
Number - can be also float number
Email - has special validation to accept only valid email (e.g.
All data types can be found in Total.js's documentation
Most of the data types has pre-defined default values so there is no need to worry about recieveing
String for example.
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
Object data type.
To make value required you can use third argument inside
schema.define() which can be
Function. If you are using custom validation function make sure to return
false. Fourth argument is custom error message.
Boolean field is
false but its flagged as required in
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 items
setRead: Details of item
setInsert: Create new item
setSave: Create new item or update existing item
setUpdate: Update item
setPatch: Update existing item (partly)
setRemove: Remove item
Filter is simple validation for provided query arguments. After method function simply add string comma-separated with query argument and values:
In most cases you want use schema methods with routes -
ROUTE. After hitting endpoint schema will handle request.
Executing multiple schema methods in single request is also possible. Methods are executed one by one but previous method needs to be successful -
(response) after method name will tell controller to apply response from
insert method only. All other responses will be ignored. HTTP response will be
Sometimes you want execute schema manually without route. To achive that use global function called
First argument is directing
EXEC() function to requested schema method:
+ validate data from selected schema (
- will ignore require flag of fields)
Posts schema name
insert method name inside
Second argument is object with body data that will be recieved inside our schema (can be
null). Last argument is response callback with
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: