Examples
Welcome to the QuickModel examples. These practical examples demonstrate real-world usage patterns and best practices for all framework features.
Fundamentals
Basic Usage
Learn the fundamentals with simple, straightforward examples:
- Creating your first model with
@Quick - Property transformations (Date, BigInt, Set, Map)
- Serialization with
toJSON() - Working with arrays of transformed types
API Models
Integrate QuickModel with REST APIs:
- Fetching and transforming API responses
- Sending data with
toJSON() - Handling pagination and nesting
- Service patterns (full CRUD)
Complex Types
Advanced type transformations:
- Nested models and deep nesting
- Collections (Set, Map)
- BigInt for large numbers
- Polymorphic models
Decorators & Features
Validation with @QRule
Define business rules and validate data:
@QRulefor property-level rulescheckRules(),isValid(),validationReport()- Async validation with
checkRulesAsync() createMany()separating valid from invalid- Dynamic messages for i18n
Forms with @QField and @QGroup
Dynamically generate form schemas:
@QFieldfor widget metadata (input, select, datepicker...)@QGroupto organize fields into sectionsgetFormSchema()andgetFormSchemaGrouped()- React integration (dynamic form)
- Custom metadata for your framework
Alias Mapping with @QAlias
Bridge snake_case and camelCase:
@QAliasto rename properties- Full roundtrip (input and output)
- Nested models with aliases
- Combining with
@QFieldand@QRule
Computed Fields with @QComputed
Derived properties included in serialization:
@QComputedfor serializable getters- Price, VAT, discount calculations
- Age, status, formatted labels
- Difference between decorated and plain getters
Creation & Testing
Mocks & Testing
Generate test data with the mock() API:
mock().random(),mock().empty(),mock().sample()mock().array(n)with per-index overridesmock().interfaceRandom()for API fixtures- Unit tests with Vitest / Jest
- Storybook: generating example props
Batch Creation & Immutability
Process arrays and create immutable instances:
createMany()for bulk imports- Separating valid from invalid with
errors[] createReadonly()for config and constants- Immutable fixtures for tests
Quick Reference
| I want to... | I use... |
|---|---|
| Transform dates/BigInt | @Quick({ field: Date }) |
| Validate business data | @QRule({ predicate, message }) |
| Map snake_case → camelCase | @QAlias('field_name') |
| Generate a form schema | @QField({ widget, label }) + getFormSchema() |
| Group form fields into sections | @QGroup('Section') + getFormSchemaGrouped() |
| Include getter in serialization | @QComputed() |
| Generate test data | Model.mock().random() |
| Bulk process with validation | Model.createMany(array) |
| Create immutable instance | Model.createReadonly(data) |
Running Examples
All examples are TypeScript and can be run with:
bash
# Using Bun
bun run example.ts
# Using ts-node
npx ts-node example.ts