# `easydatastore` - Create type-safe, in-memory datastores
## Requirements
* Python 3.11+
## Install
```bash
pip install easydatastore
```
## Usage
If you want to prototype a web application quickly and just need a way to validate your mock data, you can use `easydatastore` to create an in-memory datastore for your models, directly out of the box.
### Features:
* In-memory tables for your models with `easydatastore.Table`, with a familiar pydantic-flavored syntax and ORM-like API.
* Enforce uniqueness constraints with `Column(unique=True)`
* Use indexing for faster lookups with `Column(index=True)`
* IDE-friendly type hints ensure your coding experience is type-safe.
### Example
```python
import easydatastore.exceptions
from easydatastore import Column, Table
class Person(Table):
name: str = Column(primary_key=True)
email: str | None = Column(unique=True, default=None)
family_name: str
age: int
Person(name="Snap", family_name="Krispies", email="snap@example.com", age=92)
Person(name="Crackle", family_name="Krispies", age=92)
Person(name="Pop", family_name="Krispies", age=92)
Person(name="Tony", family_name="Tiger", email="tony@example.com", age=72)
Person(name="Cap'n", family_name="Crunch", age=53)
# retrieve an instance from the table using the .get() method and a primary key value
tony = Person.get("Tony")
print(tony) # Person(name='Tony', email=None, family_name='Tiger')
# or query your table using the .filter() method
print([person.name for person in Person.filter(family_name="Krispies")]) # ["Snap", "Crackle", "Pop"]
print([person.name for person in Person.filter(lambda person: person.age < 90)]) # ["Tony", "Cap'n"]
# delete instances from the table with the .delete() method
Person.delete(Person.get("Crackle"))
print([person.name for person in Person.all()]) # ["Snap", "Pop", "Tony"]
# easydatastore will validate uniqueness for you... these operations will raise exceptions:
try:
tony.email = Person.get("Snap").email
except ValueError as e:
print(e.args[0]) # ValueError: Value 'snap@example.com' for field 'email' is not unique
try:
Person(name="Snap", family_name="Rice", age=1)
except easydatastore.exceptions.DuplicateUniqueFieldValueError as e:
print(e.args[0]) # DuplicateUniqueFieldValueError
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jicruz96/easydatastore",
"name": "easydatastore",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.11",
"maintainer_email": null,
"keywords": "data validation, dataclasses, validation, models, meta-programming",
"author": "J.I. Cruz",
"author_email": "hello@jicruz.com",
"download_url": "https://files.pythonhosted.org/packages/f3/bd/f9ed375dfd99c92e7e2e703a813f3c74b634d2d16244ff06f9204677d57a/easydatastore-0.1.7.tar.gz",
"platform": null,
"description": "\n# `easydatastore` - Create type-safe, in-memory datastores \n\n## Requirements\n* Python 3.11+\n\n## Install\n\n```bash\npip install easydatastore\n```\n\n## Usage\n\nIf you want to prototype a web application quickly and just need a way to validate your mock data, you can use `easydatastore` to create an in-memory datastore for your models, directly out of the box.\n\n### Features:\n\n* In-memory tables for your models with `easydatastore.Table`, with a familiar pydantic-flavored syntax and ORM-like API.\n* Enforce uniqueness constraints with `Column(unique=True)`\n* Use indexing for faster lookups with `Column(index=True)`\n* IDE-friendly type hints ensure your coding experience is type-safe.\n\n### Example\n\n```python\nimport easydatastore.exceptions\nfrom easydatastore import Column, Table\n\n\nclass Person(Table):\n name: str = Column(primary_key=True)\n email: str | None = Column(unique=True, default=None)\n family_name: str\n age: int\n\n\nPerson(name=\"Snap\", family_name=\"Krispies\", email=\"snap@example.com\", age=92)\nPerson(name=\"Crackle\", family_name=\"Krispies\", age=92)\nPerson(name=\"Pop\", family_name=\"Krispies\", age=92)\nPerson(name=\"Tony\", family_name=\"Tiger\", email=\"tony@example.com\", age=72)\nPerson(name=\"Cap'n\", family_name=\"Crunch\", age=53)\n\n# retrieve an instance from the table using the .get() method and a primary key value\ntony = Person.get(\"Tony\")\nprint(tony) # Person(name='Tony', email=None, family_name='Tiger')\n\n# or query your table using the .filter() method\nprint([person.name for person in Person.filter(family_name=\"Krispies\")]) # [\"Snap\", \"Crackle\", \"Pop\"]\nprint([person.name for person in Person.filter(lambda person: person.age < 90)]) # [\"Tony\", \"Cap'n\"]\n\n# delete instances from the table with the .delete() method\nPerson.delete(Person.get(\"Crackle\"))\nprint([person.name for person in Person.all()]) # [\"Snap\", \"Pop\", \"Tony\"]\n\n# easydatastore will validate uniqueness for you... these operations will raise exceptions:\ntry:\n tony.email = Person.get(\"Snap\").email\nexcept ValueError as e:\n print(e.args[0]) # ValueError: Value 'snap@example.com' for field 'email' is not unique\n\ntry:\n Person(name=\"Snap\", family_name=\"Rice\", age=1)\nexcept easydatastore.exceptions.DuplicateUniqueFieldValueError as e:\n print(e.args[0]) # DuplicateUniqueFieldValueError\n\n```\n",
"bugtrack_url": null,
"license": null,
"summary": "The easiest way to create type-safe, in-memory datastores for your models.",
"version": "0.1.7",
"project_urls": {
"Homepage": "https://github.com/jicruz96/easydatastore",
"Repository": "https://github.com/jicruz96/easydatastore"
},
"split_keywords": [
"data validation",
" dataclasses",
" validation",
" models",
" meta-programming"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "db87c41654b2974d2e29257cc6dea0616ec64cc9cea27ace92ce90343c7009de",
"md5": "28ad767f43fc7b9898f80b8c86a062d8",
"sha256": "72720bf0d661b8150df05676d40b34cad24223463eb02b2d59b5037a863ba348"
},
"downloads": -1,
"filename": "easydatastore-0.1.7-py3-none-any.whl",
"has_sig": false,
"md5_digest": "28ad767f43fc7b9898f80b8c86a062d8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.11",
"size": 11563,
"upload_time": "2024-07-19T22:09:09",
"upload_time_iso_8601": "2024-07-19T22:09:09.946303Z",
"url": "https://files.pythonhosted.org/packages/db/87/c41654b2974d2e29257cc6dea0616ec64cc9cea27ace92ce90343c7009de/easydatastore-0.1.7-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f3bdf9ed375dfd99c92e7e2e703a813f3c74b634d2d16244ff06f9204677d57a",
"md5": "25dcd4f0cbb157397f01e6bd71189e5f",
"sha256": "6aeaa59eab51b222fea3ac2c0a9f96c0161cf65bf3ec287ad99fea16794eeacd"
},
"downloads": -1,
"filename": "easydatastore-0.1.7.tar.gz",
"has_sig": false,
"md5_digest": "25dcd4f0cbb157397f01e6bd71189e5f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.11",
"size": 8889,
"upload_time": "2024-07-19T22:09:10",
"upload_time_iso_8601": "2024-07-19T22:09:10.843048Z",
"url": "https://files.pythonhosted.org/packages/f3/bd/f9ed375dfd99c92e7e2e703a813f3c74b634d2d16244ff06f9204677d57a/easydatastore-0.1.7.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-19 22:09:10",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jicruz96",
"github_project": "easydatastore",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "easydatastore"
}