Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2.4 KiB
2.4 KiB
minidb
A tiny in-process table store with JSON persistence. Pure Python 3, standard library only.
Library
from minidb import Column, Database, JsonFileStorage
db = Database(JsonFileStorage("people.json")) # or Database() for an in-memory database
people = db.create_table("people", [
Column("name", "str", nullable=False),
Column("age", "int"),
Column("city", "str"),
])
person_id = people.insert(name="Ann", age=31, city="Berlin")
db.save()
people.select(where=lambda row: row["age"] is not None and row["age"] > 30, order_by="name", limit=10)
people.update({"city": "Rome"}, where=lambda row: row["city"] == "Berlin")
people.delete(where=lambda row: row["age"] is None)
people.count()
- Column types are
int,float,strandbool. Every table has an integeridcolumn that is assigned automatically. select(where=None, columns=None, order_by=None, descending=False, limit=None)returns copies of the rows as dictionaries, in insertion order unlessorder_byis given. Rows with NULL in theorder_bycolumn come last.whereis a callable that receives the row dictionary;Nonemeans every row.Table.create_index(column)adds a hash index that speeds upTable.find(column, value).- Column names must be identifiers and must not be one of the reserved words
AND OR NOT IN BETWEEN LIKE ILIKE IS NULL TRUE FALSE.
Command line
python3 -m minidb.cli --db people.json create people name:str:required age:int city:str
python3 -m minidb.cli --db people.json insert people name=Ann age=31 city=Berlin
python3 -m minidb.cli --db people.json select people --columns id,name --order-by age --desc --limit 5
python3 -m minidb.cli --db people.json update people --id 1 city=Rome
python3 -m minidb.cli --db people.json delete people --id 1
python3 -m minidb.cli --db people.json import-csv people people.csv
python3 -m minidb.cli --db people.json export-csv people out.csv
selectprints CSV (header line, then one line per row; NULL is an empty field, booleans aretrue/false).column=with nothing after the equals sign sets NULL.updateanddeleteprintupdated N/deleted N.- Errors print
error: ...on standard error and the exit status is 2.
Tests
python3 -m unittest discover -s tests
Query language
The textual where language is specified in SPEC.md and is not implemented yet.