Skip to main content

Simple crud operator on top sqlmodel

Project description

SQL Crud

This package is build on top SQLModel and holds basics crud operations.
The idea with this package is that i got tired of write basics crud operations over and over agin.

Exempel usage

You need to bring your own engine and schemas

from sqlmodel import Field, SQLModel, create_engine
from pydantic import BaseModel
import sqlcrud

#Base user model
class UserBase(SQLModel):
    name: str

#Sql table schema
class User(UserBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    password: str

#Create user schema
class UserCreate(UserBase):
    password: str

#Update user schema
class UserUpdate(BaseModel):
    name: Optional[str]
    password: Optional[str]

# Create a engine instans
engine = create_engine("sqlite:///database.db", echo=True)

Create your crud opetions class and pass in the CrudBase class a parent with your create and update schema definitions

class UserCrud(sqlcrud.BaseCrud[User, UserCreate, UserUpdate]):
    def __init__(self):
        sqlcrud.BaseCrud.__init__(self, model=User, engine=engine)

user_crud = UserCrud()

Available operations

Create

Save a new model in the database

newUser = UserCreate(name="Johan", password="mysupersecurepassword")
user_crud.create(model=newUser)

All

Retrieve all models from database.

users = user_crud.all()

Find

Retrieve a model by its primary key

myuser = user_crud.find(primaryKey=1)

Find by

Retrieve one or many models by a matching value in a column

get_one = user_crud.findby(column="name", value="Johan")
get_many = user_crud.findby(column="name", value="Johan", get_many=True)

Update

Update a model that already exist in the database.

me = user_crud.find(1)
updateUser=UserUpdate(password="mynewevenmoresupresecurepassword")
user_crud.update(model=me, data=updateuser)

Delete

Delete one model from database.

me = user_crud.find(1)
user_crud.delete(model=me)

Delete many models from database.

users = user_crud.all()
user_crud.deleteMany(models=users)

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

sqlcrud-1.0.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlcrud-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlcrud-1.0.0.tar.gz.

File metadata

  • Download URL: sqlcrud-1.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for sqlcrud-1.0.0.tar.gz
Algorithm Hash digest
SHA256 49adbe5c6527d01079e5f9116313e19522b07ecbc03b75e774c610b9221aa22a
MD5 d1c12bcd4d1c3c0332e64f4917d5898f
BLAKE2b-256 c7fd696050eec5729b2bb10b6abd5e698db6ace46598b49437601fbb5ed15492

See more details on using hashes here.

File details

Details for the file sqlcrud-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sqlcrud-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for sqlcrud-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11b85a6ff9362951e82b9773af085328e8f064d7408a109725c5fa29006d5e74
MD5 56aa1c459661649e197b121347362835
BLAKE2b-256 0b088b743d174914c892fbb4e2487903dfa6a174969e5db297d5a745f2e7beab

See more details on using hashes here.

Supported by

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