Files
leleka/scripts/e2e/codex/tasks/feature-query/seed/README.md
T

58 lines
2.4 KiB
Markdown

# minidb
A tiny in-process table store with JSON persistence. Pure Python 3, standard library only.
## Library
```python
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`, `str` and `bool`. Every table has an integer `id` column 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
unless `order_by` is given. Rows with NULL in the `order_by` column come last.
* `where` is a callable that receives the row dictionary; `None` means every row.
* `Table.create_index(column)` adds a hash index that speeds up `Table.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
```text
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
```
* `select` prints CSV (header line, then one line per row; NULL is an empty field, booleans are `true` / `false`).
* `column=` with nothing after the equals sign sets NULL.
* `update` and `delete` print `updated N` / `deleted N`.
* Errors print `error: ...` on standard error and the exit status is 2.
## Tests
```text
python3 -m unittest discover -s tests
```
## Query language
The textual `where` language is specified in `SPEC.md` and is not implemented yet.