- Define data structures: Schemas describe the structure, types, and validation rules for your data models.
- Automatic integration: Schemas automatically integrate with GraphQL for API generation and MongoDB for document validation.
Schema Definition
The modern approach to define schemas in Orionjs uses theschemaWithName
function to create type-safe data structures.
Basic Usage
schemaWithName()
function creates an Orionjs schema with a name, which is required for GraphQL integration. Each property is defined with its type and options.
Integration with GraphQL
Schemas defined withschemaWithName
are automatically converted to GraphQL types when used in your API, saving you from writing redundant type definitions.
- Creates a GraphQL type for
UserSchema
- Sets up the appropriate field types based on your schema definition
- Handles type conversions between your application and GraphQL
Integration with MongoDB
When using schemas with MongoDB collections, Orionjs automatically validates documents before insertion or updates:- All required fields are present
- Types match those defined in the schema
- Any custom validation rules are satisfied
Next Steps
Explore more about schemas:- Schema Types - Learn about all available data types for schema fields
- Property Options - Learn about all available options for schema properties
- Schema Validation - Understand how validation works and how to create custom validators
- Advanced Schema Usage - Discover nested schemas, arrays, and schema inheritance
- Schema Serialization - Learn how schemas are serialized for client-side validation
- GraphQL Integration - Understand how schemas integrate with GraphQL