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.

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.0.26.tar.gz (73.0 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.0.26-cp313-cp313-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.13Windows x86-64

dirsql-0.0.26-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.0.26-cp313-cp313-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

dirsql-0.0.26-cp312-cp312-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.12Windows x86-64

dirsql-0.0.26-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.0.26-cp312-cp312-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

dirsql-0.0.26-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.0.26-cp311-cp311-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

dirsql-0.0.26-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.0.26-cp310-cp310-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

dirsql-0.0.26-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.0.26-cp39-cp39-macosx_11_0_arm64.whl (2.4 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.0.26-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.0.26.tar.gz.

File metadata

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

File hashes

Hashes for dirsql-0.0.26.tar.gz
Algorithm Hash digest
SHA256 7de096b0324a7c8c04d609048cc4cbce82cbee4618315459bd0e9bd94f6d3d74
MD5 411ad1d8dc86e7ec9f27ec9e18ab73fa
BLAKE2b-256 96d9e0e3fcac0ab0a8b63a8908230cd11de5e137356f36ff057fbca7ed8368d0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.0.26-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 2.2 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.0.26-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 f97fb3dbc5be2e35927b2518bdc29c5eef0ae57f0940f672985df8603e516be4
MD5 1e34e3001e5186d49671a15f12dde9ec
BLAKE2b-256 5f2f4570d82709b23cccc8ce94d41ce4936b4bb0a03bd5de2de22281966e14f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 563603ce8aa192776632105b697b0a44391dfdc291859347d4c388694c235db4
MD5 c1534919c91fd830945dde7e2f17f1eb
BLAKE2b-256 114bf3560047bf5947d8e028c5efeb071a0d62cf59df554e243f236f2e6126d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 af45ec3e4f8fa590f8b66b23cdb5808eb9b40e14791d850965abfe8d17747ae8
MD5 e54d16ba4fb96ed7d43b703778399509
BLAKE2b-256 1e2af0b445576d000695efb23c15616ded68cfd3d7afd506c457e8de3416769f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7be010eec4d5dbeadebdd94d95f5b38a928b3754cc69c3614867bc6955834799
MD5 3a0e2f66f93b87b9e383fd89cb94364a
BLAKE2b-256 42d4e1b16fb0bac44c88896d7be94645ac824ec88a0a371946ad7e7afdce76d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.0.26-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 2.2 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.0.26-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f19727fab9ee24b9b88d4825e640ab29d09e4125cf95f2edc796426488d049d6
MD5 17d23c9b5e8a762d8b57061134d98d05
BLAKE2b-256 296220ad4113664ce4dd918860c7f5afee8c5b452d2c0f2e8b28cbd84b68c77b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 65d38bf2433fce4a39bdcbe9ea6792d15010a2fea7010d72757e98ac64fb7481
MD5 ecee8b48710d4813de125dd2eb1890da
BLAKE2b-256 79239179bf0ac6792d8bee5174b858c43d1371967fe59720ed2ef1fd28368ad3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 629bab08bb3982dbe787af3239db4c164ba8ff293feb934ec0f57d134030426e
MD5 082ee7ff8f4a3324ca16ab81a7136332
BLAKE2b-256 8f426903eb0530493a3959472afc339e0ce856cae080bb5972b0882c7ccea4a2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 da0d631f6dabaeefb075776bf9ea5bec65f6d4d5e17a17bc0d921347f10c79c6
MD5 23d4d92a9e97c01d804487cda50d98a9
BLAKE2b-256 e424368341c73572ae7231b4cf79cedc687fb4745d78aa5dd2fb995fb44f9e1c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.0.26-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.0.26-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 63a6eac7a54d03a097deeb6e47c55cbe9ce14404290bf89ac788988115e48dcb
MD5 e6e06fa2ca81b2d001f0a298e80ea8b1
BLAKE2b-256 af1518e17eab278edee225b36d69b1261b0bdac89be460de7b7fc192bd24f7d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d30aee2b0588c72714c1e2413bb8f18a51646b2c7953b1d0c9fb25265c68bff
MD5 765bf1ac20bb8ea2b7ad118568d6883e
BLAKE2b-256 d9d5a971f61c8a3f05a9beb6990f68946b58659585202e94dd82626972f6d2ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 17c5565961d1d9d862dfeb00fee8b1e7bf6663ec2e28e554cd3f61780dc2fe1f
MD5 8d64fdd5db994b5b69ba581c8d75acbf
BLAKE2b-256 4e15ef47683a56de3f9286c5a0e6725d59fa994af16e39913494e3cf0185c28a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fe345298e5e114ff21f45e99fb1e26bb33e96cff36878a7c05b6ac1e2a7c6a1a
MD5 5e1eeaffcc2bb292b45cdb8e5b109cec
BLAKE2b-256 e84e6ba2d4eed4f132b656429a94980e09f926d083eac34786230bee355138cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.0.26-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.0.26-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 326bfaa1dd19ba088a07bf3c715d79710cab91f5b4eeb99c99a225f59cc8a146
MD5 49368566b9abf3dd77a79275f3572993
BLAKE2b-256 90c5477a479faa13bf527746a76ecba3ab3ef1d57498d98d675fa83eee976a68

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39ecada79e6787425e0bb1b929d0e521577d758f8f29ed67a0a18f501dc26a40
MD5 644a74a26760f7bf4861461288ab2692
BLAKE2b-256 8c3f28b420e5566f848e945b719a8828265ae941ddf77dfc0a8ceb7926e0a518

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d26373a7860537041630af964e29173cd65cd711a6c2951c63af01c29e73dfa
MD5 42bf1ac9fcf44dd1036d61a020511ead
BLAKE2b-256 eb54ae3b5051534d24f438e8ce4870e4f10d6569c2a6b41a8edd8a82924c7a30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 00497520b4214e845cbb8b0f33a195480ffe25634813783a4fd2100dfd8f5302
MD5 4a0017bd49d7f808c48e445a8c44b925
BLAKE2b-256 b66bb859fb1ba23cd720392e25b1458c4dbd5c8c134dd4d01a9e370a8f92acb3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.0.26-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.0.26-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 eebf6b696e7ed2d5aada9eb78a75ae69a7099f5b2ab5e207fecb297c7f755b99
MD5 a2ae4e3fd0f84eb777d1a292363eaba0
BLAKE2b-256 5438befeefba9454f26c3acefa3b7616b40f95aca6ab437eb2830a12bcdca4f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 186d2008968f8519f11b2e7f78e1f3df5200662bf9b3a55e9989ba6c4cfc4de5
MD5 3e598cfa1f7acacc28a2c8ce4276adeb
BLAKE2b-256 31e6620585519c0d4efe3969617bb377e3b4f0b670c9bc3dc624097cc6a3c69c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1ead1775b9d92b3b7400073eb09da28367283c7af5e21c067a82cb38f847ef98
MD5 a36dc6910a3892fa7b7460159d6620d8
BLAKE2b-256 77fe4f660ffe27a274a1b7f25454b8107de2c70d31fd16545a68eebbf4765ead

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.0.26-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 ea040d62ff4a156217d292edc5fca005a5793807edd69547304cff834ba9c379
MD5 256fa8d1b4f1b50f8e98b042c097d6f0
BLAKE2b-256 4e21a60f6e210b16f3929c01236366fb5db76a54b3182625f5c1c6ead9b2db52

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