https://github.com/labmlai/db
Minimalistic Object-Relational Mapper for JSON/YAML/Pickle file based db
https://github.com/labmlai/db
database json minimal orm pickle python yaml
Last synced: 3 months ago
JSON representation
Minimalistic Object-Relational Mapper for JSON/YAML/Pickle file based db
- Host: GitHub
- URL: https://github.com/labmlai/db
- Owner: labmlai
- License: mit
- Created: 2020-10-16T06:00:42.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-14T08:26:26.000Z (almost 2 years ago)
- Last Synced: 2025-06-06T12:08:20.407Z (4 months ago)
- Topics: database, json, minimal, orm, pickle, python, yaml
- Language: Python
- Homepage:
- Size: 73.2 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: readme.rst
- License: license
Awesome Lists containing this project
README
.. image:: https://badge.fury.io/py/labml-db.svg
:target: https://badge.fury.io/py/labml-db
.. image:: https://pepy.tech/badge/labml-db
:target: https://pepy.tech/project/labml-dbLabML DB
========LabML DB is a minimalistic ORM database that uses JSON, YAML or Pickle files.
It uses Python typehints as much as possible to help with static checking,
and IDE features like autocompletion.You can install this package using PIP.
.. code-block:: console
pip install labml_db
Example
^^^^^^^.. code-block:: python
from labml_db import Model, Index
class Project(Model['Project']):
name: str
experiments: int@classmethod
def defaults(cls):
return dict(name='', experiments=0)class User(Model['User']):
name: str
projects: List[Key[Project]]
# This is an optional property, will automatically default to None
occupation: Optional[str]@classmethod
def defaults(cls):
# Name is not in defaults and not optional.
# It will be considered a required property
return dict(projects=[])class UsernameIndex(Index['User']):
passYou can configure it to use JSON/YAML/Pickle files
.. code-block:: python
Model.set_db_drivers([
FileDbDriver(PickleSerializer(), User, Path('./data/user')),
FileDbDriver(YamlSerializer(), Project, Path('./data/project'))
])
Index.set_db_drivers([
FileIndexDbDriver(JsonSerializer(), UsernameIndex, Path('./data/UserNameIndex.yaml'))
])You can user `get_all` and `Key.load` to retrieve models, and `save` to save models.
.. code-block:: python
proj = Project(name='nlp')
user = User(name='John')
user.projects.append(proj.key)
user.occupation = 'test'
user.save()
proj.save()keys = User.get_all()
print([k.load() for k in keys])
keys = Project.get_all()
print([k.load() for k in keys])And index is a hash-map between strings and keys.
.. code-block:: python
user_key = UsernameIndex.get('John')
if not user_key:
user = User(name='John')
user.save()
UsernameIndex.set(user.name, user.key)
else:
print(user_key.load())