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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.2-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.2.tar.gz.

File metadata

  • Download URL: dirsql-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 52a871b21f5b54941bdea6aa9dcbc6670f6d3abb47d2a5d62c2735b6d046cc52
MD5 0e8884bf297e974b0c0d55232c09f1d9
BLAKE2b-256 d18bcf17d317fcdb40f2093798661df1c52033069bbeea1e3c5293562a6a493c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.2-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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 751d22de44601159701ec91de4e4adcb9a813db6aa9b2fc6132c11821de5cd16
MD5 079f417dbb83e3db0ed44c0d25773c15
BLAKE2b-256 026ac599d009a4a56d905192f609a912a9f5b9c2df3e2d076838dfea1890e20e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7a9b2c9855a967be2b8c23b63c9407146942232e514fa8afe08112de957928ab
MD5 4d18a866d600110401b47e4499a6f79a
BLAKE2b-256 729451d9380ef4080170db06917ca17672861d7bbf5cb636c2c8176ca3920740

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 276e59f02a8bf4f4b001b87896ff47b38cec34795a5943bc0dc80fa90979511e
MD5 11c326cdafc6a7a2eb90500f8b39e2ba
BLAKE2b-256 0d8c624ffc693899c53e43ad44e55ba99002a1e0beb83cd97c3dcd05dee4c3ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e58b50b0154433b937192500b004eb027b9837113e6733eb23ee56efb1c28687
MD5 af9c02e6bf7bad443bcc039bf0dfa53f
BLAKE2b-256 059d745aff2f8e65f26ef81573a2174b7a1186aa2ea1df12f66dfb7036823410

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.2-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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 f0b8c05556bf0a899ffaa8cd48cfeba15031920cc7285d46804ccb5faa3940bf
MD5 c77ff1814685f407621a9bb36d8a2c28
BLAKE2b-256 f54a9882a6c3fe117487e09a46df641bdea833696a0e1f1bae37d64918b7fd7f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8dcc19d66de70d086ed919f6f8ebfdbfa1cb29f4ba157f18e5773346e058cc4f
MD5 4678786e80653e2f0600966aee3dca85
BLAKE2b-256 a9f5c07ed8182c0bbf78373ee583c89cfb2d598a14dd0630945f9fc852eb3cfd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ff8a5b4f5063543e370e9eeaa806c0e7fd3d0947ca3aa0a124ed53e47ae8fddd
MD5 4cf278a22217beb1e85e88a4cab612f7
BLAKE2b-256 8e5032bbf9601b3bf35e857983d68a3fea80d4cd41e8b339ab0da2725b795df0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 33dc28022b2a977949808d5647ce081cb2966555be73a3215215ed3e0d2320ae
MD5 6d39d63311bc141c03bce900ac880642
BLAKE2b-256 4d2910ce8a2b51b3a4d616686f944ecfaba82878df64a3e8a265c00fb1c8d035

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.2-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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4313f275f39b82c0a67e5d6ac7faa2ca68d0337d3249be09a3355ae0a5f316bc
MD5 beec2b59a5ff17f8b0b93f242eadb135
BLAKE2b-256 dd49c9ddb905e262bb1c20d1e1a799b32ce3e4638ddeb941b09ed3d3b7567274

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3d7dc20503725287b0cba471e56c8b122b9bb635881121cadc3ec3b8496cf54a
MD5 d8f2c36c4b5d7c9fbe655dadd9561239
BLAKE2b-256 eeb7de14bf3367022ca3669a25ff19f8498f1f0ad57c98e19c39eeaebcf0edc8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 93b5d05eedac183e6f418c67d1db909c086fc7fd5d6413e248b21e6ae23110a3
MD5 675e30b77111bb0f04b5d56935a7c9d7
BLAKE2b-256 636b84c745a941c7855c771763f89688eb38c096ed11ca739fcbe7c0a4597fa5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 09010464ad63a4be664bbfea436e0b86aaefbf85955f00d0104956e3ce51b3c1
MD5 4b3ca1bf39a6abd778644493af2a88eb
BLAKE2b-256 ed39c11cd9116864989099a5767e91f1383f9a82a17bac0033bf7e2f32bca9fb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.2-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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 2fa51aa1ce7fd580b2da03ec0adad8ae0a0d3746b76439b4aa582668376f85e8
MD5 fc881bc05a4ee99d2f4e37efe1b29207
BLAKE2b-256 55c84825673bde8444c564c17867de120d4655ece08afa144bf7b0e18e260763

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d2d795ef7e569d4d49449b7ffb1105d872eee2688ffe91de5ff88d99be4e6af0
MD5 991f60bba6bc52796bfa04ccb86efbe9
BLAKE2b-256 60edc71416ae1db16fb5872d34068c216b7f1ab44968e90254912015ba992748

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bfa07832fcdaeccee07e1897cd413574f731d136cef1362dd596f2b6c9c04953
MD5 cce702ded47fc9fad842ff7aac273534
BLAKE2b-256 e6129cbab0541b4f6ab77a31ab539ed99e20b653ea332384a72d918c6d1b0992

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 a4af037591b4eacafb7444d797cae867358faaa968bf5605e991e8a62ca784c8
MD5 9a65ae6550da0d48759913f82e8bfd08
BLAKE2b-256 cc8b052fb667be174921210219d3709f51a8e4d62b64e0165ed13275f283acf0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.2-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.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 79b99668e2ea59766040ef3ad467672e997243353f15218802504789630cfbc5
MD5 4994f532077d96990c7fafe5b01960e2
BLAKE2b-256 4833ef9ac11bcee29da2fb62d986a93773211c81f8b36e944e019b03a4fb1d2e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d102cfca7227e84f05231189c94fc4c68dbf4cf6fc95893029ca3ff4a1d520f3
MD5 d5fd2a3eeeabae22177cb6ff810c541f
BLAKE2b-256 3b8f8e1980d8c4ac83ec89d38c4cedf22dcc77c61099190d2336fd5ef328dad3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 afcf1bf4786dddc9ae1523d6d34782f685441a29148913d5c6b1119af965f755
MD5 b4c6cd479cd526e55220c96e6726dd84
BLAKE2b-256 23f1f02dca42e1f3573a8fa7e0d09af25e2f206f71e2aec2cfdd3aadaf9445f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.2-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 712e5554d5006f4dba0a4a7ac00c5a10b270ec02171011a88f9fb766d9829158
MD5 3ada475ee063e91b8c455042581a5f0c
BLAKE2b-256 33bf1f2a19dd651d900bbece64970cdbbff91e1c3065bc2a19dffcc1806678bb

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