Git Product home page Git Product logo

dynamodb-localhost's Introduction

dynamodb-localhost

Join the chat at https://gitter.im/99xt/dynamodb-localhost npm version license

This library works as a wrapper for AWS DynamoDB Local, intended for use in devops. This library is capable of downloading and installing the DynamoDB Local with a simple set of commands, and pass optional attributes defined in 'DynamoDB Local Documentation'.

This Plugin Requires

  • Java Runtime Engine (JRE) version 6.x or newer

Features

Installation

npm install --save dynamodb-localhost

Usage

Usage example

var dynamodbLocal = require("dynamodb-localhost");
dynamodbLocal.install(); /* This is one time operation. Safe to execute multiple times which installs DynamoDB once. All the other methods depends on this. */
dynamodbLocal.start({port: 8000});

Supported methods

install(callback)   To install DynamoDB Local for usage (This is one time operation unless execute remove). 'callback' function is called after installation completes (or if already installed, immediately)
start(options)      To start an instance of DynamoDB Local. More information about options shown in the coming section
stop(port)          To stop particular instance of DynamoDb Local running on an specified port
remove(callback)    To remove DynamoDB Local instance. 'callback' function is called after removal complete.

NOTE: After executing start(options), DynamoDB will process incoming requests until you stop it. To stop DynamoDB, type Ctrl+C in the command prompt window. To view dynamodb interactive web shell, go to DynamoDB Local shell in your browser.

All options for DynamoDB start:

