Skip to main content

Ephemeral SQL index over a local directory

Project description

dirsql (Python SDK)

Ephemeral SQL index over a local directory. Watches a filesystem, ingests structured files into an in-memory SQLite database, and exposes a SQL query interface. The database is purely in-memory -- the filesystem is always the source of truth.

Documentation

Installation

pip install dirsql

Requires Python >= 3.12. Ships as a native extension (Rust via PyO3) -- binary wheels are provided for common platforms.

Quick Start

import asyncio
import json
import os
import tempfile
from dirsql import DirSQL, Table

async def main():
    # Create some data files
    root = tempfile.mkdtemp()
    os.makedirs(os.path.join(root, "comments", "abc"), exist_ok=True)
    os.makedirs(os.path.join(root, "comments", "def"), exist_ok=True)

    with open(os.path.join(root, "comments", "abc", "index.jsonl"), "w") as f:
        f.write(json.dumps({"body": "looks good", "author": "alice"}) + "\n")
        f.write(json.dumps({"body": "needs work", "author": "bob"}) + "\n")

    with open(os.path.join(root, "comments", "def", "index.jsonl"), "w") as f:
        f.write(json.dumps({"body": "agreed", "author": "carol"}) + "\n")

    # Define a table: DDL, glob pattern, and an extract function
    db = DirSQL(
        root,
        tables=[
            Table(
                ddl="CREATE TABLE comments (id TEXT, body TEXT, author TEXT)",
                glob="comments/**/index.jsonl",
                extract=lambda path, content: [
                    {
                        "id": os.path.basename(os.path.dirname(path)),
                        "body": row["body"],
                        "author": row["author"],
                    }
                    for line in content.splitlines()
                    for row in [json.loads(line)]
                ],
            ),
        ],
    )
    await db.ready()

    # Query with SQL
    results = await db.query("SELECT * FROM comments WHERE author = 'alice'")
    # [{"id": "abc", "body": "looks good", "author": "alice"}]

asyncio.run(main())

Multiple Tables and Joins

db = DirSQL(
    root,
    tables=[
        Table(
            ddl="CREATE TABLE posts (title TEXT, author_id TEXT)",
            glob="posts/*.json",
            extract=lambda path, content: [json.loads(content)],
        ),
        Table(
            ddl="CREATE TABLE authors (id TEXT, name TEXT)",
            glob="authors/*.json",
            extract=lambda path, content: [json.loads(content)],
        ),
    ],
)
await db.ready()

results = await db.query("""
    SELECT posts.title, authors.name
    FROM posts JOIN authors ON posts.author_id = authors.id
""")

Ignoring Files

Pass ignore patterns to skip files during scanning and watching:

db = DirSQL(
    root,
    ignore=["**/drafts/**", "**/.git/**"],
    tables=[...],
)

Watching for Changes

DirSQL is async by default. The watch() method returns an async iterator of row-level change events.

import asyncio
import json
from dirsql import DirSQL, Table

async def main():
    db = DirSQL(
        "/path/to/data",
        tables=[
            Table(
                ddl="CREATE TABLE items (name TEXT)",
                glob="**/*.json",
                extract=lambda path, content: [json.loads(content)],
            ),
        ],
    )
    await db.ready()

    # Query
    results = await db.query("SELECT * FROM items")

    # Watch for file changes (insert/update/delete/error events)
    async for event in db.watch():
        print(f"{event.action} on {event.table}: {event.row}")
        if event.action == "error":
            print(f"  error: {event.error}")

asyncio.run(main())

API Reference

Table(*, ddl, glob, extract)

Defines how files map to a SQL table.

  • ddl (str): A CREATE TABLE statement defining the schema.
  • glob (str): A glob pattern matched against file paths relative to root.
  • extract (Callable[[str, str], list[dict]]): A function receiving (relative_path, file_content) and returning a list of row dicts. Each dict's keys must match the DDL column names.

DirSQL(root, *, tables, ignore=None)

Creates an in-memory SQLite database indexed from the directory at root. The constructor is sync and returns immediately; scanning runs in a background thread.

  • root (str): Path to the directory to index.
  • tables (list[Table]): Table definitions.
  • ignore (list[str] | None): Glob patterns for paths to skip.

await DirSQL.ready()

Wait for the initial scan to complete. Idempotent -- safe to call multiple times. Raises any exception that occurred during init.

await DirSQL.query(sql) -> list[dict]

Execute a SQL query. Returns a list of dicts keyed by column name. Internal tracking columns (_dirsql_*) are excluded from results.

DirSQL.watch() -> AsyncIterator[RowEvent]

Returns an async iterator that yields RowEvent objects as files change on disk. Starts the filesystem watcher on first iteration.

