-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update readme. Added ability to delete record. Fixed bugs around vers…
…ion tracking.
- Loading branch information
Showing
7 changed files
with
150 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# PynamoDB Single Table | ||
|
||
A [Pydantic](https://docs.pydantic.dev/latest/) ORM built on top of [PynamoDB](https://github.com/pynamodb/PynamoDB). | ||
|
||
[![PyPI](https://img.shields.io/pypi/v/pynamodb_single_table.svg)][pypi status] | ||
[![Status](https://img.shields.io/pypi/status/pynamodb_single_table.svg)][pypi status] | ||
[![Python Version](https://img.shields.io/pypi/pyversions/pynamodb_single_table)][pypi status] | ||
|
@@ -21,11 +23,73 @@ | |
|
||
## Features | ||
|
||
- TODO | ||
Provides a Django-inspired "Active Record"-style ORM using single-table design built on top of Django. | ||
|
||
```python | ||
import abc | ||
from datetime import datetime | ||
from pydantic import Field, EmailStr | ||
from pynamodb_single_table import SingleTableBaseModel | ||
|
||
class BaseTableModel(SingleTableBaseModel, abc.ABC): | ||
class _PynamodbMeta: | ||
table_name = "MyDynamoDBTable" | ||
|
||
class User(BaseTableModel): | ||
__table_name__ = "user" | ||
__str_id_field__ = "username" | ||
username: str | ||
email: EmailStr | ||
account_activated_on: datetime = Field(default_factory=datetime.now) | ||
|
||
# Make sure the table exists in DynamoDB | ||
BaseTableModel.ensure_table_exists(billing_mode="PAY_PER_REQUEST") | ||
|
||
# Create a record | ||
john, was_created = User.get_or_create(username="john_doe", email="[email protected]") | ||
assert was_created | ||
|
||
# Retrieve | ||
john_again = User.get_by_str("john_doe") | ||
assert john_again.email == "[email protected]" | ||
|
||
# Update | ||
now = datetime.now() | ||
john_again.account_activated_on = now | ||
john_again.save() | ||
|
||
assert User.get_by_str("john_doe").account_activated_on == now | ||
|
||
# Delete | ||
john_again.delete() | ||
``` | ||
|
||
## Motivation | ||
|
||
Many use cases need little more than structured CRUD operations with a table-like design (e.g., for storing users and groups), but figuring out how to host that efficiently in the cloud can be a pain. | ||
|
||
DynamoDB is awesome for CRUD when you have clean keys. | ||
It's a truly serverless NoSQL database, including nice features like: | ||
- High performance CRUD operations when you know your primary keys | ||
- Scale-to-zero usage-based pricing available | ||
- Official local testing capability | ||
- Conditional CRUD operations to avoid race conditions | ||
- Multiple methods of indexing into data | ||
- Scalable with reliable performance | ||
|
||
This project, in part, emerges from my frustration with the lack of many truly serverless SQL database services. | ||
By "truly serverless", I mean purely usage-based pricing (generally a combination of storage costs and query costs). | ||
Many small, startup applications use trivial amounts of query throughput and story trivial amounts of data, but finding a way to deploy such an application into the cloud without shelling out $10-$100's per month is tricky. | ||
In AWS, now that Aurora Serverless V1 is being replaced, there is _no_ way to do this. | ||
|
||
However, DynamoDB provides not just the basic functionality needed to do this, it's actually a really good option if your data usage patterns can fit within its constraints. | ||
That means, primarily, that you can always do key-based lookups, and that you can avoid changing your indexing strategy or database schema too much (e.g. modifying a table from having nullable columns into non-nullable). | ||
DynamoDB _can_ do custom queries at tolerable rates, but you're going to get sub-par speed and cost efficiency if you're regularly doing searches across entire tables instead of direct hash key lookups. | ||
|
||
## Requirements | ||
|
||
- TODO | ||
This project is built on the backs of Pydantic and Pynamodb. | ||
I am incredibly grateful to the developers and communities of both of those projects. | ||
|
||
## Installation | ||
|
||
|
@@ -35,10 +99,6 @@ You can install _PynamoDB Single Table_ via [pip] from [PyPI]: | |
$ pip install pynamodb_single_table | ||
``` | ||
|
||
## Usage | ||
|
||
Please see the [Command-line Reference] for details. | ||
|
||
## Contributing | ||
|
||
Contributions are very welcome. | ||
|
@@ -68,4 +128,3 @@ This project was generated from [@cjolowicz]'s [Hypermodern Python Cookiecutter] | |
|
||
[license]: https://github.com/scnerd/pynamodb_single_table/blob/main/LICENSE | ||
[contributor guide]: https://github.com/scnerd/pynamodb_single_table/blob/main/CONTRIBUTING.md | ||
[command-line reference]: https://pynamodb_single_table.readthedocs.io/en/latest/usage.html |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pynamodb_single_table" | ||
version = "0.1.0" | ||
version = "0.1.1" | ||
description = "PynamoDB Single Table" | ||
authors = ["David Maxson <[email protected]>"] | ||
license = "MIT" | ||
|
@@ -45,6 +45,7 @@ sphinx = ">=4.3.2" | |
sphinx-autobuild = ">=2021.3.14" | ||
#sphinx-click = ">=3.0.2" | ||
myst-parser = {version = ">=0.16.1"} | ||
pydantic = {extras = ["email"], version = "*"} | ||
|
||
[tool.poetry.group.constraints.dependencies] | ||
urllib3 = "<1.27" | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
"""PynamoDB Single Table.""" | ||
|
||
from .base import SingleTableBaseModel | ||
|
||
__all__ = ("SingleTableBaseModel",) |
This file contains 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
This file contains 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