Skip to main content

Using dataclasses with SQL databases.

Project description

dataclasses-sql

Build Status

Using dataclasses with SQL databases.

Examples:

import dataclasses
import sqlalchemy
import dataclasses_sql

@dataclasses.dataclass
class Car:
    brand: str = dataclasses.field(metadata={"key": True})
    model: str = dataclasses.field(metadata={"key": True})
    milage: float

# Connect to database
engine = sqlalchemy.create_engine("sqlite:///:memory:")
metadata = sqlalchemy.MetaData(engine)
metadata.reflect()

# Insert
car = Car("Kia", "Ceed", 15678)
dataclasses_sql.insert(metadata, car, check_exists=True)

car = Car("Ford", "Mustang", 4032)
dataclasses_sql.insert(metadata, car, check_exists=True)

# Select
builder = dataclasses_sql.SelectStatementBuilder()
builder.add_column(Car, "mileage"
builder.add_clause(Car, "brand", "Kia")
statement = builder.build()

with metadata.bind.begin() as conn:
    row = conn.execute(statement).fetchone()
    print(row)

Installation

Easiest way to install using pip:

pip install dataclasses-sql

For development installation from the git repository::

git clone git@github.com/ppinard/dataclasses-sql.git
cd dataclasses-sql
pip install -e .

Release notes

0.1.0

Contributors

License

The library is provided under the MIT license license.

Copyright (c) 2020, Philippe Pinard

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for dataclasses-sql, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size dataclasses_sql-0.1.1-py3-none-any.whl (7.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size dataclasses-sql-0.1.1.tar.gz (23.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page