def init_app(self, local_dynamodb=None, prefix="", skip=False): name_template = prefix + "{table_name}" if local_dynamodb is not None: self._init_local(local_dynamodb, name_template) else: self.engine = Engine(table_name_template=name_template) self.engine.bind(self.Model, skip_table_setup=skip)
def engine(session, dynamodb, dynamodbstreams): # HACK: These clients won't be used. We're going to replace the session immediately. engine = Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams) # Toss the clients above and hook up the mock session engine.session = session engine.bind(BaseModel) return engine
def engine_for_region(region, table_name_template="{table_name}"): dynamodb = boto3.client("dynamodb", region_name=region) dynamodbstreams = boto3.client("dynamodbstreams", region_name=region) return Engine( dynamodb=dynamodb, dynamodbstreams=dynamodbstreams, table_name_template=table_name_template )
def _init_local(self, local_dynamodb="http://127.0.0.1:4444", name_template="{table_name}"): dynamodb = boto3.client("dynamodb", endpoint_url=local_dynamodb) dynamodbstreams = boto3.client("dynamodbstreams", endpoint_url=local_dynamodb) self.engine = Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams, table_name_template=name_template) client = patch_engine(self.engine) client.mock_ttl["MyTableName"] = True client.mock_backups["MyTableName"] = False
def engine(dynamodb, dynamodbstreams, request): engine = Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams, table_name_template="{table_name}" + request.config.getoption("--nonce")) yield engine # This collects all subclasses of BaseModel and are not abstract. We are trying to delete any data in # dynamodb-local between unit tests so we don't step on each other's toes. concrete = set( filter(lambda m: not m.Meta.abstract, walk_subclasses(BaseModel))) for model in concrete: # we can run into a situation where the class was created, but not bound in the engine (or table created), so # we only try. As the dynamodb-local process is only running in memory this isn't too much of a problem. try: objs = list(engine.scan(model)) if objs: engine.delete(*objs) except BloopException: pass
stage = args[1] os.environ['STAGE'] = stage config_filename = 'config.' + stage + '.json' parent_dir = os.path.dirname( os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) config_filepath = os.path.join(parent_dir, config_filename) with open(config_filepath, 'r') as fp: config = json.load(fp) region = config['REGION'] client = boto3.client('dynamodb', region_name=region) engine = Engine(dynamodb=client) model = Deployment # pull all items from deployment table table = model.Meta.table_name print("Updating brewoptix-deployment with new schema") print("Getting all items in table") all_items = client.scan(TableName=table) # delete existing table print("Delete table") resp = client.delete_table(TableName=table) for _ in range(10):
if len(args) >= 2: stage = args[1] config_filename = 'config.' + stage + '.json' parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) config_filepath = os.path.join(parent_dir, config_filename) with open(config_filepath, 'r') as fp: config = json.load(fp) region = config['REGION'] try: endpoint = args[2] client = boto3.client('dynamodb', region_name=region, endpoint_url=endpoint) engine = patch_engine(Engine(dynamodb=client)) except IndexError: client = boto3.client('dynamodb', region_name=region) engine = Engine(dynamodb=client) resp = client.list_tables() tables = resp['TableNames'] for model in models: model.Meta.table_name = model.Meta.table_name.format(STAGE=stage) print('Running dynamodb tables creation script ' 'in Region: {REGION}'.format(REGION=region)) print("Dynamodb Tables: ") for model in models: if model.Meta.table_name not in tables:
'Rating': Number, 'Updated': DateTime, 'Description': Map(**{ 'Title': String, 'Body': String }), 'Sellers': Set(Integer) }) class Item(BaseModel): id = Column(UUID, hash_key=True) data = Column(Product) engine = Engine() engine.bind(BaseModel) # ================================================ # Usage # ================================================ item = Item(id=uuid.uuid4()) item.data = { 'Name': 'item-name', 'Rating': decimal.Decimal(str(random.random())), 'Updated': datetime.now(timezone.utc), 'Description': { 'Title': 'item-title', 'Body': 'item-body', },
def engine(dynamodb, dynamodbstreams): return Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams)
def engine(dynamodb, dynamodbstreams): engine = Engine(dynamodb=dynamodb, dynamodbstreams=dynamodbstreams) yield engine engine.delete(*engine.scan(User))
from bloop import BaseModel, Column, String, Integer, Engine from bloop.exceptions import ConstraintViolation class NumberStore(BaseModel): key = Column(String, hash_key=True) value = Column(Integer) engine = Engine(table_name_template="my-memory-{table_name}") engine.bind(NumberStore) def get(key): try: return engine.query(NumberStore, key=NumberStore.key == key).one().value except ConstraintViolation: return None def set(key, value): update = NumberStore(key=key, value=value) engine.save(update)
import json from os import environ from uuid import uuid4, UUID import boto3 from bloop import Engine, ConstraintViolation from todoapi.models.todo import TodoItem if environ.get("AWS_SAM_LOCAL") == "true": dynamodb = boto3.client('dynamodb', endpoint_url="http://dynamodb:8000") db = Engine(dynamodb=dynamodb) db.bind(TodoItem) else: dynamodb = boto3.client('dynamodb') db = Engine(dynamodb=dynamodb) db.bind(TodoItem, skip_table_setup=True ) # we can skip the table setup because CloudFormation will do it. def get(event, context): if event['pathParameters']: todo_item = db.query(TodoItem, key=TodoItem.uuid == UUID( event['pathParameters']['todo_id'])).one() body = todo_item.as_dict else: todos = db.scan(TodoItem) body = {"todos": [todo_item.as_dict for todo_item in todos]} return {"body": json.dumps(body), "statusCode": 200}