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

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.12+ x86-64

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

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12macOS 11.0+ ARM64

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

Uploaded CPython 3.12macOS 10.12+ x86-64

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

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11macOS 11.0+ ARM64

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

Uploaded CPython 3.11macOS 10.12+ x86-64

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

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10macOS 11.0+ ARM64

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

Uploaded CPython 3.10macOS 10.12+ x86-64

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

Uploaded CPython 3.9Windows x86-64

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

Uploaded CPython 3.9macOS 11.0+ ARM64

dirsql-0.1.5-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.5.tar.gz.

File metadata

  • Download URL: dirsql-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 e7e8465aaefaa0a2b9b46330e817d0485e101277b99127c8bfd1c5d00d176182
MD5 2c2b2a6e15e07c84d92071c1924a2946
BLAKE2b-256 ae020fa6f2448188acae592a74b009530c2296075ad47a804192f1e622ae4371

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.5-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.5-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3e150a6c92f8b7e11caef6d7db344086ddd11c51ccaae9da9739818eb4b226df
MD5 f9fb6ebf29da3862c6f1111b0e07d596
BLAKE2b-256 1c580193980e9b28adb594c490ffebe93cba6bb1dee6af525d9717e26da7908a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0dc532dec1fdb1830d9cada7d5f75ed9cd8ca719a812b2b6f4f23f9ae853ed93
MD5 ddcd6327369118c604130662981ead27
BLAKE2b-256 21b4c378d934b9a26957f9c4290fc7a4ebebebba01ce3e31b511251affbbc412

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1c392991f4870fb1b34373a9ec54363599dc4cc4318cb6eada257ece24163619
MD5 164c66afe9b46bfc7066438c3eabcc85
BLAKE2b-256 6e6254f098d66269232f35cbdda65b58062b29e1b0fe26bc0bec34691960c9b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 88a16e64b6bff7186c2467d8e3c86aea7e55d54877901fa884e35167f27e4721
MD5 f91ee6d462a833efeff6b241e2ae430c
BLAKE2b-256 8b6bd22a9927dc75e43a6241ac6fd9b777a2f0a70a9a2bbb72ab4b1e7e4466e3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.5-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.5-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 e8f874f59b241306b30e3fd439e7d55bc1f04a7fbea8a7e791e8a06258878776
MD5 5eb09103c835fb383e6752c8627bc8a1
BLAKE2b-256 ee61f47e0c4d8cced9798f438043c29005972cd71804787b1be4dab72f0c8f19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bee1408dd7fbe53916783c8c644fd03170b2fb499d46be386b54475f3c69e3db
MD5 b4b9e3c153297eb7d9afefc672fe1131
BLAKE2b-256 c2625647fe37430e1262dd2af4768ce421b5bd710c69ddaf1aebcc4911a09f17

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c712b69d595e4a7de2fbd668045781ea71c443712dce559fef41e87482f8a95d
MD5 3a43cb0177c76b100a0b83d650d8e3bb
BLAKE2b-256 8842d8c70ba749346512bb71a6febdce8c608a0755594c06c677e636e23327f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 48820fe2a0c56650dffeab29dfec90d7827e6c94ff8c940cd4a319ff9fb6cec5
MD5 a3dd5a645e5df89136757c151a981f77
BLAKE2b-256 e0efab00a64e1858cfe7808746beeb8c91b7bd7770b0b923fd23695a6486450a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.5-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.5-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 64e339f544dd762d916da023e08f3165f790b4b14b850a96249bff866d07a875
MD5 67412e3d3cece3363fc5afae4c66570d
BLAKE2b-256 f66254ded882dfc992d3dd004a470b6b75e7b97aebf1efaafd3b1da67e30da22

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e23d10c465d3de2f05cecab69d26536732a17a0228969bb15fcfa10eadb0504
MD5 71b814b3cfdcb048e812020dfe3c4328
BLAKE2b-256 9c641f5251792c0a00867974eab698e35b9a0077aa23457dd0c674a86a8e06a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 45b02c1b14fe432ef978db8a704d4348e24c18efdb3ee284694757235dfd8532
MD5 cacb571df9bcf26d9c65b2cc3742b501
BLAKE2b-256 e415b38a1acb6095d62767412cd2fe6f951a8aebb651495b66c7fd15dc38f802

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 1e935c2db4342079186ccb6cd86d33c618c87d042b0377e342d0238350f94e36
MD5 46e6b02b73a52aa2a313c12a8722f6cf
BLAKE2b-256 ebb147b652272684d2d686814dfce4029e06601f769c67a598ee544d9d656137

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.5-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.5-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 3e6cbf34e168e1e2fd33ff67b9fbb96ac4dce5bd31464cc51721bb5406fdb1a9
MD5 01217e0c24fa7ca573ef308297130bd7
BLAKE2b-256 24e795cb585a8acaba1ebab964f3294f87fadd301bcb3bf5716c12914c334779

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 66fd021b963050b0cfe4fd0bce2b34a99f5004cef3ef76ab01ab57253ff7cd9a
MD5 c24efbd1519d36850fe3499c6c84c185
BLAKE2b-256 cb3cd2336035854d6cf0fabe5bd3f365b7e3f50cc533b940bd1848ac97616583

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8432feec00a38b30bb8d3004f8e619c0e91ea556de3dafee00829117bbba936b
MD5 9c00f09ab066843f1ec6d7c788331396
BLAKE2b-256 f91d3e57cf10f61be0a672e66c0836d3a824126439ba0ee8c4e837b141beb1b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2830d9131be7d09c17131d18c24e89822f66a08ff04465d9ebfcce26acc31634
MD5 a20d3bb03fba24ca870a54b6a82f1d63
BLAKE2b-256 25e846017d5fb4be19b908ff719bc022ec664bf803f17137bf04ea6738cabeaf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dirsql-0.1.5-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.5-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 cc21d58b6d778ea7ebfc86e26fdfa40199713f4f212d83c82856e2a75726dfe1
MD5 c72d9318b43367711d0e48e8562b00fc
BLAKE2b-256 95654b80be7592de50d51ed0723bbf42a968f8ea0bac4f164741dc0efd1cfe77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f5b09d2d2396a7d9751ab57886a80484ddbc56cef1d6d4df3a6f1185a82994a7
MD5 bee07dbc416d5e2e5e123cd224031680
BLAKE2b-256 feff0d8965e7607475e6cd34715343c5d5b3f2e110eda151adcfd52a7ba8b9bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c68d0e43f24e621e8a1c9fece49d75bdcfd57c8945201027ce102f440eab1799
MD5 2fc9956b26c8892cd38c7b013ac43b47
BLAKE2b-256 df984869b78a331e99dabb48c94eb9fbf7fc94ec2e80e81849800f594c9a7283

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for dirsql-0.1.5-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 72d65142280468f343dd3b7f79ea5980d002174e3e1e0b7afce2839fd7167a5d
MD5 15f8e823b396d02b36714e9cb1365a33
BLAKE2b-256 cba0f37a639c8a860a3320af6f97fb2e9a390ab944a01e2ff9839c02f606dd9b

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