Canon Framework Examples
This directory contains example-driven demonstrations of the Canon framework functionality.
Running Examples
Each example can be run independently using tsx:
bash
# Basic Id Axiom usage
npx tsx examples/01-basic-id-axiom.ts
# Module-style canon definition
npx tsx examples/02-module-style-canon.tsOr run all examples:
bash
npx tsx examples/01-basic-id-axiom.ts && npx tsx examples/02-module-style-canon.tsExamples Overview
01-basic-id-axiom.ts
Demonstrates the fundamental usage of the Canon framework:
- Defining type-level canon configurations
- Registering runtime canon configurations
- Using the
idOf()function across different formats - Interface augmentation pattern
- Universal functions that work with any canon
Key Concepts:
- Declarative canon registration with
declareCanon() - Internal format (standard
idfield) - JSON-LD format (semantic
@idfield) - Universal API working across formats
02-module-style-canon.ts
Demonstrates the module-style pattern for shareable canons:
- Using
defineCanon()to create reusable configurations - Using
registerCanons()for batch registration - Exporting canon definitions for use in other projects
- MongoDB format with
_idfield
Key Concepts:
- Module-style canon definition
- Reusable canon configurations
- Batch registration
- Separation of definition and registration
Testing Strategy
These examples serve as:
- Documentation - Show how to use the framework
- Integration tests - Verify the complete workflow works
- Regression tests - Ensure changes don't break functionality
Unit Tests
Unit tests are colocated with source files using the .test.ts extension:
src/runtime/core.test.ts- Tests for functional coresrc/runtime/registry.test.ts- Tests for global registrysrc/utils/objects.test.ts- Tests for object utilities
Adding New Examples
When adding new examples:
- Use the
0X-descriptive-name.tsnaming convention - Include comprehensive comments explaining concepts
- Demonstrate one key feature or pattern per file
- Include console output showing results
- Update this README with the new example