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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.6-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.6.tar.gz.

File metadata

  • Download URL: dirsql-0.1.6.tar.gz
  • Upload date:
  • Size: 74.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.6.tar.gz
Algorithm Hash digest
SHA256 e617824acfc0b0ed1d509bcd287dacf314be458349c3521da7589327fb86ff9f
MD5 51691c12978ffd7a8f00fa9412962c21
BLAKE2b-256 c5697516ea34ec3763d6a633f6a84e2380151f8ec181762a837f75cdd40f9f80

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.6-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.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 46974dd3faaa95aeeeff380938605cadeab526c4a87b2a65fee9de1094669acc
MD5 b932eabf913bf166c28779ee567a4949
BLAKE2b-256 40f1b9e111df3257137bda2b78dbd4ebbf27b46f7c90d5d89246fc72d9993a5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f2433456730fd7ae97f6a27883f4123283f8fc41c3799330d094e0e1b9e4336f
MD5 6f9ac642bd03eb69da4e1182e9a4300b
BLAKE2b-256 b3eb4630f9b478a22ebea113afe63793f5e33ee35cd70dcb5c37ea8a967b7caa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0055f45280040e351d59a6a01e98e5fb1c297bd5365cc82158849f5c97422b11
MD5 179c1cc79fcf3f6ca654ad25b82e6709
BLAKE2b-256 b1e4cf73c48a027bc2564f9373d1646439aa791b820da295d2e7e6f5e680b725

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 123146dcf6cb87443e03029fb2ce26ed8028cb5b1da2449dbc331a488306d141
MD5 b7e0d4c93c17b41bfdeae49a5b139e76
BLAKE2b-256 ed923e78715980140c4189cc194c4f5d25c3fe7a355ed6a6fb1850cf3be8ee4e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.6-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.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 16b968fab06a915719281d6dfd31de1c2bf6aa225eac81b96bc164b8afa7fcc9
MD5 f66b8e1edeb06fb660bbadec411d3e10
BLAKE2b-256 b32b7a645283396df9af2ec9a1e8fb39fe183ffb240720f432dcd3e4cc3d56a0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89c606c063ae6e6375a426c9b71b9beb60a298262b230cbc0967d04b6efcc2c1
MD5 d86341d4ee0f58517e8b25051ceb76e7
BLAKE2b-256 8f8ff02cc1d2b8a1099a66ad7def939b53b0e296bcfbcde1cf2a47eb7c52f4cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8ed5a238aac3852c987ec389c5a3a1da49dcf6d3017dc3dc67908355bc968451
MD5 119a0428816d72da8c9e8caee0daeccc
BLAKE2b-256 234e449b16558c24ce86f7154aa578404d85118026749b1bdcc61788175cb153

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c7dbe8d8890083754bb0173e29476fb53992ca14656d3bf1feb09e58ccbe1217
MD5 ea4d14cdc082c38aa9cfb33ed58f4075
BLAKE2b-256 4734e75db11adc36df9dc95a0a7d130a3b2f94663eb5b00f461917fabc5deb36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.6-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.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1feb9493345df462b5489543b5edc843d71a047749fa6a1ea7ecd7af7f407d87
MD5 a1d51d75e4c30db30b4285791e5aadaf
BLAKE2b-256 2374daa9dc96f67cec54cfadff7a2eeb95c1ccdaf85c7c92687bb7fd520611a4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f649cd455a67e8439e544d484930b2c6285af273c87f52b47c57ac0b26508a84
MD5 037c637ac6d4fb9299fd1d95e2f4a727
BLAKE2b-256 1e8dbff8c499c23455eb79535dbb278d107e42166a3aba142a56b48e505b59f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d975519c42334406dfded66586bdb79df2c4780581ef202cd96c1cc59acdb274
MD5 e7a875ac0e9f75fe5365c2c9df9a6464
BLAKE2b-256 e8fa7ccbb9e466f6b996b3e0e4f2855e390b461a8872280848481402c8269b05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 8864c19e8129935322d2402bb4e0df1e36d62c0dcb1e210283293f98daf0250f
MD5 e00b75c5ff572f042235b25b550466fb
BLAKE2b-256 caee4325a25a5d7424f3afa57994238716640c36bc109e04f9a2ff0cda4644a9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.6-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.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b7d3c13dc70cb55903a12c3b43d1b06da611dc76fa92cecdff6400504d47f8c0
MD5 80248dc491e42fccf5b8e17c5f977bd7
BLAKE2b-256 8e30d03651f4e115abf1ae80484194f3c2af2c8415e585fee553f2201020fd37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3b1ec0551f5c6ff96159b0c5de8219525b4f730fcbc89318bcf051aff461b98
MD5 f0fafb7719d7289841183c798ad65986
BLAKE2b-256 974505842337b3e15796baae3a6a7e34534b8f44c920bd0f20f94894529dbbc1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 38a36ee4f1d950f26356ee8183e9d46e4a63a1514c69cde24487e8a03be5edf5
MD5 155b7a129feec735202616c437850bd4
BLAKE2b-256 b6858906ac2facb0854eac71b0be7c3b569c7df8ed16865580f3440d0c7bb393

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 27aefdfb61e72de438f3b96c6e8ad48c26d10882fdb32d6bedf3be31aa56452b
MD5 6c7fcdc66d3df3e623af9eb0ae2210f0
BLAKE2b-256 0130e97fcfa2121309413fc7a6a49ed59723d79c7b70d53589b4a96ce48f928a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.6-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.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 7c2428981f3d4bd6b1645fd5c7cfef21d68440e1e53cfe055832661329b00a07
MD5 99c9114f689cb1f9a260e6e937ca3238
BLAKE2b-256 556e1ee112b288dcea42d621d6f6342f7944a858fe4b4cfbf278f9b919bd99d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0f15c623ed1ec8c17992519b4f217a4b0c0ae1d602dbebb0cd7d3741d9e21b0
MD5 29e30a69c0a321904ef759b951eb2dcc
BLAKE2b-256 f218f2282eb963799c7c5a1b267d6ad4a70b933828899cb6883657e40120b2ad

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 72d062e32d83c8de83468efc2cacfbfaa5a5a6e1538c2656cc09869bb2ea03d3
MD5 9a6a281a726877bdaca14f12d6a234ba
BLAKE2b-256 a61c1613b941a9a16bba18954c2908a60e1b610d0f9a7692f51c2401a4d1cc1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.6-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f0581a1a11472885b20ef37a5d1fda7129680fb5687319489b5799ed607556c3
MD5 08077785c64e3b4f8901ce6918b9b551
BLAKE2b-256 d1675a404927218c3890cdbe29f47e35e441fed59e6d94c6c085a0677987fa56

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