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
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 fastsql-2.0.3.tar.gz.
File metadata
- Download URL: fastsql-2.0.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0fb504335792a763ed7d76bf53d677b51fd6a8591885c2d6381d6bb061e60c2
|
|
| MD5 |
b365f0cf0a41d3d506a57d8c800285ba
|
|
| BLAKE2b-256 |
8080f3eeb0213bfc1db1be5e4c6818230b36dc1b97e5d8b57f3d85449251794e
|
File details
Details for the file fastsql-2.0.3-py3-none-any.whl.
File metadata
- Download URL: fastsql-2.0.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d674ba9cd9f42357ade3aa8ee6a5a54fb4ded714d5fd0a085bdf5c0c48d86958
|
|
| MD5 |
9a95af57879147ef90c8790b447ff75f
|
|
| BLAKE2b-256 |
2ba3ee4f17a7d48e6e4a0407c4dd0bd8f3c660a64b6a8e0b6afc25cd6e887b4f
|