|
| 1 | +# Schema Sources |
| 2 | + |
| 3 | +The Singer SDK provides an extensible API for loading schemas from various sources through the `SchemaSource` system. This enables loading schemas from: |
| 4 | + |
| 5 | +- **File directories** - JSON schema files in a directory structure |
| 6 | +- **OpenAPI specifications** - Schema components from OpenAPI 3.1 specs (local or remote) |
| 7 | +- **Custom sources** - Implement your own schema loading logic |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +```python |
| 12 | +from typing import ClassVar |
| 13 | + |
| 14 | +from singer_sdk import RESTStream, SchemaDirectory, StreamSchema |
| 15 | + |
| 16 | +from myproject import schemas |
| 17 | + |
| 18 | +# Create a schema source from a directory. |
| 19 | +# For example, if the package has the following directory structure: |
| 20 | +# myproject |
| 21 | +# └── schemas |
| 22 | +# ├── __init__.py |
| 23 | +# ├── projects.json |
| 24 | +# └── users.json |
| 25 | +# then the following code will load the schemas from the schemas subpackage: |
| 26 | +SCHEMAS_DIR = SchemaDirectory(schemas) |
| 27 | + |
| 28 | +class ProjectsStream(RESTStream): |
| 29 | + name = "projects" |
| 30 | + schema: ClassVar[StreamSchema] = StreamSchema(SCHEMAS_DIR) # Loads from projects.json |
| 31 | +``` |
| 32 | + |
| 33 | +## OpenAPI Integration |
| 34 | + |
| 35 | +```python |
| 36 | +from typing import ClassVar |
| 37 | + |
| 38 | +from singer_sdk import OpenAPISchema, StreamSchema |
| 39 | + |
| 40 | +# Load from OpenAPI spec |
| 41 | +openapi_source = OpenAPISchema("https://api.example.com/openapi.json") |
| 42 | + |
| 43 | +class UsersStream(RESTStream): |
| 44 | + name = "users" |
| 45 | + schema: ClassVar[StreamSchema] = StreamSchema(openapi_source, key="User") # Load "User" component |
| 46 | +``` |
| 47 | + |
| 48 | +## Migrating from File-Path Based Schemas |
| 49 | + |
| 50 | +If you're upgrading from an older version of the Singer SDK that used file paths for schema loading, here's how to migrate your streams to use the new schema sources system. |
| 51 | + |
| 52 | +### Before: Using `schema_filepath` |
| 53 | + |
| 54 | +```python |
| 55 | +from pathlib import Path |
| 56 | +from singer_sdk import RESTStream |
| 57 | + |
| 58 | +SCHEMAS_DIR = Path(__file__).parent / "schemas" |
| 59 | + |
| 60 | +class ProjectsStream(RESTStream): |
| 61 | + """Projects stream with file-path based schema.""" |
| 62 | + |
| 63 | + name = "projects" |
| 64 | + schema_filepath = SCHEMAS_DIR / "projects.json" # Deprecated approach |
| 65 | +``` |
| 66 | + |
| 67 | +### After: Using `StreamSchema` with `SchemaDirectory` |
| 68 | + |
| 69 | +```python |
| 70 | +from typing import ClassVar |
| 71 | +from singer_sdk import RESTStream, SchemaDirectory, StreamSchema |
| 72 | +from myproject import schemas # Your schemas module/package |
| 73 | + |
| 74 | +SCHEMAS_DIR = SchemaDirectory(schemas) |
| 75 | + |
| 76 | +class ProjectsStream(RESTStream): |
| 77 | + """Projects stream with schema source.""" |
| 78 | + |
| 79 | + name = "projects" |
| 80 | + schema: ClassVar[StreamSchema] = StreamSchema(SCHEMAS_DIR) # New approach |
| 81 | +``` |
| 82 | + |
| 83 | +### Migration Steps |
| 84 | + |
| 85 | +1. **Organize your schemas**: Ensure your schema files are in a Python package with an `__init__.py` file: |
| 86 | + |
| 87 | + ``` |
| 88 | + myproject/ |
| 89 | + ├── __init__.py |
| 90 | + ├── streams.py |
| 91 | + └── schemas/ |
| 92 | + ├── __init__.py |
| 93 | + ├── projects.json |
| 94 | + └── users.json |
| 95 | + ``` |
| 96 | + |
| 97 | +1. **Update imports**: Add the new schema source imports: |
| 98 | + |
| 99 | + ```python |
| 100 | + from singer_sdk import SchemaDirectory, StreamSchema |
| 101 | + from myproject import schemas |
| 102 | + ``` |
| 103 | + |
| 104 | +1. **Create schema directory**: Replace file paths with a schema directory: |
| 105 | + |
| 106 | + ```python |
| 107 | + # Before |
| 108 | + SCHEMAS_DIR = Path(__file__).parent / "schemas" |
| 109 | + |
| 110 | + # After |
| 111 | + SCHEMAS_DIR = SchemaDirectory(schemas) |
| 112 | + ``` |
| 113 | + |
| 114 | +1. **Update stream classes**: Replace `schema_filepath` with the `StreamSchema` descriptor: |
| 115 | + |
| 116 | + ```python |
| 117 | + # Before |
| 118 | + class MyStream(RESTStream): |
| 119 | + schema_filepath = SCHEMAS_DIR / "my_stream.json" |
| 120 | + |
| 121 | + # After |
| 122 | + class MyStream(RESTStream): |
| 123 | + schema: ClassVar[StreamSchema] = StreamSchema(SCHEMAS_DIR) |
| 124 | + ``` |
| 125 | + |
| 126 | +1. **Handle custom schema keys** (if needed): |
| 127 | + |
| 128 | + ```python |
| 129 | + # If your stream name doesn't match the schema file name |
| 130 | + class ProjectDetailsStream(RESTStream): |
| 131 | + name = "project_details" |
| 132 | + schema: ClassVar[StreamSchema] = StreamSchema(SCHEMAS_DIR, key="ProjectDetail") # Uses ProjectDetail.json |
| 133 | + ``` |
| 134 | + |
| 135 | +### Benefits of Migration |
| 136 | + |
| 137 | +- **Better performance**: Schema caching reduces file I/O |
| 138 | +- **Type safety**: Improved type hints and validation |
| 139 | +- **Flexibility**: Support for multiple schema sources (files, OpenAPI, custom) |
| 140 | +- **Future-proof**: Access to new schema source features |
0 commit comments