egg-dynamodb
Dynamodb plugin for Eggjs.
Install
$ npm i egg-dynamodb --save
Example
We can easily get the description of a table like below:
const params = TableName: "test_table" ; const tableDescription = await appdynamodbclient;
We can also put an item into a table like this:
const param = TableName : 'test_table' Item: HashKey: 'haskey' NumAttribute: 1 ; await appdynamodb;
As you can see, app.dynamodb
is the DynamoDB DocumentClient and app.dynamodb.client
is the DynamoDB low level client.
Configuration
Enable plugin:
// {app_root}/config/plugin.jsexportsdynamodb = enable: true package: 'egg-dynamodb';
Additionally, egg-dynamodb depend on egg-aws-sdk
, so we must add the following config:
// {app_root}/config/plugin.jsexportsawsSdk = enable: true package: 'egg-aws-sdk';
and don't forget to install the egg-aws-sdk
as a dependency:
$ npm i egg-aws-sdk --save
Configure the dynamodb client:
// {app_root}/config/config.default.jsexportsdynamodb = client: endpoint: '' region: '' accessKeyId: '' secretAccessKey: '' // or multi clients // clients: { // dynamodb1: { // endpoint: '', // region: '', // accessKeyId: '', // secretAccessKey: '', // }, // dynamodb2: { // endpoint: '', // region: '', // accessKeyId: '', // secretAccessKey: '', // }, // },;
Usage
Single Client
You can use app.dynamodb
to get the dynamodb instance.
// app/controller/home.js module { return Controller async { const ctx app = this; const param = TableName : 'test_table' Item: HashKey: 'haskey' NumAttribute: 1 ; await appdynamodb; } ;};
Multi Clients
If your Configure with multi clients, you can use app.dynamodb.get('instanceName')
to get the specific dynamodb instance and use it like above.
// app/controller/home.js module { return Controller async { const ctx app = this; const param = TableName : 'test_table' Item: HashKey: 'haskey' NumAttribute: 1 ; await appdynamodb; } ;};
API
The original aws interface does not provide the straight promise support, we must invoke the .promise()
to get the promise object. It is not very convenient to use. In order to simplify the usage, egg-dynamodb wrap all the interface making them auto return the promise object which means we can directly await
any function.
DynamoDB Client
- Async batchGetItem(param)
- Async batchWriteItem(param)
- Async createBackup(param)
- Async createGlobalTable(param)
- Async createTable(param)
- Async deleteBackup(param)
- Async deleteItem(param)
- Async deleteTable(param)
- Async describeBackup(param)
- Async describeContinuousBackups(param)
- Async describeGlobalTable(param)
- Async describeGlobalTableSettings(param)
- Async describeLimits(param)
- Async describeTable(param)
- Async describeTimeToLive(param)
- Async getItem(param)
- Async listBackups listGlobalTables(param)
- Async listTables(param)
- Async listTagsOfResource(param)
- Async putItem(param)
- Async query(param)
- Async restoreTableFromBackup(param)
- Async restoreTableToPointInTime(param)
- Async scan(param)
- Async tagResource untagResource(param)
- Async updateContinuousBackups(param)
- Async updateGlobalTable(param)
- Async updateGlobalTableSettings(param)
- Async updateItem(param)
- Async updateTable(param)
- Async updateTimeToLive(param)
- Async waitFor(param)
DynamoDB DocumentClient
- Async batchGet(param)
- Async batchWrite(param)
- Async createSet(param)
- Async delete(param)
- Async get(param)
- Async put(param)
- Async query(param)
- Async scan(param)
- Async update(param)
Questions & Suggestions
Please open an issue here.