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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.8-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.8.tar.gz.

File metadata

  • Download URL: dirsql-0.1.8.tar.gz
  • Upload date:
  • Size: 76.1 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.8.tar.gz
Algorithm Hash digest
SHA256 b40e772fb48dbab3f1ace5da36def8a558a3a7dc62c7128c5dd6bceffcc0fb0a
MD5 63157b0a1ee404cbcd5c8a33c8481785
BLAKE2b-256 00ad1b072730ea220fa4b009a19518fba22ffd215bb2bfda80ba054a4a9dbd0a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.8-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.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 6c218a4fbdd97db7acf5f08decf9d3e01c9d218f6282d966b9b5d017d99f6507
MD5 3001f3499160071417538cf1d713bcf8
BLAKE2b-256 263db3afe6acf23578ce458c9641142e4f61111b54412de843a6fd535f595a8b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e33d2581dcd8b88eca703089bab6905eea91ac00e2415803b466c1b782e3be71
MD5 389896898ccc29acca3e33f32b41651b
BLAKE2b-256 aa6f76ea5e9604adfeeca73c0d2d03f64ae48f8d135fb945b1a6cfcdc55ecc2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 399070ec08ebab828299f4893e543063dbdb6567684541b9392355adbd3c178c
MD5 add0f2af3ce91ac03c57b86f9347bcdd
BLAKE2b-256 2eddc1eba0abc340d1053d7c746a6190c42d1f58529aa754b18e3a65ca84ca6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 430445b556fa9fac97972c1789a934e40ba7ebb05220c6959039ecaf6025ac83
MD5 175d0a17871abe2dec94629403f1e227
BLAKE2b-256 e5abdde3eb1ef993744f3d7098bc7871053457d5416f81137877eb0a5ae1f235

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.8-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.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 dcff491a8d13de2cfdc45d60854e7273008f0323dde11a460eaeee3e8065804e
MD5 2e67849304bfa56fd3a261ee11b4dd3e
BLAKE2b-256 b688cc0f753961c1c3795a5054be7bd6a661d98c610bed7a0775c6c838d10cf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3813fa4623c1a7cd06271c6df732dd88ed62519230b3f944eb86d19f617ca17
MD5 ba88af7e0620ab6cf885fb8d1050a982
BLAKE2b-256 fc3c41f375469f0b9fd54aeb50aa9a0418500dda759549d3c6ac54d8f4749bd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0aaf64d6476ef1ee321d0f46ec3f8d6829df521b3f292e08041b2689d263caf0
MD5 c275154145e54363f5f584030c092bfd
BLAKE2b-256 60fad920560a81b5e9263457c522a69179a79aeda3e0fced3ef5bc093f795f43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 49fca124265cead51583d956063c35cec4523effc2be4ca3fca560fca8e348bb
MD5 13d64fbdcd439f8ee989eb95fcebaeca
BLAKE2b-256 e464ba39872c5dd982c88dd3e68b56998161241150f381041fa473ec1ecc4e61

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.8-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.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b5ab5f6b27cfeb8ae3defa5d577cff56b441d6b2bb80edd0f574b599f858013b
MD5 bede7a5ccb557307581727bdc00345d0
BLAKE2b-256 54caa6bbc7fe37a008373458486a7b1b3d45ab9eecff743821c9b4935ad1efe2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 054ffa5353f8af25723586ed2ca5966204bb7ee78ea691b2515db0d0d4bd9da4
MD5 aa28e7917a8a8dfd4a72c956172756d9
BLAKE2b-256 9576d39c2b8ab6857eca20adaf84ede08f36533d7e05c7ab42f5caad9fb8cba5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 693e68990acbdb4037cbb0f06a7d36144946087b72a8ce5c80b84c9ebb17e23c
MD5 7aa0ed94936e6fd33eb864e1d725b712
BLAKE2b-256 c41cbcfd7f797f0b5c1124e4d38a5bb01dd4d13a918315ea360404868f3789cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 9313f0e76dacbbc71ebd5fbc8f93b4115436eb68e9ef926e4c9ce6c2fcae7200
MD5 b0a0b609f7f2cbe8a72b39dae5b0ec6f
BLAKE2b-256 1ea9d41b61133b204182b93decedacde2e5c3cd049ca33726cb2d4e3ef6241e9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.8-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.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 df983ed49f30c66476d53f24e572cbc8b8742a13e5dd553a2b5d47c6f376fd82
MD5 ce7f28e5ed602a5fb5fed55c1990e268
BLAKE2b-256 be49a537adc444a52b04cde8c54106f55908b4c5a9c17b91e6057cffc87a8a0b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad2effaf30dcae6ae37104615345a28a63ad8f385b3c9c7cde53fed3336478ce
MD5 010f6bd7b2642347b9c3936425f01c46
BLAKE2b-256 92b3472a85d58702bf3915cbdfd2416ee7006ebe385e19b4aa6452b0591c4008

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d8ddb590fef5847056803a7c0de8af67191ecf2df67e36571c4aa3f4af90c1a6
MD5 2417e2a96a479ba4bfbb95ec38c86138
BLAKE2b-256 e5879c92b8ff1ae939f885a6cb128924f2ca274b9cf0b47350335795c8e05727

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 431b8ec02ca64dd70ca7bfb69ab506c8c7e44e562868f6d5357f5aea7c2ea8ad
MD5 d3f897f8076ab773b47fef76fcb1ec6f
BLAKE2b-256 8d4a1620b371e43638c3ce256244a8f27cbe269f8e0c63c13481bbbaa6cb5567

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.8-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.8-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 65ab9f8d54b6d2210b0721f8c1ae58bf9bfb589e978a571bc58c25d7d0b037f2
MD5 fd51ad263ea3af070d1bfabb2bbdbfa7
BLAKE2b-256 ddd7b54cb851751aba0975acdc2cac731f30f3b993675602b9f4ddd85cae9775

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b70b41f5a2e372c59b7469bfc157e455210ef454ff9375693fed23c935e8cd29
MD5 d3fc99324b563412b91c724d96d52a17
BLAKE2b-256 b7a4a59a6831ff55269da050e9906680dfbe19582a7205c2afed164b9b9ec133

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9bfc2522eb194b5ce656051a9e07aa9f7d2819dc24b1cbb766b6569355704a65
MD5 2f629e1317fa4e7c4d24f8c48f29f2cf
BLAKE2b-256 f26347f4dfad9f594d2c2fb40f84c7c5f836eb43b58ce88eceffcb6a5aee46ac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.8-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c7ae62f74286ed6d6582236be314614de7e0c8e10e87b994310e28c1c6792ecf
MD5 1d6a5cc9581f45574254fe91b76059b5
BLAKE2b-256 96120644175f53bb37bd6c061c9227b422aecf471bceb985db0fc6ea3dd31cee

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