Skip to main content

Ephemeral SQL index over a local directory

Project description

dirsql (Python SDK)

Ephemeral SQL index over a local directory. dirsql watches a filesystem, ingests structured files into an in-memory SQLite database, and exposes a SQL query interface -- the filesystem is always the source of truth.

Documentation

Also available as dirsql on crates.io and dirsql on npm.

Installation

pip install dirsql

Requires Python >= 3.12. Ships as a native extension (Rust via PyO3); prebuilt binary wheels are provided for common platforms.

Quick start

DirSQL is async by default: the constructor returns immediately, scanning runs in a background thread, and you await db.ready() before querying. Each table is a (ddl, glob, extract) triple: the DDL defines the SQLite schema, the glob selects files (relative to the root), and extract turns a matched file into a list of row dicts. dirsql does not read file contents -- if extract needs the file body it reads path itself; return an empty list to skip a file.

import asyncio
import json
from dirsql import DirSQL, Table

async def main():
    db = DirSQL(
        "./my-blog",
        tables=[
            Table(
                ddl="CREATE TABLE posts (title TEXT, author TEXT)",
                glob="posts/*.json",
                extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
            ),
        ],
    )
    await db.ready()

    posts = await db.query("SELECT * FROM posts WHERE author = 'alice'")
    print(posts)

asyncio.run(main())

Multiple tables and joins

db = DirSQL(
    "./my-blog",
    tables=[
        Table(
            ddl="CREATE TABLE posts (title TEXT, author_id TEXT)",
            glob="posts/*.json",
            extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
        ),
        Table(
            ddl="CREATE TABLE authors (id TEXT, name TEXT)",
            glob="authors/*.json",
            extract=lambda path: [json.loads(open(path, encoding="utf-8").read())],
        ),
    ],
)
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(
    "./my-blog",
    ignore=["**/drafts/**", "**/.git/**"],
    tables=[...],
)

Loading SQLite extensions

Pass extensions to load SQLite extension shared libraries onto the connection at startup (before any CREATE TABLE). Each entry is a dict with a path and an optional entrypoint init-symbol override:

db = DirSQL(
    "./my-blog",
    tables=[...],
    extensions=[
        {"path": "./ext/vec0.dylib", "entrypoint": "sqlite3_vec_init"},
        {"path": "./ext/myext.so"},  # entrypoint derived from the filename
    ],
)
await db.ready()

# The extension's functions are now callable in queries:
rows = await db.query("SELECT vec_version() AS v")

dirsql enables extension loading only while loading the configured libraries, then disables it again, so the SQL load_extension() function is never exposed to your queries. Programmatic entries load first, followed by any [[dirsql.extension]] entries declared in a config file. See the config reference.

Watching for changes

db.watch() returns an async iterator of row-level change events as files change on disk:

async for event in db.watch():
    print(f"{event.action} on {event.table}: {event.row}")
    if event.action == "error":
        print(f"  error: {event.error}")

Each event has .action ("insert", "update", "delete", or "error"), .table, .row (the new row, or the deleted row on delete), .old_row (the previous row, on update), .file_path, and .error (on error).

CLI

pip install dirsql also installs a dirsql console script that runs an HTTP server exposing the SDK over HTTP: POST /query for SQL and GET /events for a Server-Sent Events change stream. Run dirsql (or uvx dirsql) to start it. See the CLI reference.

License

MIT

Project details


Release history Release notifications | RSS feed

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.3.83.tar.gz (273.4 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.3.83-cp311-abi3-win_amd64.whl (5.2 MB view details)

Uploaded CPython 3.11+Windows x86-64

dirsql-0.3.83-cp311-abi3-manylinux_2_39_x86_64.whl (6.4 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.39+ x86-64

dirsql-0.3.83-cp311-abi3-manylinux_2_39_aarch64.whl (6.2 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.39+ ARM64

dirsql-0.3.83-cp311-abi3-macosx_11_0_arm64.whl (5.5 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

dirsql-0.3.83-cp311-abi3-macosx_10_12_x86_64.whl (5.7 MB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file dirsql-0.3.83.tar.gz.

File metadata

  • Download URL: dirsql-0.3.83.tar.gz
  • Upload date:
  • Size: 273.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dirsql-0.3.83.tar.gz
Algorithm Hash digest
SHA256 ceffe33d5ccfc96848d36da62f235c5d1cb02772a2bf0db127b026b4840c6504
MD5 24828bdd330336028a5a29eb8ab2bf6d
BLAKE2b-256 63d0ba47a8f171e55de38b332b584938260784f139f8adc322eb39b1a1c2ccba

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83.tar.gz:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirsql-0.3.83-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: dirsql-0.3.83-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.2 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.3.83-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a9f744db9c65e0c62f7e7cb7d999272e4038711ca86f9a858b6386761cdd6c9c
MD5 2349a0f2b7d20ba84585205034796732
BLAKE2b-256 4776aaee5f6834f08494ff53b832ffa29ebf73d7eabf327f0acd87b2c44f626f

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83-cp311-abi3-win_amd64.whl:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirsql-0.3.83-cp311-abi3-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.3.83-cp311-abi3-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 b4c13c713da515eb0732e4a20f3a6719704f93c77bf4e8021683382b7edfb74c
MD5 f5e1f120816484ef2cf8cc4297c46c30
BLAKE2b-256 3204daa104dbe5f7d364863b970cb3c076034480fc4693a4cd721574e2f525ff

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83-cp311-abi3-manylinux_2_39_x86_64.whl:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirsql-0.3.83-cp311-abi3-manylinux_2_39_aarch64.whl.

File metadata

File hashes

Hashes for dirsql-0.3.83-cp311-abi3-manylinux_2_39_aarch64.whl
Algorithm Hash digest
SHA256 2be7cfb26cb563958144487058648e23209c1d5a1b7792ba4345db588b32b3f9
MD5 d29076daaa535c979e65f46ce7998056
BLAKE2b-256 13133909bc4da95c7c12cf6de2a87c7647c5346dfb0a94dcbf3e0440f21fbb7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83-cp311-abi3-manylinux_2_39_aarch64.whl:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirsql-0.3.83-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dirsql-0.3.83-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c09640533d9779c1880e01cc8495e3d0560e2fd98e663e18072f4764d7332159
MD5 6e3cdea2e5344da0a726a553fa6fc93e
BLAKE2b-256 ffc277c3215e8ba6a8b9d38fdab825c9a502b594c54a6ca4b7f05f432924bb8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83-cp311-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dirsql-0.3.83-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dirsql-0.3.83-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 31e6e4b2d29494bcb9fe475f3f68f58a5992ce329d3ea8f8d2ddd98c47c1173f
MD5 eb0c7eae6b8d4d6d4e91acdd31a825df
BLAKE2b-256 fec2262f6c5a9458c84a81d3e6c292a75293fb30882199bf2ff30ed74c3c7658

See more details on using hashes here.

Provenance

The following attestation bundles were made for dirsql-0.3.83-cp311-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on thekevinscott/dirsql

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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