The DynamoDB component takes one or more table models as input and provides a full CRUD functionality for the data for the tables.
Name | Type | Description |
---|---|---|
region | string required |
The region the table will be created in. |
tables | object[] required |
Array of AWS DynamoDB table definitions |
Name | Type | Description |
---|---|---|
ddbtables | array |
Array containing information about all the deployed DynamoDB tables |
The component requires a few input parameters to manage the DynamoDB table(s):
region
: required -string
: AWS region where the table(s) will be created.tables
: required - An array of table models.name
: required -string
: The name of the table.hashKey
: required -string
: An existing field name from the schema that will be the partition key of type 'HASH'.rangeKey
: optional -string
: An existing field name from the schema that will be the sort key of type 'RANGE'.provisionedThroughput
: optionalreadCapacity
: optional -number
: The read capacity value.writeCapacity
: optional -number
: The write capacity value.
indexes
: optional - The indexes for the table model.- [A list of indexes and it's properties. See below.]
schema
: required - The schema for the table model.- [A list of field names and it's properties. See below.]
options
: optional - A list of additional options.- [A list of options. See below.]
The optional indexes
attribute of the table model is defined as below:
indexes
: optionalname
: required -string
: The name of the index.type
: required -string
: The type of the index. Values: [global
|local
]hashKey
: required -string
: An existing field name from the schema.rangeKey
: optional -string
: An existing field name from the schema.
The required schema
attribute of the table model is defined as below:
- [
fieldname
]: required -string
: The name of the field.type
: required -string
: The type of the field. Values: [string
|number
|binary
|boolean
|date
|uuid
|stringset
|numberset
|binaryset
|email
|username
|password
|birthyear
]options
: optionalrequired
: optional -boolean
: If the field is required or not. Values: [true
|false
]default
: optional - Default value for the field.
Note: In shorthand notation, it is [fieldname
]: [type
]
The optional options
attribute of the table model is defined as below:
timestamps
: optional -boolean
: Adds timestamp field names likecreatedAt
andupdatedAt
to the table schema. Values: [true
|false
]createdAt
: optional -boolean
: Overrides the timestamp attribute to specify ifcreatedAt
field is wanted or not. Values: [true
|false
]updatedAt
: optional -boolean
: Overrides the timestamp clause to specify ifupdatedAt
field is wanted or not. Values: [true
|false
]
type: dynamodb-model
inputs:
region: us-east-1
tables:
- name: BlogPost
hashKey: authorEmail
rangeKey: title
indexes:
- name: BlogTitleIndex
type: global
hashKey: title
rangeKey: createdAt
schema:
id: uuid
authorName: string
authorEmail:
type: email
options:
required: true
title: string
content: binary
tags: stringset
published:
type: boolean
options:
default: false
options:
timestamps: true
type: dynamodb-model
inputs:
region: us-east-1
tables:
- name: BlogPost
hashKey: authorEmail
rangeKey: title
provisionedThroughput:
readCapacity: 2
writeCapacity: 2
indexes:
- name: BlogTitleIndex
type: global
hashKey: title
rangeKey: createdAt
schema:
id: uuid
authorName: string
authorEmail:
type: email
options:
required: true
title: string
content: binary
tags: stringset
published:
type: boolean
options:
default: false
options:
timestamps: true
- name: User
hashKey: email
indexes:
- name: UserCityIndex
type: global
hashKey: city
schema:
fname: string
lname: string
email:
type: email
options:
required: true
city: string
options:
timestamps: true
The component exposes operations via these commands:
deploy
remove
insert
destroy
get
The deploy
command will create all the tables defined in the tables
list.
$ components deploy
Creating table(s)...
All tables have been created.
$ components remove --tablename <table name>
Removing table: '<table name>'
The insert
command will insert an item into the specified table.
$ components insert --tablename BlogPost --itemdata \
'{
"authorName": "Rupak Ganguly",
"authorEmail": "[email protected]",
"title": "How to create a DynamoDB component",
"content": "some junk data",
"tags": ["how-to", "DynamoDB", "components", "serverless"],
"published": true
}'
Item inserted to table: 'BlogPost'
{
"authorName":"Rupak Ganguly",
"authorEmail":"[email protected]",
"title":"How to create a DynamoDB component",
"content":{
"type":"Buffer",
"data":[115,111,109,101,32,109,111,114,101,32,106,117,110,107,32,100,97,116,97]
},
"tags":["how-to","DynamoDB","components","serverless"],
"published":true,
"id":"1959366d-d595-47a9-b9e1-baf929cea552"
}
$ components insert
Incorrect or insufficient parameters.
Usage: insert --tablename <tablename> --itemdata <data in json format>
$ components insert --tablename BlogPost
Incorrect or insufficient parameters.
Usage: insert --tablename <tablename> --itemdata <data in json format>
$ components insert --tablename BlogPost --itemdata '{}'
Error inserting data to table: 'BlogPost'
One or more parameter values were invalid: Missing the key authorEmail in the item
The get
command will retrieve an item from the specified table.
$ components get --tablename BlogPost --keydata \
'{
"authorEmail": "[email protected]",
"title": "How to create a DynamoDB component"
}'
Item retrieved from table: 'BlogPost'
{
"content":{
"type":"Buffer","data":[115,111,109,101,32,106,117,110,107,32,100,97,116,97]
},
"authorEmail":"[email protected]",
"authorName":"Rupak Ganguly",
"published":true,
"id":"8461c448-cb60-4045-8a71-4c9c1a8a1e13",
"tags":["DynamoDB","components","how-to","serverless"],
"title":"How to create a DynamoDB component"
}
components get --tablename BlogPost --keydata \
'{
"authorEmail": "[email protected]"
}'
Error retrieving item from table: 'BlogPost'
The provided key element does not match the schema
The destroy
command will delete an item from the specified table.
$ components destroy --tablename BlogPost --keydata \
'{
"authorEmail": "[email protected]",
"title": "How to create a DynamoDB component"
}'
Item deleted from table: 'BlogPost'
components destroy --tablename BlogPost --keydata \
'{
"authorEmail": "[email protected]"
}'
Error deleting item from table: 'BlogPost'
The provided key element does not match the schema