{ port : 8000, /* Port to listen on. Default: 8000 */
  cors : '*', /* Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. The default setting for cors is an asterisk (*), which allows public access. */
  inMemory : true, /* DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both dbPath and inMemory at once. */
  dbPath : '<mypath>/', /* The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both dbPath and inMemory at once. For the path, current working directory is <projectroot>/node_modules/dynamodb-localhost/dynamob. For example to create <projectroot>/node_modules/dynamodb-localhost/dynamob/<mypath> you should specify '<mypath>/' with a forwardslash at the end. */
  sharedDb : true, /* DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration. */
  delayTransientStatuses : true, /* Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status.) */
  optimizeDbBeforeStartup : true,  /* Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter. */
  heapInitial: undefined, /* A string which sets the initial heap size e.g., heapInitial: '2048m'. This is input to the java -Xms argument */
  heapMax: undefined, /* A string which sets the maximum heap size e.g., heapMax: '1g'. This is input to the java -Xmx argument */

Links

Contributing

We love our contributors! If you'd like to contribute to the project, feel free to submit a PR. But please keep in mind the following guidelines:

  • Propose your changes before you start working on a PR. You can reach us by submitting a Github issue. This is just to make sure that no one else is working on the same change, and to figure out the best way to solve the issue.
  • If you're out of ideas, but still want to contribute, help us in solving Github issues already verified.
  • Contributions are not just PRs! We'd be grateful for having you, and if you could provide some support for new comers, that be great! You can also do that by answering this plugin related questions on Stackoverflow. You can also contribute by writing. Feel free to let us know if you want to publish a useful guides, improve the documentation (attributed to you, thank you!) that you feel will help the community.

Credits

Bunch of thanks to doapp-ryanp who started dynamodb-local project

dynamodb-localhost's People

Contributors

aneilbaboo avatar ashanfernando avatar codyseibert avatar davidecavaliere avatar eheyder avatar exkazuu avatar gitter-badger avatar henhal avatar jrschumacher avatar kalpanibhagya avatar mjzone avatar rajikaimal avatar rehrumesh avatar ryanfelt avatar sutt0n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dynamodb-localhost's Issues

Remove dependency on rmdir

There is a moderate severity security issue with one of your dependencies (i.e., the path to the vuln serverless-localhost > rmdir > node.extend.

It looks like the easiest fix is to just stop using rmdir since it doesn't do much and hasn't been updated in quite a while. Let me know if you'd like me to submit a PR.

More info: https://npmjs.com/advisories/781

Allow a custom install path

Hello,
First of all, thanks for this lib and the serverless-dynamodb-local plugin!

I'm using this library through the serverless-dynamodb-local and when using it with yarn, I have to install the dynamodb jar everytime I install a new package (since yarn removes the bin folder that this lib creates to download the jar binary).

I have been able to make the changes that will use a better directory for this when using it with serverless (a .dynamodb dir int the service folder, same as .serverless one).

Most of the changes are in the serverless plugin, for this lib I just need to change the following piece of code in https://github.com/99xt/dynamodb-localhost/blob/master/dynamodb/starter.js

var starter = {
    start: function (options, config) {
        /* Dynamodb local documentation http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html */
        var additionalArgs = [],
            port = options.port || config.start.port,
-           db_dir = utils.absPath(config.setup.install_path),
+           db_dir = options.install_path || utils.absPath(config.setup.install_path),
            jar = config.setup.jar;

Then, in the serverless plugin I would change in https://github.com/99xt/serverless-dynamodb-local/blob/v1/index.js:

    constructor(serverless, options) {
        this.serverless = serverless;
        this.service = serverless.service;
        this.serverlessLog = serverless.cli.log.bind(serverless.cli);
        this.config = this.service.custom && this.service.custom.dynamodb || {};
-       this.options = options;
+       this.options = _.merge({
+               localPath: path.join(serverless.config.servicePath, '.dynamodb')
+           },
+           options
+       );
    startHandler() {
        const config = this.config;
        const options = _.merge({
-                sharedDb: this.options.sharedDb || true
+                sharedDb: this.options.sharedDb || true,
+                install_path: this.options.localPath
            },
            config && config.start,
            this.options
        );

        // otherwise endHandler will be mis-informed
        this.options = options;
        if (!options.noStart) {
          dynamodbLocal.start(options);
        }
        return BbPromise.resolve()
        .then(() => options.migrate && this.migrateHandler())
        .then(() => options.seed && this.seedHandler());
    }

Let me know what you think! I have the changes ready so if you are ok, I can create the two PRs.

local DynamoDB deleteItem behaves different compared to AWS DynamoDB

We have found in our use of DynamoDB local that the response from real AWS DynamoDB and Local DynamoDB are different when you attempt to delete data which does not exist. When using DynamoDB local the callback for <DynamoDbInstance>.deleteItem is called with a non-null value for the data parameter, however, for real AWS DynamoDB the callback is called with null for the data parameter.

We use dynamodb-localhost through serverless-dynamodb-local. We use [email protected] which pulls in [email protected]

Here is a representation of how we are exuting the deleteItem command:

var params = {
  Key: invalidKey,        // key that does not exist 
  TableName: "validTable" // table exists
 };
 dynamodb.deleteItem(params, function(err, data) {
 // data here should be null, which is the correct AWS behaviour
   if (err) console.log(err, err.stack);
   else     console.log(data); 
 });

When the data parameter is not null, dynamodb local responds with the following:

{"ConsumedCapacity":{"TableName":"validTable","CapacityUnits":1},"LOG_ID":"cqg6"}

Failed to handle redirect to download dynamodb_local

Current implementation does not cater for redirect, causing error when downloading dynamodb_local

/Users/anggra/Documents/Work/web/testing/node_modules/dynamodb-localhost/dynamodb/installer.js:23
                throw new Error('Error getting DynamoDb local latest tar.gz location ' + response.headers.location + ': ' + response.statusCode);
                ^

Error: Error getting DynamoDb local latest tar.gz location http://103.1.138.149/s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz: 302
    at ClientRequest.<anonymous> (/Users/anggra/Documents/Work/web/testing/node_modules/dynamodb-localhost/dynamodb/installer.js:23:23)
    at Object.onceWrapper (events.js:291:19)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:189:7)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:522:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
    at Socket.socketOnData (_http_client.js:411:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:189:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    at TCP.onread (net.js:551:20)

I've created a fix at https://github.com/anggras/dynamodb-localhost/tree/handle-redirect
Please let me know if it is acceptable

Receiving 403 response when downloading dynamodb_local_latest.tar.gz from s3 bucket

Hey ๐Ÿ‘‹

My first contribution here - seems to be a regression of #37

latest tar.gz was updated yesterday

<ContentsContents>
  <Key>dynamodb_local_latest.tar.gz</Key>
  <LastModified>2023-02-02T18:02:28.000Z</LastModified>
  <ETag>"265a5d3d71889672d30e2bce91e83387-6"</ETag>
  <Size>42739735</Size>
  <StorageClass>STANDARD</StorageClass>
</Contents>

But starting dynamodb fails with

Started downloading dynamodb-local from http://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz into .../.dynamodb. Process may take few minutes.
โœ– Uncaught exception
Environment: linux, node 14.17.0, framework 3.24.1 (local), plugin 6.2.2, SDK 4.3.2
Credentials: Local, "..." profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
Error: Error getting DynamoDb local latest tar.gz location undefined: 403
    at ClientRequest.<anonymous> (/.../node_modules/dynamodb-localhost/dynamodb/installer.js:29:15)

Unsure who owns the s3 bucket, but it is not reachable programmatically, but reachable through browser download https://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz

Checking if we are missing any headers

DynamoDB Local failed to start with code 1

I keep getting this error for some reason. Doesn't give too much detail about what exactly happened. Any ideas?

DynamoDB Local failed to start with code 1
{ Error: connect ECONNREFUSED 127.0.0.1:8000
    at Object.exports._errnoException (util.js:1012:11)
    at exports._exceptionWithHostPort (util.js:1035:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1080:14)
  message: 'connect ECONNREFUSED 127.0.0.1:8000',
  code: 'NetworkingError',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8000,
  region: 'us-west-2',
  hostname: 'localhost',
  retryable: true,
  time: 2016-09-13T03:26:05.804Z }

Not compatible with Windows

Issue

Using this library under Windows (cmd) does not spawn the DynamoDB java process.

Possible solution

  1. Passing in the shell: true option into child_process.spawn would execute the command in a shell.
  2. Replace child_process.spawn with a cross platform compatible shell library such as shelljs.

Version bump

The recent critical fixes to the download url have not been published to npm. Please bump the version to make that happen.

sls dynamodb install failing to download

Actual Behaviour

When I run
sls dynamodb install

it fails with the errors below:
Error: Error getting DynamoDb local latest tar.gz location undefined: 403
image

The AWS page and the corresponding S3 buckets that provide the local versions of DynamoDB seem to be working under https only.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html

It appears that serverless-dynamodb-local used to download the tar.gz file over http and that worked for a long time. But it appears that the security on the s3 bucket for local DynamoDbs have been tightened and only https is supported. This happened in early March 2023.

A fix to this would involve modifying the download link to https protocol and also ensuring that https works in installer.js

image

Expected Behaviour

Expect the latest local DynamoDB tar.gz file to download and be unzipped locally.

Steps to reproduce it

Run
sls dynamodb install

LogCat for the issue

Provide logs for the crash here

Screenshots of the issue

Where-ever possible attach a screenshot of the issue.

Would you like to work on the issue?

YES

localPath

installing new DyanamoDB does not add the local path in the path.

I have to assign localpath to "bin" in order to install the database.

[ '-Djava.library.path=/Users/aaalsubaie/Projects/propman-server/node_modules/dynamodb-localhost/dynamodb//bin/DynamoDBLocal_lib', '-jar', 'DynamoDBLocal.jar', '-port', 8000 ]

this might be the source of the problem:
db_dir = utils.absPath(config.setup.install_path)

tar.Extract is not a function

The latest update of tar version in package.json is breaking the 'sls dynamodb install' functionality in your other repo - serverless-dynamodb-local. Change it back to 2.0.0 fixes the issue.

Allow downloading via https

Behind a firewall, http could be disabled in some usecases... it would be nice to be able to support downloading via https as well

Allow to run in the background

As simple as this command is, this is truly required on any setup that requires testing.

Ideally in my mocha tests, I would write in the global_config.js:

/* eslint-disable */
const dynamodbLocal = require('dynamodb-localhost');

// setup the database
before(async function () {
  await dynamodbLocal.install();
  await dynamodbLocal.start({ port: 8000 });
});

// teardown
after(async function() {
  await dynamodbLocal.stop();
});

// run before each test
beforeEach(() => {});

// run after each test
afterEach(() => {});

Obviously with other test files next to it, who just make a dynamodb connection to the local test database.
This setup, the serverless plugin, or the original dynamodb-local, none of them support the option to run this in the background, as such, these are all useless when attempting to run a CI/CD job on. I can't tell a pipeline to "press ctrl-c" when you're done.

Support dockerized environment

Since I bought a MacBook with the M1 chip, all my projects using dynamodb-localhost (indirectly through serverless-dynamodb-local) are a pain, since the Java version of DynamoDB does not work on Apple Silicon.
There are several issues in dependent repos about this problem. StackOverflow's best answer is to install a separate Java environment within Rosetta, or even a separate entire brew installation, but this is really a hassle and comes with some issues.

I'm not sure if anyone is working on providing the missing (SQLite) library for arm64, but in the meantime, I was wondering if this project would be interested in offering support for running DynamoDB through docker, since that actually works fine on M1 macs?

It would save a ton of hassle for users who are today stuck with having to install Java under Rosetta 2 and then somehow get the rest of their environment running on native Apple architecture to interact with DynamoDB in a Java installation in an emulated i386 shell?

Basically the only difference is that instead of calling java -jar DynamoDB.jar <options> you call docker run -d -p 8000:8000 amazon/dynamodb-local -jar DynamoDB.jar <options>, and I was thinking this could either be a new option within the options passed to start(), or a separate startDocker(), or possibly controlled through an environment variable.

I'd be happy to give a PR a shot if interesting.

support for download via proxy

If you're trying to install behind a (corp) proxy, the http client doesn't attempt to make the connection via the users HTTP(S)_PROXY envvar.

I have a patch ready if this would be useful.

Only caveat is that the URL in the HTTP_PROXY envvar begins with a protocol e.g. HTTP_PROXY=http://localhost:3128. HTTP_PROXY=localhost:3128 wont work, due to limitations in Node's url parsing function.

Fix dynamo download

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/dynamodb-localhost/dynamodb/config.json b/node_modules/dynamodb-localhost/dynamodb/config.json
index 857d66f..207799a 100644
--- a/node_modules/dynamodb-localhost/dynamodb/config.json
+++ b/node_modules/dynamodb-localhost/dynamodb/config.json
@@ -1,6 +1,6 @@
 {
     "setup": {
-        "download_url": "http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz",
+        "download_url": "https://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz",
         "install_path": "/bin",
         "jar": "DynamoDBLocal.jar"
     },
diff --git a/node_modules/dynamodb-localhost/dynamodb/installer.js b/node_modules/dynamodb-localhost/dynamodb/installer.js
index b4c9450..3db2802 100644
--- a/node_modules/dynamodb-localhost/dynamodb/installer.js
+++ b/node_modules/dynamodb-localhost/dynamodb/installer.js
@@ -3,17 +3,17 @@
 var tar = require('tar'),
     zlib = require('zlib'),
     path = require('path'),
-    http = require('http'),
+    https = require('https'),
     fs = require('fs'),
     ProgressBar = require('progress'),
     utils = require('./utils');
 
 var download = function (downloadUrl, installPath, callback) {
-    http.get(downloadUrl, function (response) {
+    https.get(downloadUrl, function (response) {
             if (302 != response.statusCode) {
                 callback(new Error('Error getting DynamoDb local latest tar.gz location: ' + response.statusCode));
             }
-            http.get(response.headers.location, function (redirectResponse) {
+            https.get(response.headers.location, function (redirectResponse) {
                     var len = parseInt(redirectResponse.headers['content-length'], 10),
                         bar = new ProgressBar('Downloading dynamodb-local [:bar] :percent :etas', {
                             complete: '=',

This issue body was partially generated by patch-package.

unwanted console.log[s] in integration tests output

Running the following test:


describe('DynamodbService', () => {
  let instance: UserPersistenceService;

  beforeEach(() => {
    instance = new UserPersistenceService();
    dynamodb.install();
    dynamodb.start({
      port: 8000
    });
  });

  it('can be instantiated', () => {

    expect(instance instanceof UserPersistenceService).toBeTruthy();
  });


  afterEach(() => {
    dynamodb.stop({
      port: 8000
    })
  });
});

produces the following output in tests results:

 PASS  packages/datagator/src/lib/dynamodb/dynamodb.service.spec.ts (6.001s)
  โ— Console

    console.log node_modules/dynamodb-localhost/dynamodb/installer.js:12
      Started downloading dynamodb-local from http://s3-us-west-2.amazonaws.com/dynamodb-local/dynamodb_local_latest.tar.gz into /home/developer/src/microgamma/node_modules/dynamodb-localhost/dynamodb/bin. Process may take few minutes.
    console.log node_modules/dynamodb-localhost/index.js:30
      Dynamodb Local Started, Visit: http://localhost:8000/shell

I expect the output to be clean and to still be able to get debugging info if wanted using DEBUG=dynamodb-localhost:* environment variable

Add possibility to add `convertEmptyValues` option

Great work guys!

It would be awesome if you would allow us to pass the convertEmptyValues option to the local dynamodb.
Currently whenever you try to migrate a json object with an empty string as a value, you'll get:

One or more parameter values were invalid: An AttributeValue may not contain an empty string

exception.

DynamoDB Local Web Shell was deprecated.

DynamoDB Local Web Shell seems to be deprecated.

DynamoDB Local Web Shell was deprecated with version 1.16.X and is not available any longer from 1.17.X to latest.

See here: https://stackoverflow.com/questions/70535330/dynamodb-local-web-shell-does-not-load

It worked on DynamoDB Local Web Shell version 1.16.0, but not on 1.17.0.
So I think it is necessary to correct the message displayed when starting DynamoDB Local.

I have created a pull request, so please check it.

At amazon/dynamodb-local:1.16.0

$ docker run -p 8000:8000 amazon/dynamodb-local:1.16.0

1.16.0

At amazon/dynamodb-local:1.17.0

$ docker run -p 8000:8000 amazon/dynamodb-local:1.17.0

1.17.0

Error downloading Dynamodb ECONNREFUSED

During installation step, download is failing, where is it downloading from?

(using serverless-dynamodb-local)

Full stack trace:

sls dynamodb install
[Error: Error getting DynamoDb local latest tar.gz location: 404]
/Users/csilk/dev/project/node_modules/dynamodb-localhost/dynamodb/installer.js:43
                    throw Error("Error in downloading Dynamodb local " + err);
                    ^

Error: Error in downloading Dynamodb local Error: connect ECONNREFUSED 127.0.0.1:80
    at Error (native)
    at ClientRequest.<anonymous> (/Users/csilk/dev/project/node_modules/dynamodb-localhost/dynamodb/installer.js:43:27)
    at emitOne (events.js:90:13)
    at ClientRequest.emit (events.js:182:7)
    at Socket.socketErrorListener (_http_client.js:262:9)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at emitErrorNT (net.js:1246:8)
    at nextTickCallbackWith2Args (node.js:475:9)
    at process._tickDomainCallback (node.js:430:17)
$ sls dynamodb install
[Error: Error getting DynamoDb local latest tar.gz location: 404]
/Users/csilk/dev/project/node_modules/dynamodb-localhost/dynamodb/installer.js:43
                    throw Error("Error in downloading Dynamodb local " + err);
                    ^

Error: Error in downloading Dynamodb local Error: connect ECONNREFUSED 127.0.0.1:80
    at Error (native)
    at ClientRequest.<anonymous> (/Users/csilk/dev/project/node_modules/dynamodb-localhost/dynamodb/installer.js:43:27)
    at emitOne (events.js:90:13)
    at ClientRequest.emit (events.js:182:7)
    at Socket.socketErrorListener (_http_client.js:262:9)
    at emitOne (events.js:90:13)
    at Socket.emit (events.js:182:7)
    at emitErrorNT (net.js:1246:8)
    at nextTickCallbackWith2Args (node.js:475:9)
    at process._tickDomainCallback (node.js:430:17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.