Skip to main content

Pydastic is an elasticsearch python ORM based on Pydantic.

Project description

Pydastic

Package version

build Python Version Dependencies Status Code style: black

Security: bandit Pre-commit Semantic Versions License Coverage Report

Pydastic is an elasticsearch python ORM based on Pydantic.

💾 Installation

Pip:

pip install pydastic

Poetry:

poetry add pydastic

🚀 Features

  • Simple CRUD operations supported
  • Dynamic index support when committing operation

📋 Usage

Defining Models

from pydastic import ESModel

class User(ESModel):
    name: str
    phone: Optional[str]
    last_login: datetime = Field(default_factory=datetime.now)

    class Meta:
        index = "user"

Establishing Connection

An elasticsearch connection can be setup by using the connect function. This function adopts the same signature as the elasticsearch.Elasticsearch client and supports editor autocomplete. Make sure to call this only once. No protection is put in place against multiple calls, might affect performance negatively.

from pydastic import connect

connect(hosts="localhost:9200")

CRUD: Create, Update

# Create and save doc
user = User(name="John", age=20)
user.save(wait_for=True)
assert user.id != None

# Update doc
user.name = "Sam"
user.save(wait_for=True)

CRUD: Read Document

got = User.get(id=user.id)
assert got == user

CRUD: Delete

user = User(name="Marie")
user.save(wait_for=True)

user.delete(wait_for=True)

Dynamic Index Support

Pydastic also supports dynamic index specification. The model Metaclass index definition is still mandatory, but if an index is specified when performing operations, that will be used instead. The model Metaclass index is technically a fallback, although most users will probably be using a single index per model. For some users, multiple indices per model are needed (for example one user index per company).

user = User(name="Marie")
user.save(index="my-user", wait_for=True)

user.delete(index="my-user", wait_for=True)

Support Elasticsearch Versions

Part of the build flow is running the tests using elasticsearch 7.12.0 DB as well as python client, and using 8.1.2 as well (DB as well as client, as part of a build matrix). This ensures support for multiple versions.

📈 Releases

None yet.

You can see the list of available releases on the GitHub Releases page.

We follow Semantic Versions specification.

We use Release Drafter. As pull requests are merged, a draft release is kept up-to-date listing the changes, ready to publish when you’re ready. With the categories option, you can categorize pull requests in release notes using labels.

🛡 License

License

This project is licensed under the terms of the MIT license. See LICENSE for more details.

📃 Citation

@misc{pydastic,
  author = {Rami Awar},
  title = {Pydastic is an elasticsearch python ORM based on Pydantic.},
  year = {2022},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/ramiawar/pydastic}}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydastic-0.3.0.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

pydastic-0.3.0-py3-none-any.whl (7.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page