Skip to content

Commit

Permalink
SQLite storage is added. Msgspec is supported. Documentation is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
stasdavydov committed Jul 5, 2024
1 parent 71602c3 commit 456b273
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python Object Storage

Simple JSON file storage for Python dataclasses and Pydantic models, thread and multiprocess safe.
Simple fast JSON file storage for Python dataclasses and Pydantic models, thread and multiprocess safe.

----

Expand Down Expand Up @@ -119,8 +119,11 @@ assert for_kids in leo_books
```python
import pys

# Initialize storage
storage = pys.storage('.path-to-storage')
# Initialize default (file) storage
storage1 = pys.storage('.path-to-storage')

# Initialize SQLite storage
storage2 = pys.sqlite_storage('path-to-storage.db')

# Save a model with optional relation to other models
storage.save(model, [related_model | (RelatedModelClass, related_model_id), ...])
Expand All @@ -133,10 +136,17 @@ storage.delete(ModelClass, model_id, [related_model | (RelatedModelClass, relate

# List models by specified ModelClass with optional relation to other models
storage.list(ModelClass, [related_model | (RelatedModelClass, related_model_id), ...])

# Destroy storage
storage.destroy()
```

## Release Notes

### 0.0.4
SQLite storage is added.
Support of `msqspec` JSON and structures is added.

### 0.0.3
Benchmark is added, performance is improved.
Fixed dependency set up.
Expand Down
6 changes: 4 additions & 2 deletions pys/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ def _save(self, model: StoredModel, prev=Related):
prev_id,
prev_cls.__name__ if prev else None,),
)
return self.con.total_changes
return model.__my_id__()

def save(self, model: StoredModel, *related_model: Related) -> Any:
prev_model = None
last_id = None
for m in related_model + (model,):
self._save(m, prev=prev_model)
last_id = self._save(m, prev=prev_model)
prev_model = m
return last_id

def delete(self, model_class: Type[StoredModel], model_id: str, *related_model: Related) -> None:
last_related = related_model[-1] if related_model else None
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
README = (HERE / "README.md").read_text()

setup(name='pysdato',
version='0.0.3',
version='0.0.4',
description='Simple JSON file storage for Python dataclasses and pydantic models, thread and multiprocess safe',
long_description=README,
long_description_content_type="text/markdown",
Expand Down
21 changes: 21 additions & 0 deletions tests/test_sample1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass

import msgspec

import pys
from pys.pydantic import ModelWithID

Expand Down Expand Up @@ -33,6 +35,25 @@ class Author:
# Persist model Author
leo = Author(name='Leo Tolstoy')
leo_id = storage.save(leo)
assert leo_id

# Load model Author by its ID and check it's the same
another_leo = storage.load(Author, leo_id)
assert another_leo.name == leo.name


def test_sample_msgspec():
# Initialize storage with path where files will be saved
storage = pys.sqlite_storage('storage.db')

@pys.saveable
class Author(msgspec.Struct):
id: str
name: str

# Persist model Author
leo = Author(id='leo', name='Leo Tolstoy')
leo_id = storage.save(leo)

# Load model Author by its ID and check it's the same
another_leo = storage.load(Author, leo_id)
Expand Down

0 comments on commit 456b273

Please sign in to comment.