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
File details
Details for the file fastsql-2.0.0.tar.gz
.
File metadata
- Download URL: fastsql-2.0.0.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 | 5ecd80f28c9cecb320db2f8e7ef8aa3240f31f74dd39164a92b314788dc3a96d |
|
MD5 | 6d2ec318511202f5ec04ef702a0a0f7a |
|
BLAKE2b-256 | 35c5720877124d5cacd49fba5c51c2db6690301a65d2b8a15d8582e7a0860ec4 |
File details
Details for the file fastsql-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: fastsql-2.0.0-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 | 81c7aa61b6b53c280948adf33e53d65c0b09d3bbd7b8459fbf36289da6100fd5 |
|
MD5 | 38111c06e2e8418e7793db5cae3bdbc4 |
|
BLAKE2b-256 | 13b14b3af53db4bf4c82e4a8ca9128481956d51c992cfd063778533a14c6b0c6 |