Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regions as attributes #9

Closed
JohnCEarls opened this issue Mar 26, 2014 · 5 comments
Closed

Regions as attributes #9

JohnCEarls opened this issue Mar 26, 2014 · 5 comments

Comments

@JohnCEarls
Copy link

I like the idea of adding the region to the Model, but if you are already using region as an attribute (say because you use the table to track aws resources), you get an error (dynamodb not available in this region).

Not sure if this is solvable other than by A. 'region' is not an allowed attribute name or B. change the way the table regions are set/stored. In any case, this is an issue I figured you should be aware of.

Thanks for all your hard work.

@jlafon
Copy link
Contributor

jlafon commented Mar 26, 2014

Hi John,

I agree that something should be done to avoid attribute name collisions, and I will leave this issue open for a while to let people comment on it before deciding how to fix it.

My goal is to keep the Model API simple and Pythonic. With that in mind, here are two possible solutions.

  • A nested class for configuration. This is how Django and Django-Tastypie do it.
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute


class UserModel(Model):
    """A user model class"""
    class Meta:
        """A class for settings"""
        table_name = "users"
        region = "us-east-1"
    user_name = UnicodeAttribute(hash_key=True)
  • Make setters for region (and other table level settings).
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute


class UserModel(Model):
    """A user model class"""
    table_name = "users"
    user_name = UnicodeAttribute(hash_key=True)

# This would have to be done before using UserModel
UserModel.set_region('us-east-1')

@adamchainz
Copy link
Contributor

Hi,

I'm in favour of the Meta class, for similarity with the most popular ORM. But what about __ vars, like __region__ ? A lot of other ORM libraries use this pattern.

On this note, how is one supposed to use a PynamoDB Model with DynamoDBLocal? There's nowhere to set the endpoint url, which would be another Meta var...

@jlafon
Copy link
Contributor

jlafon commented Mar 26, 2014

@adamchainz , using __region__ style variable names is not a bad idea. Although, I do prefer Meta because it cleanly encapsulates all of the configuration values.

There are two ways to set the endpoint URL for now:

from pynamodb.models import Model
from pynamodb.connection import TableConnection

class TestModel(Model):
    connection = TableConnection('table-name', host='http://localhost')

or

from pynamodb.models import Model
from pynamodb.connection import TableConnection

class TestModel(Model):
    # normal attributes here

TestModel.connection = TableConnection(TestModel.table_name, host='http://localhost')

If we change the API to include a Meta class, it could be like this:

from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute


class UserModel(Model):
    """A user model class"""
    class Meta:
        """A class for settings"""
        table_name = "users"
        region = "us-east-1"
        host = 'http://localhost'
    user_name = UnicodeAttribute(hash_key=True)

@adamchainz
Copy link
Contributor

👍 for Meta and encapsulating all the configuration in one place.

@jlafon
Copy link
Contributor

jlafon commented Mar 26, 2014

I implemented the Meta class idea on a branch, you can see how it looks here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants