Skip to main content
The schema package in Orionjs provides a powerful type-safe way to define data structures in your application. Schemas serve two main purposes:
  1. Define data structures: Schemas describe the structure, types, and validation rules for your data models.
  2. Automatic integration: Schemas automatically integrate with GraphQL for API generation and MongoDB for document validation.

Structure and Naming

  • Use schemaWithName() function from @orion-js/schema
  • Follow naming conventions: {EntityName}Schema for the schema, {EntityName} for the type
  • Keep schemas focused on a single entity or concept
  • Put schemas in app/{component}/schemas/{EntityName}/index.ts
  • Prefer createEnum for enum types
  • Avoid big entities; split them into smaller ones and use composition
  • Use typedId() for MongoDB document IDs
  • Schema validation errors should be camelCase (e.g., priceMustBePositive)

Basic Usage

Nested Schemas

Create nested schemas for complex data structures:

Integration with GraphQL

Schemas defined with schemaWithName are automatically converted to GraphQL types when used in your API:
When the above resolver is registered, Orionjs automatically:
  • Creates a GraphQL type for ProductSchema
  • 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:

Common Schema Types

Property Options

Properties can be configured with the following options:

Next Steps

Explore more about schemas: