BETA
My accountGet started
May 192021
Total.js Platform
3 min read

Tutorial series - #3 Schemas

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 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. 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.

NOTE: If you are expecting deep nested 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.

NOTE: There is special behaviour with required 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 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

Filter is simple validation for provided query arguments. After method function simply add string comma-separated with query argument and values:

NOTE: If provided query argument is invalid it will simple convert that argument to its default value (just like in Data validation section).

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 name
  • insert method name inside Posts 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:

More about EXEC() function