fasthtml
fasthtml copied to clipboard
Where to find out more about the MiniDataApi spec?
- I have tried Google, no useful results
- I have tried fastlite repo, no mention
MiniDataApi spec sounds like something that'll be useful. Where can I find more about it?
Heh I gotta write it still! Coming soon :D
Heh I gotta write it still!
But this seems like something already existing
it'll be easier if you pick a lib that supports the MiniDataAPI spec.
Here we are using SQLite, with the FastLite library, which supports the MiniDataAPI spec.`
How are DBs or libraries supporting a spec that hasn't been written yet?
What are comments like
# `xtra` is part of the MiniDataAPI spec.
in adv_app.py or pep8_app.py referencing?
I wrote the lib first based on what's in my head around the API.
adv_app.py is the best file to look at.
This is ChatGPT's best guess (I had it read the adv_app.py file)
Introduction to MiniData API Spec
The MiniData API spec is a lightweight database interaction standard designed to simplify and streamline the management of data within applications. It supports essential CRUD (Create, Read, Update, Delete) operations, schema management, and filtering, ensuring a flexible and efficient way to handle data. Below is a guide on how the MiniData API spec works, illustrated through an example application.
Basic Concepts
-
Database Initialization: The MiniData API spec allows for easy database initialization using libraries like SQLite. You can connect to a database and access its tables using a simple and consistent interface.
-
Table Management: Tables in the MiniData API are managed through a collection attribute (e.g.,
db.t). You can check for the existence of tables and create them if necessary, defining their schema directly in the code. -
Dataclass Integration: The spec supports Python's
dataclassto provide type safety and structure to the data objects returned from queries.
Key Features
-
Creating and Managing Tables:
db = database('data/utodos.db') todos, users = db.t.todos, db.t.users if todos not in db.t: users.create(dict(name=str, pwd=str), pk='name') todos.create(id=int, title=str, done=bool, name=str, details=str, priority=int, pk='id') -
Defining Data Models with Dataclasses:
Todo, User = todos.dataclass(), users.dataclass() -
CRUD Operations:
-
Create: Insert new records into the table.
new_user = users.insert(dict(name='Alice', pwd='password123')) -
Read: Query records from the table using primary keys or filters.
user = users['Alice'] # Fetch by primary key todo_list = todos(name='Alice') # Fetch with filter -
Update: Modify existing records in the table.
todos.update({'done': True}, 1) # Mark todo with id 1 as done -
Delete: Remove records from the table.
todos.delete(1) # Delete todo with id 1
-
-
Filtering and Security with
xtra: Thextramethod ensures that users can only access their own records by adding filters automatically.def before(req, sess): auth = req.scope['auth'] = sess.get('auth', None) if not auth: return RedirectResponse('/login', status_code=303) todos.xtra(name=auth)
Example Application Walkthrough
-
Setup and Initialization:
- Initialize the database and tables.
- Define data models using dataclasses.
from fasthtml.common import * db = database('data/utodos.db') todos, users = db.t.todos, db.t.users if todos not in db.t: users.create(dict(name=str, pwd=str), pk='name') todos.create(id=int, title=str, done=bool, name=str, details=str, priority=int, pk='id') Todo, User = todos.dataclass(), users.dataclass() -
CRUD Operations in Routes:
- Example of a POST request handling user login.
@rt("/login") def post(login:Login, sess): try: u = users[login.name] except NotFoundError: u = users.insert(login) if not compare_digest(u.pwd.encode("utf-8"), login.pwd.encode("utf-8")): return login_redir sess['auth'] = u.name return RedirectResponse('/', status_code=303) -
Filtering with
xtra:- Ensure users only see their own todos.
def before(req, sess): auth = req.scope['auth'] = sess.get('auth', None) if not auth: return RedirectResponse('/login', status_code=303) todos.xtra(name=auth)
Conclusion
The MiniData API spec provides a simple and consistent way to manage database operations in Python applications. It supports essential CRUD functionalities, schema management, and data filtering, making it a powerful tool for developers to build robust and secure applications. By following the principles outlined in this guide, you can effectively utilize the MiniData API spec in your projects.
This is ChatGPT's best guess (I had it read the
adv_app.pyfile)
Oh that's actually pretty good, thx.
Can't wait for this to land
The current draft is here. It needs some work, there's some missing items. I hope to have the first iteration in today!
The MiniData spec is here! Many thanks to everyone who helped get it out the door. 😄