Methods
init
Initialize the DynamoDB object.
db:init( config )
Config Properties
Name | Description | Type |
---|---|---|
aws_key | User AWS access key. | String |
aws_secret | User AWS secret key. | String |
aws_region | AWS region for DynamoDB. | String |
debug | Output response data structure. | Boolean |
Example
local db = require("dynamodb.client")
db:init({
aws_key = auth.key,
aws_secret = auth.secret,
aws_region = "us-east-1",
debug = true
})
Warning
It should be noted that your service could be compromised if someone gets a hold of your secret key. Make sure to always create a unique IAM user, with the minimum permissions required.
request
Make a request to the DynamoDB service.
Important
A request requires an event listener to reteive the response data. See Events.
db:request(action, payload)
Request Properties
Name | Description | Type |
---|---|---|
action | The DynamoDB action to perform. | String |
payload | The required payload for the action. | Table |
See Actions for more details on action types.
See Examples for more details on payload types.
events
To capture the request response data, you must register an event listener on the events property.
DynamoDBEvent
--set up listener function
local function dynamoDBResponse( evt )
if evt.error then
print(evt.reason, evt.status)
else
--response as table data
local data_table = evt.result --Table
--response as JSON string
print(evt.response) --JSON
end
end
--add event listener
db.events:addEventListener("DynamoDBEvent", dynamoDBResponse)
--make request
db:request("ListTables", {Limit=5})
Event Properties
The "DynamoDBEvent" will fire when data from the service is returned. The event can contain the following properties.
error
If the error propery is set, an error of some type has occurred. You can view the reason, and status code using the event properties:
local function dbListener( evt )
if evt.error then
--reason
print(evt.reason)
--status code
print(evt.status)
end
end
...
result
If no error has occured then you can retreive the data table through the result property:
local function dbListener( evt )
if not evt.error then
--result table
print(evt.result.some_key_path)
end
end
Note
If you'd like to get a hold of the raw JSON data response, you can use the response property.
Attribute Type Methods
DynamoDB attribute values require an attribute type. While you can manually create each value entry, these methods offer another approach, as well as, make sure the encoding is properly handled.
For more details on attributes data see DynamoDB AttributeValue.
S
Returns a formatted string value as an attribute value.
local str = db:S("A string value")
N
Returns a formatted number value an attribute value.
local num = db:N(12)
B
Returns encoded binary data as an attribute value.
local bin = db:B(<binary-data>)
M
Returns a map value as an attribute value.
Tip
A map is equivalent to a Lua data table. The values in a map must be typed. A map can hold any attribute type, including nested values.
local map = db:M({
Artist = db:S("The Acme Band"),
Rating = db:N(10),
Genre = db:L(db:S("Pop"), db:S("Upbeat"))
})
L
Returns a list value as an attribute value.
Tip
A list is equivalent to a Lua table array. The values in a list must be typed. A list can hold any attribute type, including nested values.
local list = db:L({
db:S("Hello"),
db:N(25),
db:BOOL(false),
db:M({Age=db:N(25), Name=db:S("John")})
})
SS
Returns a string set as an attribute value.
Tip
A string set is equivalent to a table array that can only contain string attribute types.
local string_set = db:SS({
db:S("Hello"),
db:S("World")
})
NS
Returns a number set as an attribute value.
Tip
A number set is equivalent to a table array that can only contain number attribute types.
local num_set = db:NS({
db:N(12),
db:N(34),
db:N(2.34)
})
BS
Returns a binary set as an attribute value.
Tip
A binary set is equivalent to a table array that can only contain binary attribute types.
local bin_set = db:BS({
db:B(<binary-data>),
db:B(<binary-data>)
})
BOOL
Returns a boolean value as an attribute value.
local bool = db:BOOL(false)
NULL
Returns a null value type as an attribute value.
local null = db:NULL()