🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

egg-dynamodb

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-dynamodb

dynamodb plugin for egg.js.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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

Keywords

egg

FAQs

Package last updated on 04 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts