JSON Directory Repository
Contents
JSON Directory Repository#
JSON (JavaScript Object Notation) directory repository is a data store
in which each item represents a JSON file in a directory. The stems of
the files are the IDs specified in the id_field
of the repository.
from redbird.repos import JSONDirectoryRepo
repo = JSONDirectoryRepo(path="path/to/repo", id_field='id')
With Pydantic model:
from pydantic import BaseModel
from redbird.repos import JSONDirectoryRepo
class MyItem(BaseModel):
id: str
name: str
age: int
repo = JSONDirectoryRepo(path="path/to/repo", model=MyItem, id_field='id')
Note
The id_field
must be specified as the name of the JSON file
depends on the value of this field.
Warning
The order in which the items are stored depends on the operating system thus the order in which the items are returned when reading the repository is not guaranteed to be fixed.
Usage#
Now you may use the repository the same way as any other repository. Please see:
Class#
- class redbird.repos.JSONDirectoryRepo(*, model=<class 'dict'>, id_field, query_model=<class 'redbird.base.BasicQuery'>, errors_query='raise', field_access='infer', ordered=False, path, field_names=None, kwds_json_load={}, kwds_json_dump={})#
JSON directory repository
This repository represents a directory which contains JSON files. Each item represents a file and the
id_field
is the stem of the file.- Parameters
path (Path-like) – Path to the repository directory
model (Type) – Class of an item in the repository. Commonly dict or subclass of Pydantic BaseModel. By default dict
id_field (str) – Attribute or key that identifies each item in the repository.
field_access ({'attr', 'key'}, optional) – How to access a field in an item. Either by attribute (‘attr’) or key (‘item’). By default guessed from the model.
query (Type, optional) – Query model of the repository.
errors_query ({'raise', 'warn', 'discard'}) – Whether to raise an exception, warn or discard the item in case of validation error in converting data to the item model from the repository. By default raise
kwds_json_load (dict) – Keyword arguments passed to
json.load
.kwds_json_dump (dict) – Keyword arguments passed to
json.dump
. Useful for prettifying the files
Examples
repo = JSONDirectoryRepo(path="path/to/repo", id_field="id")
- insert(item)#
Insert item to the repository
- Parameters
item (instance of model) – Item to insert to the repository
Examples
repo.insert(Item(id="a", color="red"))
- filter_by(**kwargs)#
Filter the repository
- Parameters
**kwargs (dict) – Query which is used to conduct furher operation.
Examples
repo.filter_by(color="red")
- update(item)#
Update item in the repository
- Parameters
item (instance of model) – Item to update in the repository
Examples
repo.update(Item(id="a", color="red"))
- delete(item)#
Delete item from the repository
- Parameters
item (instance of model) – Item to delete from the repository
Examples
repo.delete(Item(id="a", color="red"))