Skip to main content

A MiniDataAPI spec implementation for SQLAlchemy V2

Project description

fastsql

Install

pip install fastsql

How to use

from fastsql import *
from dataclasses import dataclass

First we instantiate our database using FastSQL’s Database class:

db = Database("sqlite:///:memory:")

We demonstrate fastsql’s features here using dataclasses-as-schema inspired by FastHTML’s adv_app example.

@dataclass
class User:
    name:str
    pwd:str

@dataclass
class Todo:
    title:str
    name:str
    done:bool=False
    details:str=''
    id:int=None

Equipped with our schemas, let’s turn them into database tables.

users = db.create(User, pk='name')
todos = db.create(Todo, pk='id')

Let’s confirm the table design.

db.print_schema()
Table: Todo
  - *id: INTEGER
  - title: VARCHAR
  - name: VARCHAR
  - done: BOOLEAN
  - details: VARCHAR
Table: User
  - *name: VARCHAR
  - pwd: VARCHAR

Does a table exist?

users.exists()
True

Manipulating data

Creating, reading, updating, and deleting records in database tables.

Let’s create some dataclass objects representing users and todos.

u0 = User('jph','foo')
u1 = User('rlt','bar')
t0 = Todo('do it', 'jph')
t1 = Todo('build it', 'jph')
t2 = Todo('write book', 'rlt')

Let’s convert these dataclass objects into database records. To do that we insert them into their tables using the aply named insert method:

users.insert(u0)
users.insert(u1)
todos.insert(t0)
todos.insert(t1)
todos.insert(t2)
Todo(title='write book', name='rlt', done=False, details='', id=3)

Display all the user records.

for user in users():
    print(user)
User(name='jph', pwd='foo')
User(name='rlt', pwd='bar')

Use where statement to filter records, in this case only jph’s todos.

for todo in todos(where="name = :name", name="jph"):
    print(todo)
Todo(title='do it', name='jph', done=False, details='', id=1)
Todo(title='build it', name='jph', done=False, details='', id=2)

Look only for those records with the word it in it.

for todo in todos(where="title LIKE :title", title="%% it%%"):
    print(todo)
Todo(title='do it', name='jph', done=False, details='', id=1)
Todo(title='build it', name='jph', done=False, details='', id=2)

Fetch a record just by the primary key.

user = users['rlt']
user
User(name='rlt', pwd='bar')

Change a value in a record.

user.pwd = 'baz'
users.update(user)
users['rlt']
User(name='rlt', pwd='baz')

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

fastsql-2.0.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

fastsql-2.0.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file fastsql-2.0.1.tar.gz.

File metadata

  • Download URL: fastsql-2.0.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for fastsql-2.0.1.tar.gz
Algorithm Hash digest
SHA256 05762ce8e342f38a871a54477d8f7912723bf313548a7ecc4a0e725c7f8675fb
MD5 d7a53747712db58d63e41e5fd216074e
BLAKE2b-256 da6ce2d9ed9f7f441f59cdd6db76a7e72ac379c09cc14867199ebb5639439112

See more details on using hashes here.

File details

Details for the file fastsql-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: fastsql-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for fastsql-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b07ba6ca616a4b3423450d49e763b3d2cfac4662cb385b2e3cd18d652f145cc2
MD5 2168e40daaffd6d468e095adef0ac87a
BLAKE2b-256 0e7681ed2127c51d4477236c65f803224ede74aa4bf6e65345737d184be40cae

See more details on using hashes here.

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