Working with Data Models
15 minJordan Lee
Nexus uses a flexible schema system that lets you define custom data models for your application. This guide covers creating schemas, defining relationships, validating data, and querying your models efficiently.
Defining a Schema
Schemas are defined using our declarative JSON format:
{
"name": "product",
"fields": [
{ "name": "title", "type": "string", "required": true },
{ "name": "price", "type": "number", "min": 0 },
{ "name": "category", "type": "relation", "target": "category" },
{ "name": "tags", "type": "string[]", "maxItems": 10 }
]
}Relationships
Nexus supports one-to-one, one-to-many, and many-to-many relationships between models. Use the relation field type and specify the target model:
{ "name": "author", "type": "relation", "target": "user", "cardinality": "one" }
{ "name": "tags", "type": "relation", "target": "tag", "cardinality": "many" }Querying Data
Use the query API to filter, sort, and paginate your data:
const products = await client.data.query('product', {
filter: { category: 'electronics', price: { $lt: 100 } },
sort: { field: 'created_at', order: 'desc' },
limit: 20,
offset: 0
});Validation
Built-in validation ensures data integrity. Define validation rules directly in your schema, and the API will reject invalid data with descriptive error messages.