|
| 1 | +===== |
| 2 | +Types |
| 3 | +===== |
| 4 | + |
| 5 | +Advanced Alchemy provides several custom SQLAlchemy types to enhance your database interactions. |
| 6 | + |
| 7 | +DateTimeUTC |
| 8 | +----------- |
| 9 | + |
| 10 | +A timezone-aware DateTime type that ensures UTC timezone handling in the database. |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | +
|
| 14 | + from advanced_alchemy.types import DateTimeUTC |
| 15 | +
|
| 16 | + class MyModel: |
| 17 | + created_at = Column(DateTimeUTC) |
| 18 | +
|
| 19 | +The ``DateTimeUTC`` type: |
| 20 | + |
| 21 | +- Ensures all datetime values are stored in UTC |
| 22 | +- Requires timezone information for input values |
| 23 | +- Automatically converts stored values to UTC timezone |
| 24 | +- Returns timezone-aware datetime objects |
| 25 | + |
| 26 | +Encrypted Types |
| 27 | +--------------- |
| 28 | + |
| 29 | +Two types for storing encrypted data with support for multiple encryption backends: |
| 30 | + |
| 31 | +EncryptedString |
| 32 | +~~~~~~~~~~~~~~~ |
| 33 | + |
| 34 | +For storing encrypted string values with configurable length. |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + from advanced_alchemy.types import EncryptedString |
| 39 | +
|
| 40 | + class MyModel: |
| 41 | + secret = Column(EncryptedString(key="my-secret-key")) |
| 42 | +
|
| 43 | +EncryptedText |
| 44 | +~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +For storing larger encrypted text content (CLOB). |
| 47 | + |
| 48 | +.. code-block:: python |
| 49 | +
|
| 50 | + from advanced_alchemy.types import EncryptedText |
| 51 | +
|
| 52 | + class MyModel: |
| 53 | + large_secret = Column(EncryptedText(key="my-secret-key")) |
| 54 | +
|
| 55 | +Encryption Backends |
| 56 | +~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +Two encryption backends are available: |
| 59 | + |
| 60 | +- ``FernetBackend``: Uses Python's cryptography library with Fernet encryption |
| 61 | +- ``PGCryptoBackend``: Uses PostgreSQL's pgcrypto extension (PostgreSQL only) |
| 62 | + |
| 63 | +GUID |
| 64 | +---- |
| 65 | + |
| 66 | +A platform-independent GUID/UUID type that adapts to different database backends: |
| 67 | + |
| 68 | +- PostgreSQL/DuckDB/CockroachDB: Uses native UUID type |
| 69 | +- MSSQL: Uses UNIQUEIDENTIFIER |
| 70 | +- Oracle: Uses RAW(16) |
| 71 | +- Others: Uses BINARY(16) or CHAR(32) |
| 72 | + |
| 73 | +.. code-block:: python |
| 74 | +
|
| 75 | + from advanced_alchemy.types import GUID |
| 76 | +
|
| 77 | + class MyModel: |
| 78 | + id = Column(GUID, primary_key=True) |
| 79 | +
|
| 80 | +BigIntIdentity |
| 81 | +-------------- |
| 82 | + |
| 83 | +A BigInteger type that automatically falls back to Integer for SQLite: |
| 84 | + |
| 85 | +.. code-block:: python |
| 86 | +
|
| 87 | + from advanced_alchemy.types import BigIntIdentity |
| 88 | +
|
| 89 | + class MyModel: |
| 90 | + id = Column(BigIntIdentity, primary_key=True) |
| 91 | +
|
| 92 | +JsonB |
| 93 | +----- |
| 94 | + |
| 95 | +A JSON type that uses the most efficient JSON storage for each database: |
| 96 | + |
| 97 | +- PostgreSQL/CockroachDB: Uses native JSONB |
| 98 | +- Oracle: Uses Binary JSON (BLOB with JSON constraint) |
| 99 | +- Others: Uses standard JSON type |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + from advanced_alchemy.types import JsonB |
| 104 | +
|
| 105 | + class MyModel: |
| 106 | + data = Column(JsonB) |
| 107 | +
|
| 108 | +Type Features |
| 109 | +------------- |
| 110 | + |
| 111 | +All types include: |
| 112 | + |
| 113 | +- Proper Python type annotations for modern IDE support |
| 114 | +- Automatic dialect-specific implementations |
| 115 | +- Consistent behavior across different database backends |
| 116 | +- Integration with SQLAlchemy's type system |
| 117 | + |
| 118 | +Usage Example |
| 119 | +------------- |
| 120 | + |
| 121 | +Here's a complete example using multiple types: |
| 122 | + |
| 123 | +.. code-block:: python |
| 124 | +
|
| 125 | + from sqlalchemy import Column |
| 126 | + from advanced_alchemy.types import ( |
| 127 | + DateTimeUTC, |
| 128 | + EncryptedString, |
| 129 | + GUID, |
| 130 | + JsonB, |
| 131 | + ) |
| 132 | +
|
| 133 | + class User: |
| 134 | + id = Column(GUID, primary_key=True) |
| 135 | + created_at = Column(DateTimeUTC) |
| 136 | + password = Column(EncryptedString(key="secret-key")) |
| 137 | + preferences = Column(JsonB) |
| 138 | +
|
| 139 | +Using Types with Alembic |
| 140 | +------------------------ |
| 141 | + |
| 142 | +If you are not using Advanced Alchemy's built-in `alembic` templates, you need to properly configure your ``script.py.mako`` template. The key is to make the custom types available through the ``sa`` namespace that Alembic uses. |
| 143 | + |
| 144 | +Type Aliasing |
| 145 | +~~~~~~~~~~~~~ |
| 146 | + |
| 147 | +In your ``script.py.mako``, you'll need both the imports and the type aliasing: |
| 148 | + |
| 149 | +.. code-block:: python |
| 150 | +
|
| 151 | + # Import the types |
| 152 | + from advanced_alchemy.types import ( |
| 153 | + EncryptedString, |
| 154 | + EncryptedText, |
| 155 | + GUID, |
| 156 | + ORA_JSONB, |
| 157 | + DateTimeUTC |
| 158 | + ) |
| 159 | +
|
| 160 | + # Create aliases in the sa namespace |
| 161 | + sa.GUID = GUID |
| 162 | + sa.DateTimeUTC = DateTimeUTC |
| 163 | + sa.ORA_JSONB = ORA_JSONB |
| 164 | + sa.EncryptedString = EncryptedString |
| 165 | + sa.EncryptedText = EncryptedText |
| 166 | +
|
| 167 | +These assignments are necessary because: |
| 168 | + |
| 169 | +1. Alembic uses the ``sa`` namespace when generating migrations |
| 170 | +2. Custom types need to be accessible through this namespace |
| 171 | +3. Without these aliases, Alembic might not properly detect or reference the custom types |
| 172 | + |
| 173 | +Example Usage |
| 174 | +~~~~~~~~~~~~~ |
| 175 | + |
| 176 | +This setup allows you to use the types in migrations like this: |
| 177 | + |
| 178 | +.. code-block:: python |
| 179 | +
|
| 180 | + # In generated migration file |
| 181 | + def upgrade(): |
| 182 | + op.create_table( |
| 183 | + 'users', |
| 184 | + sa.Column('id', sa.GUID(), primary_key=True), |
| 185 | + sa.Column('created_at', sa.DateTimeUTC(), nullable=False), |
| 186 | + sa.Column('secret', sa.EncryptedString(), nullable=True), |
| 187 | + ) |
0 commit comments