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 requiredAfter 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 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 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.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 itemFilter
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.
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 inside Posts
schemaSecond 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: