egg-dynamodb

1.0.0 • Public • Published

egg-dynamodb

NPM version build status Test coverage David deps Known Vulnerabilities npm download

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 app.dynamodb.client.describeTable(params);

We can also put an item into a table like this:

  const param = {
    TableName : 'test_table',
    Item: {
      HashKey: 'haskey',
      NumAttribute: 1,
    }
  };
 
  await app.dynamodb.put(param);

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.js
exports.dynamodb = {
  enable: true,
  package: 'egg-dynamodb',
};

Additionally, egg-dynamodb depend on egg-aws-sdk, so we must add the following config:

// {app_root}/config/plugin.js
exports.awsSdk = {
  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.js
exports.dynamodb = {
  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.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      const param = {
        TableName : 'test_table',
        Item: {
          HashKey: 'haskey',
          NumAttribute: 1,
        }
      };
      await app.dynamodb.put(param);
    }
  };
};

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.exports = app => {
  return class HomeController extends app.Controller {
    async index() {
      const { ctx, app } = this;
      const param = {
        TableName : 'test_table',
        Item: {
          HashKey: 'haskey',
          NumAttribute: 1,
        }
      };
      await app.dynamodb.get('instance1').put(param);
    }
  };
};
 

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

DynamoDB DocumentClient

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-dynamodb

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

13.4 kB

Total Files

7

Last publish

Collaborators

  • ngot