1
1
.. _quickstart :
2
- .. module :: flask.ext.dynamo
2
+ .. module :: flask_dynamo
3
3
4
4
5
5
Quickstart
@@ -69,7 +69,7 @@ Below is an example::
69
69
# app.py
70
70
71
71
from flask import Flask
72
- from flask.ext.dynamo import Dynamo
72
+ from flask_dynamo import Dynamo
73
73
74
74
app = Flask(__name__)
75
75
app.config['DYNAMO_TABLES'] = [
@@ -104,7 +104,7 @@ All you need to do is pass your app to the ``Dynamo`` constructor::
104
104
# app.py
105
105
106
106
from flask import Flask
107
- from flask.ext.dynamo import Dynamo
107
+ from flask_dynamo import Dynamo
108
108
109
109
app = Flask(__name__)
110
110
app.config['DYNAMO_TABLES'] = [
@@ -123,8 +123,38 @@ All you need to do is pass your app to the ``Dynamo`` constructor::
123
123
124
124
dynamo = Dynamo(app)
125
125
126
+ If you use the app factory pattern then use::
127
+
128
+ # app.py
129
+
130
+ from flask import Flask
131
+ from flask_dynamo import Dynamo
132
+
133
+ def create_app():
134
+ app = Flask(__name__)
135
+ app.config['DYNAMO_TABLES'] = [
136
+ {
137
+ TableName='users',
138
+ KeySchema=[dict(AttributeName='username', KeyType='HASH')],
139
+ AttributeDefinitions=[dict(AttributeName='username', AttributeType='S')],
140
+ ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
141
+ }, {
142
+ TableName='groups',
143
+ KeySchema=[dict(AttributeName='name', KeyType='HASH')],
144
+ AttributeDefinitions=[dict(AttributeName='name', AttributeType='S')],
145
+ ProvisionedThroughput=dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
146
+ }
147
+ ]
148
+ dynamo = Dynamo()
149
+ dynamo.init_app(app)
150
+ return app
151
+
152
+ app = create_app()
153
+
154
+
126
155
From this point on, you can interact with DynamoDB through the global ``dynamo ``
127
- object.
156
+ object, or through ``Flask.current_app.extensions['dynamodb'] `` if you are
157
+ using the Flask app factory pattern.
128
158
129
159
130
160
Create Your Tables
@@ -145,44 +175,30 @@ This works great in bootstrap scripts.
145
175
Working with Tables
146
176
-------------------
147
177
148
- Now that you've got everything setup, you can easily access your tables in one
149
- of two ways: you can either access the table directly from the ``dynamo ``
150
- global, or you can access the table in a dictionary-like format through
151
- ``dynamo.tables ``.
178
+ Now that you've got everything setup, you can easily access your tables
179
+ in a dictionary-like format through ``dynamo.tables ``.
152
180
153
181
Below is an example view which creates a new user account::
154
182
155
183
# app.py
156
184
157
185
@app.route('/create_user')
158
186
def create_user():
159
- dynamo.users.put_item(data={
160
- 'username': 'rdegges',
161
- 'first_name': 'Randall',
162
- 'last_name': 'Degges',
163
-
164
- })
165
-
166
- # or ...
167
-
168
187
dynamo.tables['users'].put_item(data={
169
188
'username': 'rdegges',
170
189
'first_name': 'Randall',
171
190
'last_name': 'Degges',
172
191
173
192
})
174
193
175
- Either of the above will work the same.
176
-
177
194
On a related note, you can also use the ``dynamo.tables `` dictionary to iterate
178
195
through all of your tables (*this is sometimes useful *). Here's how you could
179
196
iterate over your existing DynamoDB tables::
180
197
181
198
# app.py
182
199
183
- with app.app_context():
184
- for table_name, table in dynamo.tables.iteritems():
185
- print table_name, table
200
+ for table_name, table in dynamo.tables.items():
201
+ print(table_name, table)
186
202
187
203
188
204
Deleting Tables
@@ -195,8 +211,7 @@ The below code snippet will destroy all of your predefined DynamoDB tables::
195
211
196
212
# app.py
197
213
198
- with app.app_context():
199
- dynamo.destroy_all()
214
+ dynamo.destroy_all()
200
215
201
216
.. note ::
202
217
Please be *extremely * careful when running this -- it has the potential to
0 commit comments