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.9.tar.gz (76.2 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.9-cp313-cp313-win_amd64.whl (2.3 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.9-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.9.tar.gz.

File metadata

  • Download URL: dirsql-0.1.9.tar.gz
  • Upload date:
  • Size: 76.2 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.9.tar.gz
Algorithm Hash digest
SHA256 0866a545ed9aa674ea22d4d619d25751051da6af61958104b76515343841559a
MD5 ae38628d53b6ac3cc324d0ca2ecaaa05
BLAKE2b-256 85a1bcb7507c1c1cc90de87466b7f0c18984e8a08a6905290c539d65bdff62ab

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.9-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.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 bc3f724457180d5413d65a2f30c1523f472f7336ff3ba223fa8c89a0ae9c68bc
MD5 9dd8b5943f6dbad4964cb00e5f6260fe
BLAKE2b-256 315001906a7dfbcd60556c96b725325f87df7c9375950c06b4dc593ae5e4ae73

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9b8084c06a27f7eb3115ec41fa83a0ac4047abc673713e82de3533230126f90b
MD5 8d3fe6412ace10efd911d3f60efc3a88
BLAKE2b-256 04ac1c3f61a7ade9ab9cfb6a55e7d9eb6bbacec77347afca0e8456e56df1848b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 88164f55dbaa4a46805601b8e5514e77fee0c922ead1ffebbd67e0b8f56696a0
MD5 d22caa9a63c99bbdeedaac2e7bdb458e
BLAKE2b-256 c5aa992d878bad6b7fd290bd6659e8a63811a326947eca6042201a94ad9672a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 326c770ac5ae942e5865c73198599a7b44065f087d0bb0ab6e28414e6af17a07
MD5 f4e3a2b3c483b18452c19fba6e94ad02
BLAKE2b-256 d997bd7dc9421c0466aa532f053d4e92279cc259e58118ebbb13278164449fee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.9-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.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 bc4b13e737b38e0dcd10c49edb47fc20a775cbb362653f2772df8d63d2c6951e
MD5 5691fb23c88430903d096352c04b78d4
BLAKE2b-256 1a1d1c4af6055e1c1473174b82ac0dcb19de01bf83c010188c08c695db72145a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b90c3155e02ad0b34d81e27de181c98488721388077bbb7bcde55b1e3b6f1052
MD5 2fd11dfb5f4bd1e93f8ae53a4bd3d5b2
BLAKE2b-256 b8bb00fbe29f776e5c852601a17ab97a492f0fa514ce71bd1d4ed1ed9028f7d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9878d3fabadab586169a39f4157fc11d6f1dae49d02b8b2fb7b9b5354cf38ea5
MD5 02d2b088a55564a21fe54697589d8767
BLAKE2b-256 5c2fb046e412916fa0a0ce6b2c23766e32d8e530743f23f69f5dd90d392887e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1e1150b4876ee1cedc751748ae69c44e0b51a6f29bf059c8c4e09f20d28ab5da
MD5 012148957efee8245c544ea379698c0e
BLAKE2b-256 8073d19672bb47c7715c878c54d58278206c13e2496e8f0ba8adea87a8175fb1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.9-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.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 de9e0b77f5614e8e52ec59dd60978cc29459d1fb800430725a2d3b4b34c83694
MD5 efb3b4f8a65404d37517b7cc69031dd6
BLAKE2b-256 2515d9b974b258b91eacb0a5b773131241980631fca605deaee5ffc69a863884

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0ec05ad3624d70caa0ad675888e21bac2ceda493208222e91e87913d037f3a18
MD5 926d61ae11efee41e948d144d388d549
BLAKE2b-256 803d6cf777c0924d4a0495b7cc8501eb5f413c6f0dced7d310d6d3dd57155a28

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 58132946b4ef6c4059c62099ee22a8ea216fc39dde65619de5fead0a25539aa8
MD5 b7aa868e10952c5e43975836309cea0a
BLAKE2b-256 b8edf076434dd3a6a212603d30bf845fb3305c1560c9d7ee7c290e80fd0712f0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 cef14a75f56a9258d9ce76928e04382614b5044494df76feb2622f4ca8380b16
MD5 ac90b027f91cc7be65fb4c0dc28c3e97
BLAKE2b-256 e4634274a0a43eff916586c99ebf755aaf15b9cad3f403640abb2732e6c98cae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.9-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.9-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 153ffd78d900de573f98820b8e99f8bebfeaebe5d3903a04258461e185006767
MD5 f327506817a1a5829e019124fa9d1a63
BLAKE2b-256 856f43c3d2bfafc942ec6107bb9da91b1b8c4ddbeb756b5f189c3a506bb3b93b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 abc41e553f38fc78c0b19c6de51e4e72c0c4473ad73e783679f64826dafc8ca4
MD5 95576462101bc1f4a7e73e6d4f3fdba2
BLAKE2b-256 a2adddd92b92d315e2b992784125d0d647d50fdb33c724ef63ae44563cd02ae9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7b7ad33c3fd8797827c7f536269e01b3e5e9e5b417904ca1a1345a3b1344abff
MD5 f5190ff5432076dbf32d79d77fa3d2cb
BLAKE2b-256 4aa667d1961d84a355c6a6bc3c4e7bd41569935e17eee02dcfd9d8c6f8c0c90a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2415e7c63474c50a72889d962d8e43af5ab0f0ce5fd258e94261c5e67b867745
MD5 976d47638b275ee1cea8b034669ca377
BLAKE2b-256 b64cac497dfe74b0ba2a7477b57a3df811bc9a0a949ee1e2451da22c71a68d5e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.9-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.9-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 e88c235cad17a83384e67c64bd2d326baec5b481090f8d9033a06254de54e1cb
MD5 8c3c976a6287f51048ffdd7df3866248
BLAKE2b-256 5dbab4a0c79ce0a0f8ad2863cc66e965913195ad4207ec13e4e86acd28d9586e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0dd22c7d4647ad5aa0d5b91bca7a15eeb3c36003c7216b8dc19077f59f31c751
MD5 f391eed1b003b4d74739cf5d616a114b
BLAKE2b-256 664d45a89c3600bf63e78d501ae08095ac00e4d0c5d36524797a32dc15b71197

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 793ba8fa691f6a765d94dfd117f3d2f97652914884fddeea4da950dd4600a55a
MD5 19ecbd34640ca4d7d79f197869d34722
BLAKE2b-256 c199d7e45791a5b23a7a2697b0cd54e55ce1474d02fcd411c961db1be4896ebd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.9-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 28a4b3b1179111933ad4bc2834b1fa760ab7297c5443f4e9a6dd5731213db129
MD5 b150eb07628d87c3bc11733ab434dcb5
BLAKE2b-256 36786ecc8e6a9776cea7a2e7b06abc1227ddbe7fbabba18ec7d88ac9ec44fd48

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