fasthtml icon indicating copy to clipboard operation
fasthtml copied to clipboard

Where to find out more about the MiniDataApi spec?

Open soumendra opened this issue 1 year ago • 7 comments

  • 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?

soumendra avatar Jul 31 '24 00:07 soumendra

Heh I gotta write it still! Coming soon :D

jph00 avatar Jul 31 '24 09:07 jph00

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?

anddam avatar Aug 05 '24 21:08 anddam

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.

jph00 avatar Aug 05 '24 22:08 jph00

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

  1. 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.

  2. 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.

  3. Dataclass Integration: The spec supports Python's dataclass to provide type safety and structure to the data objects returned from queries.

Key Features

  1. 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')
    
  2. Defining Data Models with Dataclasses:

    Todo, User = todos.dataclass(), users.dataclass()
    
  3. 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
      
  4. Filtering and Security with xtra: The xtra method 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

  1. 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()
    
  2. 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)
    
  3. 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.

pigeonflight avatar Aug 06 '24 13:08 pigeonflight

This is ChatGPT's best guess (I had it read the adv_app.py file)

Oh that's actually pretty good, thx.

anddam avatar Aug 06 '24 19:08 anddam

Can't wait for this to land

soumendra avatar Aug 07 '24 08:08 soumendra

The current draft is here. It needs some work, there's some missing items. I hope to have the first iteration in today!

pydanny avatar Aug 15 '24 21:08 pydanny

The MiniData spec is here! Many thanks to everyone who helped get it out the door. 😄

pydanny avatar Sep 25 '24 08:09 pydanny