https://github.com/sayanarijit/sqla-fancy-core
SQLAlchemy core, but fancier
https://github.com/sayanarijit/sqla-fancy-core
Last synced: 7 months ago
JSON representation
SQLAlchemy core, but fancier
- Host: GitHub
- URL: https://github.com/sayanarijit/sqla-fancy-core
- Owner: sayanarijit
- License: mit
- Created: 2023-02-27T06:05:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-29T02:36:41.000Z (almost 2 years ago)
- Last Synced: 2025-03-19T05:55:53.693Z (7 months ago)
- Language: Python
- Homepage: https://pypi.org/project/sqla-fancy-core/
- Size: 38.1 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sqla-fancy-core
SQLAlchemy core, but fancier.
### Basic Usage
```python
import sqlalchemy as sa
from sqla_fancy_core import TableFactorytf = TableFactory()
# Define a table
class Author:id = tf.auto_id()
name = tf.string("name")
created_at = tf.created_at()
updated_at = tf.updated_at()Table = tf("author")
# Define a table
class Book:id = tf.auto_id()
title = tf.string("title")
author_id = tf.foreign_key("author_id", Author.id)
created_at = tf.created_at()
updated_at = tf.updated_at()Table = tf("book")
# Create the tables
engine = sa.create_engine("sqlite:///:memory:")
tf.metadata.create_all(engine)with engine.connect() as conn:
# Insert author
qry = (
sa.insert(Author.Table)
.values({Author.name: "John Doe"})
.returning(Author.id)
)
author = next(conn.execute(qry).mappings())
author_id = author[Author.id]
assert author_id == 1# Insert book
qry = (
sa.insert(Book.Table)
.values({Book.title: "My Book", Book.author_id: author_id})
.returning(Book.id)
)
book = next(conn.execute(qry).mappings())
assert book[Book.id] == 1# Query the data
qry = sa.select(Author.name, Book.title).join(
Book.Table,
Book.author_id == Author.id,
)
result = conn.execute(qry).fetchall()
assert result == [("John Doe", "My Book")], result
```### With Pydantic Validation
```python
from pydantic import BaseModel, Field
from sqla_fancy_core import TableFactorytf = TableFactory()
def field(col, default=...):
return col.info["field"](default)# Define a table
class User:
name = tf.string(
"name", info={"field": lambda default: Field(default, max_length=5)}
)
Table = tf("author")# Define a pydantic schema
class CreateUser(BaseModel):
name: str = field(User.name)# Define a pydantic schema
class UpdateUser(BaseModel):
name: str | None = field(User.name, None)assert CreateUser(name="John").model_dump() == {"name": "John"}
assert UpdateUser(name="John").model_dump() == {"name": "John"}
assert UpdateUser().model_dump(exclude_unset=True) == {}with pytest.raises(ValueError):
CreateUser()
with pytest.raises(ValueError):
UpdateUser(name="John Doe")
```