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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.3-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.3.tar.gz.

File metadata

  • Download URL: dirsql-0.1.3.tar.gz
  • Upload date:
  • Size: 73.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.3.tar.gz
Algorithm Hash digest
SHA256 2ae4df7781a9209527934f0ca21271b2ac9363e7388f4543696d35631b7c18f1
MD5 eb9b71343140903c776189d0ccd04100
BLAKE2b-256 d5b825a36101fd7b8121fba5ece8967ca736ab41812ccd17e4f0338da4efa2c5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.3-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.3-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 822e3ded555a5cbaf7e852562ded43d4f364452d6b4dbd18c42553e8b6496b11
MD5 a76ce583d852acefb036b12bedd9b8b0
BLAKE2b-256 c7d20bd1d9d129a2040ba04991b1f3688ab0708dc3bd26e52f16d11ea9ddab20

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7446f1ef526655ca673ff682524bb2b7adab330bd869946a8bd58559dbeafe1f
MD5 91ee1f4844e03641a226966ffebcdd6e
BLAKE2b-256 9a26d7c8c006f42680c2907f8e67e7be250dfbcf1f137c4a2e1f3583ca178274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3b139b4539aefb644ae259e2581d87f24cc91bfaf4b763261e66613dcd37bdb1
MD5 2cb4c55148ec4365179f0a18a8d11308
BLAKE2b-256 817faddef058c89bc26b94d8d62178f596dea115487fd96c3420fbaf88256466

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 009ec58db9aa92cafd5c946342362368e8054f4c67885828402376fbe9bb68e1
MD5 5d798848a7d83d139e36f4f3d75febfe
BLAKE2b-256 1e0bf4e758e35aaeaf72fb931cdd1a05fd230df205817a82ab20e1c2b35da93d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.3-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.3-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 9391dd12e64b7129ba709fd81b8c1e6ac46be247f8670c53e9a4d1eae68f5d20
MD5 9b87cc2d716239000a7236e7ece2ed66
BLAKE2b-256 cc1cf038dd765bd5257721082e72f6e1e0e5274be2294b6dcd19ec0bf9ad0401

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c18d4255c7d92d247dbf0acd83b45f5a7656b6a3b5ff0f8daba0bde4908707db
MD5 f5967f58535f38c054bad024a414cbe9
BLAKE2b-256 6953521c5b78b95dd8fd65301ffe78d70948675416f70671d08f98ff8f4858d6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cfa36ace354c2023e67b03c19da5269e1b4ee24416ea9cd709e9f2e0c52345d0
MD5 b5701e9e92054d469194e60a7b720027
BLAKE2b-256 fd4d9f5f7bd9401e8142f60690d37b87f1506135d041a682f1371a61b569174d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a4d34b1477794f459cdd52725234ddba7d9e1f84b4aa9181f66c122ef724cae2
MD5 05e6370c504c3cf3ac5ed6fe2e8359f7
BLAKE2b-256 51f14ded7783ea762833f57dff0be408b878daac3010a34e6449b9f1103fb0cb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.3-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.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1d2029e2ba58d47469643de97859a8994588cd75181e4b67ec0abb4c6ef26fe9
MD5 ec82952892c4f32a3b97c1509fad33f4
BLAKE2b-256 5a38289e05ec77a6fa70dec007882d4b4f9c310b225cce9c6a434280217ba7c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ae9e8263e087285d89b0f813bd3bcd08c21d06320aa20d0b9a08d52d78f73cf8
MD5 ea5d276376c32a2441c5097ea6c4db0a
BLAKE2b-256 cf5215a7533ea2c427c96fb748c76a631a657c6b3409a8a198cd2b8e914f73e8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0bfbd0cda83a4bd35650ebb8f69a1b7aa96ffc7987c92d52d031ffad57f297ed
MD5 08598445d3197f4ea8ecdd8ae5f5ab58
BLAKE2b-256 5245fdc157076da1551893c07da55459dfb63166cf3183434934bc9d0749ef91

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7fb3983f6966a222913a49b19b0bb0a1b3451073f1ae90be96c229928b5f9a84
MD5 d5d85012d57d25af5e5ef74a7cf70817
BLAKE2b-256 241d0dd0f97808c488e40ca8cefd4e54c8508c51bea84e0ba69411573c83147e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.3-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.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 e9fdd9644605e0be12209bcb1ae478a0127999509de75d865040c61e27815566
MD5 6f5bfe2cffe5c627eff6c6fb8c50112c
BLAKE2b-256 0c6f009e2761c54b1901d44cf9ac4945cd0b4440aa23142a7b8d6eae9c892661

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 739d75ec3b17115fc16b12d7b5b546cca344a5f5127c173738d610f0a96e0902
MD5 f125e33c60f43e4ce4d0613961f51fde
BLAKE2b-256 a0c23b381243a64eb9884e990bcce2889ccdbb5e1eaa4d1bb6e63c36f0cf7dac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f7e214a6327f2d64728a4570feafdc4b953c42ec19baadfa0af19ede201f642e
MD5 7eb22ab7adf808d4619aa462a681bb10
BLAKE2b-256 55c8b375cba13535431c7b2db4d0b3b4ff52e97b18197c788dc29228ed762a1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c5b051a635f0d8f84daa1e6e648bdd6ca5bb2b95aa251f8bc35134d53ce44ebd
MD5 65db50b386ede574ad3407ab629f351f
BLAKE2b-256 35691f3ddef535b6ffc5357e71a1855a994f9778e17c28c392e907634a4e5689

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.3-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.3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b34716c43f59ae6d9a115c80823c8b51bba231865f955c09118a1596765f104e
MD5 c203b384183ce86bb935c4dc1b523299
BLAKE2b-256 fda54bbc6dadd446d7ecdbd8072106b1f638717a41dd04381a9994004050764b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e5d9353dd267431422ade1c2a0cb28ad4e625cfa2b92478cbfde0446df12cd11
MD5 697f4666f2183588b9ce2a1b55fef9ac
BLAKE2b-256 4ce35d397d0da20b6ad1fbf6a1e24d4bc0a52c687a6171af1d9478b6566dd23a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d0fc11790ece79903f5f3f42ee3fcef2012162364e1adc8ff61e9e759ebffddf
MD5 91b56bbba7fd419c923331e3bd5b588c
BLAKE2b-256 f5c8430ede4f9b38f8d7447d2a7fc0984adcc6291d215524234b4dc3221cbdd8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.3-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e943b99dd3d2a0e73cc06c8d276cb9955a3040864a7a50d5f90bbc17c956480a
MD5 dc3d786c76b3ce7215ec1863aff8e7ad
BLAKE2b-256 f909b22f137c603958cd928780a4d2110ef3ec445cdaf691f98f0a7db1fec2da

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