Introduction
Thetable
primitive lets you define database tables using XanoScript.
Each table corresponds to a database table you could create in Xano’s visual builder — but expressed in code.
Tables will typically:
- Declare their name and description
- Define a schema with fields and data types
- Configure indexes for performance
- Create views for data access patterns
- Set authentication and security settings
Anatomy
Every XanoScript table follows a predictable structure. Here’s a quick visual overview of its main building blocks — from declaration at the top to settings at the bottom.You can find more detail about each section by continuing below.
Declaration
Every table starts with a declarative header that specifies its type, name, and basic configuration.XanoScript
Element | Required | Description |
---|---|---|
table | ✅ | Declares a table primitive. |
table_name | ✅ | The unique name for the table (e.g., user , product ). |
description | no | Optional human-readable description of the table’s purpose. |
auth | no | When true , enables authentication features for this table. |
Section 1: Schema
Theschema
block defines all fields (columns) in your table. Each field specifies its data type, constraints, and validation rules.
XanoScript
- Declare its type (
int
,text
,email
,password
, etc.) - Mark it as optional (
?
) or nullable (?
) - Set default values (
?=default_value
) - Apply filters for validation and transformation (
filters=trim|lower
)
Important: Your schema must begin with an ID field. You can choose one of two types:Changing your primary key type after table creation is not supported.
Learn more about the available data types
Section 2: Indexes
Theindex
block defines database indexes to improve query performance and enforce constraints.
XanoScript
primary
— Primary key constraintbtree
— Standard B-tree index for fast lookupsgin
— Generalized inverted index for complex queriesunique
— Enforces uniqueness (use with|
separator)
- Target single or multiple fields
- Specify sort order (
asc
,desc
) - Enforce uniqueness constraints
- Span multiple fields for composite queries
Section 3: Views
Theviews
block defines database views that control how data is presented and accessed.
XanoScript
- Hide sensitive fields like passwords from API responses
- Create aliases for different access patterns
- Set default sorting for consistent data ordering
- Control data visibility for different use cases
Settings
Table primitives support optional settings for organization and categorization. These settings are defined at the root level of the table block.Setting | Type | Required | Description |
---|---|---|---|
tags | array[string] | no | A list of tags used to categorize and organize the table in your workspace. |
Field Types and Modifiers
Field Modifiers
Fields can be marked as required, nullable, and/or specify a default value:Option | Description |
---|---|
<field_name> | Makes the field required and not nullable |
Example: text name | |
<field_name>? | Makes the field optional and not nullable |
Example: text name? | |
?<field_name>? | Makes the field required but nullable |
Example: text ?name? | |
?<field_name> | Makes the field required and nullable |
Example: text ?name | |
<field_name>?=<value> | Makes the field optional with a default value |
Example: text name?=defaultValue |
Filters
Filters can be applied to fields for validation and data transformation:XanoScript
min:n
— Enforces minimum lengthmax:n
— Enforces maximum lengthminAlpha:n
— Requires minimum alphabetic charactersminDigit:n
— Requires minimum digitspattern:regex
— Validates against regex pattern
trim
— Removes whitespacelower
— Converts to lowercaseupper
— Converts to uppercase
alphaOk
— Whitelists alphabet characters (a-z, A-Z)digitOk
— Whitelists numerical characters (0-9)ok:chars
— Whitelists specific characters (e.g.,ok:.-_
)
startsWith:prefix
— Enforces prefixprevent:blacklist
— Prevents blacklisted phrases
Field Properties
Fields can have additional properties defined within braces. These properties provide metadata and configuration for the field:XanoScript
table
— Specifies the related table for relationship fieldsdescription
— Human-readable description of the field’s purpose
Multi-field Indexes
Indexes can span multiple fields for complex querying and performance optimization:XanoScript
- Composite queries — When you frequently query by multiple fields together
- Performance optimization — Faster lookups for complex WHERE clauses
- Sorting efficiency — Optimized ordering by multiple columns
asc
or desc
).
Detailed Example
Below, you’ll see a complete example of a typical user table.XanoScript
What’s Next
Now that you understand how to define tables in XanoScript, here are a few great next steps:Learn about data types
Explore all the available field types and their specific properties.
Try it out in VS Code
Use the XanoScript VS Code extension with Copilot to write XanoScript in your favorite IDE.
Learn about APIs
Create APIs that interact with your database tables to build complete backend functionality.