Local_tables - An IndexedDB wrapper for storing anvil rows locally

local_tables works as a wrapper on top of IndexedDB, allowing you to store and retrieve rows through an API similar to Anvil tables.

It lets you store Anvil rows exactly as they are, with full support for media, datetime objects, and linked rows. Useful for apps that work offline or need to cache data locally.

What you can do:

  1. Store and access your locally stored data using an API similar to Anvil tables
  2. Convert Anvil row objects into a format you can store and retrieve from the IndexedDB (the library handles the serialisation and deserialisation).
  3. Use most of the anvil tables functionalities, including q.* queries, ordering, pagination and transactions.

How to install

Step 1: Clone the anvil app and add it as a dependency

Step 2: Pin the version to v0.1 (Serves a minified build)

Quickstart

from local_tables import tables as lt
from local_tables.tables import local_tables, queries as q

# Define the database schema
lt.Database().setup( 
    version=1,
    schema = {
        "employees": {
            "indexed": ['name', 'age', 'company'],
            "links": {"company": "companies"}
        },
        "companies": {}
    }
    
)

# Store an anvil row
company = anvil.server.call('get_company')
local_tables.companies.add_row(self.company, put = True)

# Create a row locally
row = local_tables.employees.add_row(
            name = self.name_input.text,
            age = int(self.age_input.text),
            avatar = self.avatar_picker.file,
            
            created_at = datetime.now(anvil.tz.tzlocal())
        )

row['company'] = company # Update a row
print(row['company']['name']) # Access linked rows 
print(local_tables.employees.search(age = q.between(5, 35))[0]['age']) # Search rows

For detailed docs, check out the README on the Github repo - GitHub - Divyesh06/local-tables-anvil: local_tables - Built with Anvil · GitHub

Example Apps

  • local_tables_demo - A minimal working example - Anvil | Login

  • Expenses Tracker - a fully fledged offline-first app with two-way sync Anvil | Login

Note

This library is fresh out of the oven and hasn’t been tested extensively in the wild yet. While I’ve added significant test coverage, please make sure to test it thoroughly on your end as well. You can report any findings here or on the GitHub repo.

Additionally, I will probably be working on a proper documentation site as well to dive deeper into the usage, examples and internal workings.

5 Likes