DirSQL.from_config(path) -> DirSQL

Create a DirSQL instance from a .dirsql.toml config file. Returns immediately; scanning runs in the background. Call await db.ready() before querying.

RowEvent

Emitted by watch() when a file change produces row-level diffs.

  • table (str): The affected table name.
  • action (str): One of "insert", "update", "delete", "error".
  • row (dict | None): The new row (for insert/update) or deleted row (for delete).
  • old_row (dict | None): The previous row (for update only).
  • error (str | None): Error message (for error events).
  • file_path (str | None): The relative file path that triggered the event.

How It Works

The Rust core (rusqlite + notify + walkdir) does the heavy lifting:

  1. Startup scan: Walks the directory tree, matches files to tables via glob patterns, calls the user-provided extract function for each file, and inserts rows into an in-memory SQLite database.
  2. File watching: Uses the notify crate (inotify on Linux, FSEvents on macOS) to detect file creates, modifications, and deletions.
  3. Row diffing: When a file changes, the new rows are diffed against the previous rows for that file, producing granular insert/update/delete events.
  4. Python bindings: PyO3 exposes the Rust core as a native Python extension module. The async layer runs blocking operations in a thread pool via asyncio.to_thread.

License

MIT

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

dirsql-0.1.10.tar.gz (77.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dirsql-0.1.10-cp313-cp313-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.13Windows x86-64

dirsql-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

dirsql-0.1.10-cp313-cp313-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

dirsql-0.1.10-cp313-cp313-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

dirsql-0.1.10-cp312-cp312-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.12Windows x86-64

dirsql-0.1.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

dirsql-0.1.10-cp312-cp312-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

dirsql-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

dirsql-0.1.10-cp311-cp311-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.11Windows x86-64

dirsql-0.1.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

dirsql-0.1.10-cp311-cp311-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

dirsql-0.1.10-cp311-cp311-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

dirsql-0.1.10-cp310-cp310-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.10Windows x86-64

dirsql-0.1.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

dirsql-0.1.10-cp310-cp310-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

dirsql-0.1.10-cp310-cp310-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10macOS 10.12+ x86-64

dirsql-0.1.10-cp39-cp39-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.9Windows x86-64

dirsql-0.1.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

dirsql-0.1.10-cp39-cp39-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.10-cp39-cp39-macosx_10_12_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9macOS 10.12+ x86-64

File details

Details for the file dirsql-0.1.10.tar.gz.

File metadata

  • Download URL: dirsql-0.1.10.tar.gz
  • Upload date:
  • Size: 77.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10.tar.gz
Algorithm Hash digest
SHA256 3bde4bc0c34d120af6c43b1c8d5ecdaaa90cbc7c3ce2caefb53b8bb02d51b87f
MD5 3aed0c174f5ee2d4c3d3fc8c53b00c79
BLAKE2b-256 aea51ef8cb6ed971238ae70e48f860c9f6517e35441e5809cb6f24098aed7077

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.1.10-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 4abb1d88821c3fc0df23944d69ae7000f610e02a6908fedd9a55f45992134c08
MD5 16c944bd191ea3d07a5b2c2152ddc926
BLAKE2b-256 3fa07ef9073c7e4bc9bd27071fc601ecdbae21facbc3c240fcb88e6f92aadf53

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8b2309acd8d9a868b70a150ea1936de85deef64435338bec994e5fb86433ca4b
MD5 6939dc44a5a5d30a6dd3c1115c540b23
BLAKE2b-256 6a9c30e0c9e83157baf00dee6f7b60f978e9684c125535b7c737847bb62e8b19

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 eeb86e852dcf674d9242ffb92650088edc010eb0401a44627fc35b0742360421
MD5 73daaf7bcdbd43b19903f1a6f06448b2
BLAKE2b-256 6d82b7cc55bf2e5e398c43d241317ad715e8b08d5c5d1a40b04a47c9dda611de

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a3793d8cbae27907f612dd02214a9c87fc35786ac5a10461d7dcb42a07a936be
MD5 64d3b3280a27eaf85ce2424e66b2f512
BLAKE2b-256 154bc9d539a7b801b082bb159523ce51a635b3480cd5698f9d01b8094fa5b416

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.1.10-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a242e9c2e2ec5de67f24e49a1f4ad1e52bf1f58145c730b2fa3c9d53473b6ae8
MD5 91749fbb1c74031688b48f55449e9581
BLAKE2b-256 f391ca5abf6d15b80d45c34fe96eec4f5f96923c862c557f55b14301108c2703

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ea654d799f8fe9b99d1486716041b3e745e991d974fec4f2fa964d261e9c8dc4
MD5 5f09b5014edb972498e63f16a13a7abe
BLAKE2b-256 4bd5ac348d5bd3f3046ddff1fb553dd7d114e7eadb374dfa38443d9eb895bdd6

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 99b17f033719552bf7443d2a86d8db51351f04605ff24b8cb10301ea8287b5c3
MD5 f96ec4319828e8a54fa9899fea040fb4
BLAKE2b-256 42494621adeca091dbd4ffc15fa2f82c9990829d4da45cbb61b36fea9eff1d07

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4be546ca7613b315ee3a6848995dbf449a08c2ea87ead299c71dad89b33773e4
MD5 049b1d3bd457a0c30c944f15c174bacd
BLAKE2b-256 3ebeb0662a6578e61f4959f063b10e2ea18e65a55777f4851174d539eeaf84e3

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.1.10-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ebfe9c68d71cf34f4015a54f9d3719885d746388c66dceacbd42f2da6315e185
MD5 bcc5d8b1715afd31a5fb23364fcaf676
BLAKE2b-256 c497227a05d8c6258999782f039e6eb978a9fc5a77b5a6a819622134d8d40496

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b7ccdca41e482fdfe4fe006c12de2c17f8b3a1c6eeb068d77a148530a3e68804
MD5 747ead74065c09e70a43a6c45c93c1e3
BLAKE2b-256 8d555ee842ca3a8eaa26a243237846560f6af6ab1e59ac42d6fff6ac9420db60

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a831d1c4b638b92c0a36cbcb6f25c4a52f33784f016bc6bf73464ca9e2b482e9
MD5 e6fd7c54f94cd090b86aec2ae4d2a681
BLAKE2b-256 5dab0cc0023d93d43da986c59a113fadb4c9b58cf23b4a36f120442cff32f818

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bef8d15ed722ec3ce90e4b7c7014da9654da5d5352c6a90ab5219a36f97e5384
MD5 cbfebfb7bd88f490e5f4dd998f481aa1
BLAKE2b-256 3cee04df8c38646e4ca1770bdbf981c3fb4eb7566940e5056c17ad5536c566a1

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.1.10-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 23b19e685a5cc220e783ac1f09c8e0714cda14a23eee9815d27e8cf88a801700
MD5 1fd016afb7a1b7a2fa10ffec4808d4d2
BLAKE2b-256 12be12372d052b5511c97c5bf9e5643419f98eb39c46383ce2c16a1cc335fbe1

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dc0e56929eb493a95eda742ceb603adba39d5a0b31f3a9536fc2104063c9f550
MD5 c15cb48ae5abc9773fada4d2582273ad
BLAKE2b-256 f98159f789c5a8f27b46b6605cd5e02b001e521063b9bd5ef994683ff7219dec

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7681adea1231878996624e5c8b6ef5fd44aa175ac68ea8451ff66a4bced3cc43
MD5 0b9458f39b6f7a7258c15b4a75b26baf
BLAKE2b-256 71f21a8cfc43acc12bc3b76503336ea6c51ab09f0161e27ed9c6bed63966f778

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp310-cp310-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e03ec163a2fc3e0ac4f08478d1b97981a078e5eedba712a8c82c3013c218904f
MD5 9968114f2908c7069672f84a53dc284f
BLAKE2b-256 d2cc52580d1734d84cb3eca50a5436f3e9449ed1b419bfd63159785d689a6fec

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.1.10-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 2.3 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.1.10-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 cc2ca55a076cc6be811d7f72511a820152d470081e5680af5876f122ac261cc9
MD5 bec096edd4d92be0fec105acc753d1c5
BLAKE2b-256 005c538b4cfdb993bb7b5e0003e930f46813137cbe4c58eb1a235118df68aae3

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48b5d55fd98bc152648b1a9dfc02694e18842361365d0c9e6e67f411b30ef7f3
MD5 f5d7981cffdc3bcec743d0faeb53d2ea
BLAKE2b-256 eee3fa51e8cb8e304a6ae8c885dff1b1f4b890ec7d39e7da70a7aef92c559f12

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7ce784eb1612e2ce44c51da40e5f56c2882013a055a08146f3168beaf9795a5f
MD5 13c6a4db8651578db4a89bfc159664c7
BLAKE2b-256 eb203c62f0f5f1a6b43b02f65ff96f4928f5f8fd02103cbd4075e3f6fad835f0

See more details on using hashes here.

File details

Details for the file dirsql-0.1.10-cp39-cp39-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.1.10-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 aeee8512b4ac0977033d9a895d56722c2175c5bfcff84b6592c972930c37c054
MD5 d4882c640cacb06d4c70138c521323cb
BLAKE2b-256 7bec862584763a19c813ddf8c0d3c954528d0985894fd9691297aa87a512b633

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page