Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
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
where may also be a string in the query language specified in SPEC.md. It is available from Table.select, Table.update,
Table.delete and Table.count, from minidb.query.compile_where, and as --where on the command line.
people.select(where="age >= 18 AND city IN ('Berlin', 'Rome') AND NOT name LIKE 'A%'")
people.update({"city": "Oslo"}, where="note IS NULL")
people.count(where="age BETWEEN 20 AND 30")
python3 -m minidb.cli --db people.json select people --where "age > 30 OR city = 'Rome'" --order-by age
python3 -m minidb.cli --db people.json update people --where "city IS NULL" city=Unknown
python3 -m minidb.cli --db people.json delete people --where "age < 18"
- Operators:
= != <> < <= > >=,IN (...),NOT IN,BETWEEN a AND b,LIKE/ILIKEwith%and_,IS NULL,IS NOT NULL. - Precedence:
NOTbinds tighter thanAND, which binds tighter thanOR; parentheses group. - Literals: integers, floats,
TRUE/FALSE,NULLand single-quoted strings (''for a quote). Identifiers are column names. - Comparisons with NULL are unknown and unknown rows are not selected; use
IS NULL. - Comparing a string with a number raises
QueryTypeErrorwhen the first offending row is evaluated; an unknown column raisesQueryNameErrorand a malformed expressionQuerySyntaxError(with the 0-basedposition) before any row is examined. updateanddeleteon the command line take either--id Nor--where EXPR.