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.1.1.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.1.1-cp313-cp313-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.1-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.1.tar.gz.

File metadata

  • Download URL: dirsql-0.1.1.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.1.1.tar.gz
Algorithm Hash digest
SHA256 a7e0d746ad23693063cb6c3ff2f394217bf77a79af187398678c4ca47b73a243
MD5 a2b86ab17497a80891a86c002841d9b0
BLAKE2b-256 89a07f43909cd7f4ddafcc64dbde8986f649a358db208765d2e4e679bd8c98c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.1-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.1.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 17707dcbe3c1b3b79920ee5168dd3adab1d3bd58fa24efd79ab482e5b556c7d0
MD5 e4b2aa1c16f0f728319b22ecfd7b08e6
BLAKE2b-256 51497fc1d6bc3ea954d80fafee5203327696e5baad9ebe4e35ca0f4d68ba30dc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 267d72a67d5ee5158903e58891fcdc5d4c76c079ec8504775c9a1a262f064bfb
MD5 f74f5533aea6a74b97c5e1cc8e364e7c
BLAKE2b-256 cf60b50722c7426a75547492fd5e1c910f1ba1cbeb6329c81af19acb8930f7f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 efd3c4afa1cea368f4d2ecac2dc8d4a14bafc162c3d6a107972d1a54c6d6da4a
MD5 5c9dbf0a998a9faff29786bbe0b52f95
BLAKE2b-256 284448ade58419170e1b88d89f6bc9c2d573cd0a26187549ee1bd0a9ebe686a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c32f912eabb61b5efaf7f4d4897d9ea5ddff94cd35663c3b5fad0eca0d9f00b2
MD5 0049ec1b989ddd00ca93a1fd2efe03e8
BLAKE2b-256 535731a59e71afbebfc01f66e986d7bf59c0761cf6dec420a692542d9c707b46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.1-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.1.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 7031b6e74b1f1d276675596d90450401c0699c34e4af15c5449e0217a6b46535
MD5 aaece904c9349af8df4b30b6c5743093
BLAKE2b-256 2143fc8a4867228aef0a6c8cc8680b6b66975e12594147820aa506af3f5b1249

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 465a52d18f8da60fe486118432878a03a327d9fbd204eddec5a3fe9eeaa4406e
MD5 6b2f408f5d0b343d512a7e19f7ad38b1
BLAKE2b-256 05d68a39cfddf98e9c5d0104672604d3e71772e032b1d6697dde0faa5900a75b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 67c668f2c3cb4ee1b120ff7a8cefce7364444f181c62f961cf3f55ae8bbd8f0d
MD5 7270cb37038bc97faffbe51d7367ed3d
BLAKE2b-256 45625df2562da2d7daab51180e6fbfbb7e67aa4d2a6345d9fec1d20cea0da88d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 0237123e01f38313173d759a6a46856fee6a7ac92de5649365190fae834d4c9a
MD5 85ab98d7e1ded9452afd9f3d53011a3d
BLAKE2b-256 ebd1032ac851f8f7582e20446d519428f68245eec643b9a70e2d75a030480c65

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.1-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.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 7def8f821c950d0ae5b29dc36936461bc7163cab7969f3a6f370709bfdcc370f
MD5 7255aa81cd008b8f38b240e1ac348721
BLAKE2b-256 d043a5a96e5b888afe08b336ac86cf66fb7fdf321331570a04f7821dd190ef82

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9ce7088b8379952852ed38916980e73dc205e7d51011f463824d612a4c0afc71
MD5 30d73093c1c481a3f3c35a3c2abc8fff
BLAKE2b-256 7f81634e5ba140e80d27f4aaee34fe58c4ed1dcdc84f907715c13951dae3c303

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a92c58b77eb3c1507ef3e79a5562eea0cf2889c005cf752c6e703d9fdc9f2225
MD5 14889728294353d58a15430078a11a05
BLAKE2b-256 624c5a450c25ca1227856310e6f34831f7925e04456dcb422ee0351672e0afbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 31d820c9270bbb63c033ed7db0f9d850c5d733cf0a6507079ceeb18e991d6b38
MD5 ca28f77bf573d3a3992462d85464dcb3
BLAKE2b-256 40d6e8f84177594cec2e856b7c7a6ab0c256c637b3793c655d3e6a326984d338

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.1-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.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c1c845b65f14b0b6666be5f3d6e2c49876f9db185278264890fe85bb82fb4b20
MD5 388a2380fe343a2bdf75a2294ba4ebb2
BLAKE2b-256 d14c3af352ebff959bfbc047791d9b59ee3e3503f12ca6eeb226d16cb63d5d42

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0e766a133b467ecff0592a771125d8a406a5aca466075ad455a31e9b55c5d455
MD5 26cd6170e77338751ea8988ae92f5453
BLAKE2b-256 0a3cfd5c91e6f641dadf2aef916177f1bc148c8521d92b90dea4537c87ec757f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 15807a7438ebdcf4f2409fe5cf1e56c8156097b7bc1a66a3f1fda303a3bcf069
MD5 c03fb1165eb1ce33851b3d00faf98374
BLAKE2b-256 1fa361daf113312efb43e06688597b1f73ae037c997f9b496409b5c076d970b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5d18248b75a4eccd3aa188b89b23c8704671f7df362826156937a8bdfc02ec40
MD5 a3372a0b285e853ec98b4ca38a73f67b
BLAKE2b-256 65eb014937fa3fcaed115b234036e3fa35434d44339e65e9734934f1873a687b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.1-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.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 dbbdc7ef22bf96c10bbddae9dbf61a8147878661b1bf66eb1f330b88e1937379
MD5 1b07da7922c4c6ecb0d5bae7ffa636fa
BLAKE2b-256 210961b333f8b7ceafb8234e78f3fbcefc45a63bd049d76cc60dd2de42ba007b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 123f3dc08083abb6bf11cef738029dbd7a262f30c7ce95838d12325fa8acb5c3
MD5 472227bace14b6f7771659717d0cc93e
BLAKE2b-256 af17e457e2b03abaccc3530e6570975ea95158e54725669afa1036ee0391c949

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 32457deec35aa3e301851f0abd005dc3a4a2a917ef4f6c490089dde09b99e3d2
MD5 f95ba258fa3707854ea039b21b9ebfbc
BLAKE2b-256 619b54420a9a2602855fa032f6ca11b0002fd9e672ca2e6c23a422701f163142

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.1-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 599d06e0cc428eee5e36f29d9d618aa382296007b69ec15bd0a59d2ebe3f3200
MD5 cd4085a9179765e5cb80191687bb8333
BLAKE2b-256 7b50110f84216b8b91d237ddcc26366f2e6043f1632b3c8c5699f35e2afb8fcd

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