flask-dynamo
flask-dynamo copied to clipboard
Bug in documentation
The example in the documentation contains this:
app.config['DYNAMO_TABLES'] = [
{
TableName='users',
KeySchema=[dict(AttributeName='username', KeyType='HASH')],
AttributeDefinitions=[dict(AttributeName='username', AttributeType='S')],
ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
}, {
TableName='groups',
KeySchema=[dict(AttributeName='name', KeyType='HASH')],
AttributeDefinitions=[dict(AttributeName='name', AttributeType='S')],
ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
}
]
Which is invalid syntax, because the keys in the dict are not quoted. A quick fix is to change the braces to dict( ... )
Also, the example endpoint:
@app.route('/create_user')
def create_user():
dynamo.tables['users'].put_item(data={
'username': 'rdegges',
'first_name': 'Randall',
'last_name': 'Degges',
'email': '[email protected]',
})
...does not work, because the parameter for put_item()
should be Item
instead of data
, and the view should return some valid response instead of None
, for example return ""
, or better jsonify the value returned by put_item()
.
Although simple to fix, I find these issues worrying, because they are at least 2 years old, and apparently nobody noticed them yet. Is the library maintained?
@jldiaz can you show the right code?
@edusantana Yes, of course:
# app.py
from flask import Flask, jsonify
from flask_dynamo import Dynamo
app = Flask(__name__)
app.config['DYNAMO_TABLES'] = [
dict(
TableName='users',
KeySchema=[dict(AttributeName='username', KeyType='HASH')],
AttributeDefinitions=[dict(AttributeName='username', AttributeType='S')],
ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
),
dict(
TableName='groups',
KeySchema=[dict(AttributeName='name', KeyType='HASH')],
AttributeDefinitions=[dict(AttributeName='name', AttributeType='S')],
ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
)
]
# ...
@app.route('/create_user')
def create_user():
r = dynamo.tables['users'].put_item(Item={
'username': 'rdegges',
'first_name': 'Randall',
'last_name': 'Degges',
'email': '[email protected]',
})
return jsonify(r)
@jldiaz Just looking today, I was wondering the same. That said, thanks for sharing your implementation.
So strange that this hasn't been merged into the docs.
C'mon @rdegges , fix the doc 🔨
⚠️ For app factory approach users there is another typo in the doc: Flask.current_app.extensions['dynamodb']
should be flask.current_app.extensions['dynamo']
I've opened up a PR for this here: https://github.com/rdegges/flask-dynamo/pull/44
+1. Bumped into this now and updated docs would have helped.
Also the put_item
needs to be Item=
app.extensions['dynamo'].tables['table_name'].put_item(Item=data)