Skip to main content

A very simple CRUD class for SQLModel! :sparkles:

Project description

basesqlmodel

Latest Commit
Package version

A very simple CRUD class for SQLModel! :sparkles:

Installation

pip install basesqlmodel

Usage

import asyncio

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlmodel import Field

from basesqlmodel import Base

engine = create_async_engine("sqlite+aiosqlite:///:memory:")
SessionLocal = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)


class Potato(Base, table=True):
    id: int = Field(primary_key=True)
    name: str


async def main():
    # Create tables
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

    # Interact with the Potato table
    async with SessionLocal() as session:
        obj = await Potato.create(session, name="Potato")
        print(f"Potato created: {repr(obj)}")

        obj = await Potato.get(session, name="Potato")
        print(f"Potato retrieved: {repr(obj)}")

        await obj.update(session, name="Fake Potato")
        print(f"Potato updated: {repr(obj)}")

        await Potato.delete(session, name="Fake Potato")
        print(f"Potato deleted: {repr(obj)}")

        objs = await Potato.get_multi(session)
        print(f"Confirm that the database is empty: {objs}")


asyncio.run(main())

License

This project is licensed under the terms of the MIT license.

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

basesqlmodel-0.1.0.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

basesqlmodel-0.1.0-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page