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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.4-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.4.tar.gz.

File metadata

  • Download URL: dirsql-0.1.4.tar.gz
  • Upload date:
  • Size: 73.6 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.4.tar.gz
Algorithm Hash digest
SHA256 ada5434afd80c961c849e703b7916108516472e1475aa50c0313afac4b763227
MD5 d41eecccad6cd5ab68acdb733347d2a7
BLAKE2b-256 458d0bf74e6279557d1d360f9532235f3c97a172253de6e00e4cb82966a85c5d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.4-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.4-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 cfb0c0fc36fb408db2606e353d339b248694cc28b3d4feb62bc4ebf0b41d9083
MD5 27902df39c14fab99fca872b75e4e58a
BLAKE2b-256 4dc499bd66eff029648e611eb20d939e08ab1f7ad6fa1467a52428a3014fc071

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e816720ef62cf8bd22a12c6cdb3effc4a6685ae3f420e39545fa05515bfea84e
MD5 580a1b64ce1946e45fc39d3faf9286af
BLAKE2b-256 6cee98dbd8d0d5429d599d56e3abbc4e48536727397d187a3b4a55c712a7063a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d80a52b2d78cfdebab6063a7a54212c362da0778ff2c7a79356c1fdbe5770e52
MD5 483fd6aeabadc27c2cbd491490a1f5dd
BLAKE2b-256 4f998c36d4c61c3411064b581d735908a48861bd5298638bf4d2ecd0d2c8229e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 795b13bd59b38b314f11fc4db30401caf48960dd934e00d3246ea7418629f871
MD5 3bcc52f3a61e38606315be9caf259f9b
BLAKE2b-256 7be913688d8b698dc4d6cb2f437ffcdb1c559289e3b1a3420b84854aac444325

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.4-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.4-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 793b3f91ddc7d9c33503d62a0ba79b601ed64846491505eb22e02758de3b9e2a
MD5 bfc61df1bbe43bbc771023c3785a2e9a
BLAKE2b-256 027929e6a69a5d0a6c138eb76f94de46ce4747fa7ce478ed54d7ed7a76ef903c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b0aee89188e216d249a6dbd64354760b29331d8d03c136a93244d58990525615
MD5 2caaa246c073e0c2ff7a66dac9442f02
BLAKE2b-256 6cb06b5f774f5cf493fe74ce1bc8a7e750aceb061e61cbfdf325cd92d3528c25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 15bc746dbd3ad2548ec7c805de10c320bed9ff9fe38c839497be4fb51d5de744
MD5 e8c04deaeac69d320fccfaee1fc9949b
BLAKE2b-256 776a77214bde0d096b5c02ad26fb768b36a3c7cc7e48c635b0de1e1a703beb23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 45dc6825da30597546550aeebf9847de46ee216a44b6a5969e122e8e62e8ec4d
MD5 13e1ae3b5cb3d0a45c4a83a507a9c8ab
BLAKE2b-256 7f0bd4fb730c5034363354e15a3286d02497d4caeb0aea1e52227c35b5e4e3f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.4-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.4-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 c5cf9d98a97ac16dc77e38929b1b2e409f38fa55a909e48091053c56148ab7bd
MD5 648fc992ee3d19cbd984bb48ed891cc0
BLAKE2b-256 d7a18f4b687aaf9c05947871155156305da81c5f14937cd95a8cf0c35d2350b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0582d74dc9ed27fdd4ba9047f23ed0804328b45aec1548b9b6babfd97e2b22b0
MD5 60334eb853b1714efaf7107f9532434f
BLAKE2b-256 2b1cd8d74467351c67fc2dc601dbc65b0756e621bc4b39f2f13913af482c3f8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ab0725883994db26f71ab7c8601175fed9744ce70e0b2f5c21bdf3ef21a8b59f
MD5 942a1272fe1a1e5e4d91945b170be2a4
BLAKE2b-256 4258e2d73d6c3255b37b4362c775e1dead00b12b8865cbe157f30678fc58a777

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 bcc77021cec1ca15c78385e018221bf14bddb335183bb61b0c72aad113eba761
MD5 5976c47e6d8d7dd12ba363ad64954298
BLAKE2b-256 30be92f3d93c900bc58a5c2498c52bab534bf263808640dac44c5110f33dcc95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.4-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.4-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3a093e378edc31146b38f6bc26b26f8a505294495249e9f9f8307d04aed8a884
MD5 3e1b25b8eaf93979cb0ba1f25f36edd2
BLAKE2b-256 aaeafff9e77b222f6f55761e6bcd5ffeca88b7635de585b0b454a017e2753f15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0f4607c82e6f887ca30fe7e028f33bd2da05731b803167a6d1e49460ae4d1d90
MD5 429db787cd2556ef87e012214d6eb87a
BLAKE2b-256 77f648dafa0e540f780a330cfddd83c0f9ee13544d7faee7f29338400672e56f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 176d4a4e65779ff4da9d7f7001c8e2e1215319355d5b67a95c2e41088e0df109
MD5 5912f01b417da7b49f76dab38f33e987
BLAKE2b-256 bf34096522fc0597e86e178917081df5c3d4c6051278195b48aa94d76fd44bb7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 7f094dfda9dee5f857ad38568e59398acaa8ed90b15422a72ce7b31c65e15ef2
MD5 d6e650af6a77f895993af28c0bdb3529
BLAKE2b-256 773c7ae03ad448fc6a768a01661566b20633998b06d3e8d72acec407d49d2e16

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.4-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.4-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 8f456fab7ce60d136132ba001758afbe2a04af9c27674413eab0e4345f1e4b4f
MD5 1fc00c913747e9f3f11243d91b5466e8
BLAKE2b-256 7e62328f4c32db36a48857445c6346dc126e0aa8558e005b9b5404a7c3bbc34f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c8f94d26832bafb2b600d2216ef2afab96d778c043962da7055f080ef6455cd3
MD5 08b2210e184973fa66ebc749fc02eaef
BLAKE2b-256 d26b4bd9219ac19ac9d546eaf6de60055c76f180027acbe3ea67b9d76efc591b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c126735dfdb52bcbc999f9269923404f59e5bc5321956dcdac57a5290583df2d
MD5 a45ff7523bd105328301e0efd01cca81
BLAKE2b-256 a21c16e63f8dd03338d76003dc2814b3a9921205a6bf37448b52d51b83097a9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.4-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 e8d38f19e27680991c6266d3e48b1fad2b9f2108d7de9f3e767cf41ab92bb649
MD5 ebdb1631f6a4e1d5fedaf71d10d451bc
BLAKE2b-256 c3fd004e8055440d756a272cdfc5e133a59b347ba88d07e2d1356d7538e98ce1

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