sqlalchemy-pydantic-mapper library for mapping SQLAlchemy models to Pydantic
Project description
sqlalchemy-pydantic-mapper
sqlalchemy-pydantic-mapper simplifies converting SQLAlchemy instances (subclasses of sqlalchemy.orm.DeclarativeBase) into Pydantic models (pydantic.BaseModel).
It supports:
- registering custom synchronous and asynchronous mappers;
- registration via decorator or by passing
func=directly; - automatic mapping via Pydantic if the model has
model_config = ConfigDict(from_attributes=True); map(...)— an async method returning the target Pydantic model instance (mustawaitit).
Usage Examples (Full Code Snippets)
- Simple registration via
func=and checking_mappers:
from sqlalchemy_pydantic_mapper import ObjectMapper
def mapper(db: UserDB) -> UserSchema:
return UserSchema(id=db.id, name=db.name)
ObjectMapper.register(UserDB, UserSchema, func=mapper)
assert ObjectMapper._mappers[UserDB][UserSchema] is mapper
- Registration via decorator:
@ObjectMapper.register(UserDB, UserSchema)
def mapper2(db: UserDB) -> UserSchema:
return UserSchema(id=db.id, name=db.name)
- Async mapper (registration + usage):
@ObjectMapper.register(UserDB, UserSchema)
async def async_mapper(db: UserDB) -> UserSchema:
# e.g., async call or await something
return UserSchema(id=db.id, name=db.name.upper())
import asyncio
async def main():
user = UserDB()
user.id = 1
user.name = "alice"
res = await ObjectMapper.map(user, UserSchema)
print(res) # UserSchema(id=1, name='ALICE')
asyncio.run(main())
- Auto-mapping via Pydantic (
from_attributes=True):
class UserSchema(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
name: str
If no custom mapper is registered for the from_class -> to_class pair, ObjectMapper.map(instance, UserSchema) automatically calls:
UserSchema.model_validate(instance, from_attributes=True)
- Example in a test (synthetic):
async def test_auto_mapping():
stud = StudentDB()
stud.id = 2
stud.name = "Bob"
result = await ObjectMapper.map(stud, UserSchema)
assert result.id == 2
assert result.name == "Bob"
- Example for manual mapping (different names / logic):
def stud_to_studschema(db: StudentDB) -> StudSchema:
return StudSchema(id=db.id, name=db.name)
ObjectMapper.register(StudentDB, StudSchema, func=stud_to_studschema)
Errors and Behavior on Incorrect Usage
TypeErroriffrom_does not inheritDeclarativeBase:
class NotABase: pass
ObjectMapper.register(NotABase, UserSchema) # -> TypeError
TypeErrorifto_does not inheritBaseModel:
class NotABaseModel: pass
ObjectMapper.register(UserDB, NotABaseModel) # -> TypeError
ValueErroriffuncis missing andto_does not havemodel_config = ConfigDict(from_attributes=True):
class BadSchema(BaseModel):
id: int
name: str
ObjectMapper.register(UserDB, BadSchema) # -> ValueError
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_pydantic_mapper-0.1.0.tar.gz.
File metadata
- Download URL: sqlalchemy_pydantic_mapper-0.1.0.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d32941931b55c2b3b6510ba750f813e6fdcad9bc691a63f9d1ca2ede79ecacff
|
|
| MD5 |
5eced9d7169f0e3abb6e7b82b6fbac26
|
|
| BLAKE2b-256 |
01700c89b48a6800fc78f51afca50ca973a23bb752ffa25963212f731bad0d5b
|
Provenance
The following attestation bundles were made for sqlalchemy_pydantic_mapper-0.1.0.tar.gz:
Publisher:
publish.yml on ItzSkyReed/sqlalchemy-pydantic-mapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlalchemy_pydantic_mapper-0.1.0.tar.gz -
Subject digest:
d32941931b55c2b3b6510ba750f813e6fdcad9bc691a63f9d1ca2ede79ecacff - Sigstore transparency entry: 446271716
- Sigstore integration time:
-
Permalink:
ItzSkyReed/sqlalchemy-pydantic-mapper@2e6143bbf1b71f1b3581228b6567e0e24d1278fc -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/ItzSkyReed
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2e6143bbf1b71f1b3581228b6567e0e24d1278fc -
Trigger Event:
release
-
Statement type:
File details
Details for the file sqlalchemy_pydantic_mapper-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_pydantic_mapper-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d374a7e1e824c3230a248d033ccca996952e3eb25b959ee93e70336c72bd944
|
|
| MD5 |
31f64d499179f4706e460dc056125478
|
|
| BLAKE2b-256 |
a74bac6d768121309101f410cb45894539f5a86e63cdc6ada76a6bb8b407c421
|
Provenance
The following attestation bundles were made for sqlalchemy_pydantic_mapper-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on ItzSkyReed/sqlalchemy-pydantic-mapper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlalchemy_pydantic_mapper-0.1.0-py3-none-any.whl -
Subject digest:
3d374a7e1e824c3230a248d033ccca996952e3eb25b959ee93e70336c72bd944 - Sigstore transparency entry: 446271760
- Sigstore integration time:
-
Permalink:
ItzSkyReed/sqlalchemy-pydantic-mapper@2e6143bbf1b71f1b3581228b6567e0e24d1278fc -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/ItzSkyReed
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2e6143bbf1b71f1b3581228b6567e0e24d1278fc -
Trigger Event:
release
-
Statement type: