Skip to content

Commit beb184f

Browse files
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
1 parent b5092e2 commit beb184f

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

docs/advanced/encrypted-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ In this example, the `secret_name` field will be automatically encrypted when yo
1616
For this to work, you need to have `sqlalchemy-utils` and `cryptography` installed.
1717
///
1818

19-
For a more detailed walkthrough, including how to create and query data with encrypted fields, check out the [Encrypting Data Tutorial](../tutorial/encrypted-type.md).
19+
For a more detailed walkthrough, including how to create and query data with encrypted fields, check out the [Encrypting Data Tutorial](../tutorial/encrypted-type.md).

docs/tutorial/encrypted-type.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ Name: Jamie Tartt, Decrypted Secret Name: Baby Shark
6565
Name: Dani Rojas, Decrypted Secret Name: Fútbol is Life
6666
```
6767

68-
As you can see, `EncryptedType` handles the encryption and decryption for you automatically, making it easy to store sensitive data securely.
68+
As you can see, `EncryptedType` handles the encryption and decryption for you automatically, making it easy to store sensitive data securely.

docs_src/advanced/encrypted_type/tutorial001.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
2-
import os
31
from typing import Optional
42

53
import sqlalchemy
64
from sqlalchemy import Column
75
from sqlalchemy_utils import EncryptedType
86
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
9-
from sqlmodel import create_engine, Field, Session, SQLModel
10-
7+
from sqlmodel import Field, SQLModel, create_engine
118

129
# For a real application, use a securely managed key
1310
ENCRYPTION_KEY = "a-super-secret-key"
1411

12+
1513
class Hero(SQLModel, table=True):
1614
id: Optional[int] = Field(default=None, primary_key=True)
1715
name: str
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
sqlmodel
22
sqlalchemy-utils
3-
cryptography
3+
cryptography

docs_src/tutorial/encrypted_type/tutorial001.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Import necessary modules
2-
import os
32
from typing import Optional
43

54
import sqlalchemy
65
from sqlalchemy import Column, text
76
from sqlalchemy_utils import EncryptedType
87
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
9-
from sqlmodel import create_engine, Field, Session, SQLModel, select
8+
from sqlmodel import Field, Session, SQLModel, create_engine, select
109

1110
# Define a secret key for encryption.
1211
# In a real application, this key should be stored securely and not hardcoded.
1312
# For example, you could load it from an environment variable or a secret management service.
1413
ENCRYPTION_KEY = "a-super-secret-key"
1514

15+
1616
# Define the Character model
1717
# This model represents a table named 'character' in the database.
1818
class Character(SQLModel, table=True):
@@ -32,18 +32,21 @@ class Character(SQLModel, table=True):
3232
)
3333
age: Optional[int] = None
3434

35+
3536
# Define the database URL and create the engine
3637
# We are using a SQLite database for this example.
3738
sqlite_file_name = "database.db"
3839
sqlite_url = f"sqlite:///{sqlite_file_name}"
3940
engine = create_engine(sqlite_url)
4041

42+
4143
# This function creates the database and the Character table.
4244
# It first drops the existing table to ensure a clean state for the example.
4345
def create_db_and_tables():
4446
SQLModel.metadata.drop_all(engine)
4547
SQLModel.metadata.create_all(engine)
4648

49+
4750
# This function creates some sample characters and adds them to the database.
4851
def create_characters():
4952
# Create instances of the Character model
@@ -61,6 +64,7 @@ def create_characters():
6164
# Commit the changes to the database
6265
session.commit()
6366

67+
6468
# This function demonstrates how the encryption works.
6569
def demonstrate_encryption():
6670
with Session(engine) as session:
@@ -80,7 +84,10 @@ def demonstrate_encryption():
8084
print("\nData as accessed through SQLModel:")
8185
for character in characters:
8286
# The secret_name will be the original, decrypted string.
83-
print(f"Name: {character.name}, Decrypted Secret Name: {character.secret_name}")
87+
print(
88+
f"Name: {character.name}, Decrypted Secret Name: {character.secret_name}"
89+
)
90+
8491

8592
# The main function that runs the example.
8693
def main():
@@ -91,6 +98,7 @@ def main():
9198
print("\nDemonstrating encryption...")
9299
demonstrate_encryption()
93100

101+
94102
# Run the main function when the script is executed.
95103
if __name__ == "__main__":
96-
main()
104+
main()

0 commit comments

Comments
 (0)