Last active
June 12, 2021 13:59
-
-
Save cassioeskelsen/bf3aaf6576ddf198e4caf8853a0716a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| from pyravendb.store import document_store | |
| @dataclass(eq=True) | |
| class State: | |
| name: str | |
| Id: str = None # actually, state abbreviation | |
| def __hash__(self) -> int: | |
| return hash(self.Id) | |
| @dataclass(eq=True, unsafe_hash=True) | |
| class Address: | |
| address: str | |
| complement: str | |
| zip_code: int | |
| city: str | |
| state: State | |
| @dataclass(eq=True) | |
| class Customer: | |
| name: str | |
| address: Address | |
| document: str | |
| Id: str = None | |
| def __hash__(self) -> int: | |
| return hash(self.Id) | |
| def create_instance() -> Customer: | |
| sc = State('SC', 'Santa Catarina') | |
| addr = Address('Rua do Python, 8', 'Primeiro Andar', 89000000, 'Pytholandia', sc) | |
| customer = Customer('Guido Ayende', addr, '8345004') | |
| return customer | |
| if __name__ == "__main__": | |
| store = document_store.DocumentStore(urls=['http://localhost:8080'], database="python") | |
| store.initialize() | |
| customer = create_instance() | |
| with store.open_session() as session: | |
| session.store(customer) | |
| session.save_changes() | |
| with store.open_session() as session: | |
| c = session.load(customer.Id, Customer, nested_object_types={'address': Address}) | |
| print(c) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment