Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanp committed Apr 1, 2015
1 parent 5adb305 commit 4897f79
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Logs
logs
*.log
*.iws
**/.idea
.DS_Store
.tmp

# Runtime data
pids
Expand All @@ -16,12 +20,8 @@ coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# dynamodb-local
dynamodb-local

A wrapper for AWS DynamoDB Local, intended for use in testcases.

# Usage

```javascript
var DynamoDbLocal = require('dynamodb-local');

DynamoDbLocal.launch(8000);
DynamoDbLocal.relaunch(8000);
```

See [AWS DynamoDB Docs](http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html) for more info on how to interact with DynamoDB Local.
122 changes: 122 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"use strict";

var os = require('os'),
spawn = require('child_process').spawn,
fs = require('fs'),
http = require('http'),
tar = require('tar'),
zlib = require('zlib'),
path = require('path'),
mkdirp = require('mkdirp');

const JARNAME = 'DynamoDBLocal.jar';

var tmpDynamoLocalDirDest = os.tmpdir() + 'dynamodb-local',
runningProcesses = {},
DynamoDbLocal = {
/**
*
* @param port
* @param dbPath if omitted will use in memory
* @param additionalArgs
* @returns {Promise.<ChildProcess>}
*/
launch: function (port, dbPath, additionalArgs) {
if (!dbPath) {
dbPath = '-inMemory';
}
else {
dbPath = '-dbPath ' + dbPath;
}

if (!additionalArgs) {
additionalArgs = [];
}
else if (Array.isArray(additionalArgs)) {
additionalArgs = [additionalArgs];
}

return installDynamoDbLocal()
.then(function () {
let args = [
'-Djava.library.path=./DynamoDBLocal_lib -jar ' + JARNAME,
'--port', port
];
args.concat(additionalArgs);

let child = spawn('java', args, {cwd: 'test', env: process.env});

runningProcesses[port] = child;

return child;
});
},
stop: function (port) {
if (runningProcesses[port]) {
runningProcesses[port].kill();
}
},
relaunch: function (port, db) {
this.stop(port);
this.launch(port, db);
}
};

module.exports = DynamoDbLocal;

function installDynamoDbLocal() {
console.log("Checking for ", tmpDynamoLocalDirDest);
return new Promise(function (resolve, reject) {
try {
let jarPath = tmpDynamoLocalDirDest + '/' + JARNAME,
stats = fs.lstatSync(tmpDynamoLocalDirDest);

if (stats.isDirectory() && fs.existsSync(tmpDynamoLocalDirDest + '/' + jarPath)) {
resolve();
}
}
catch (e) {
}

http
.get('http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.tar.gz', function (response) {
if (302 != response.statusCode) {
reject("Error getting DyanmoDb local latest tar.gz location: " + response.statusCode);
}

http
.get(response.headers['location'], function (redirectResponse) {
if (200 != redirectResponse.statusCode) {
reject("Error getting DyanmoDb local latest tar.gz location " + response.headers['location'] + ": " + redirectResponse.statusCode);
}
redirectResponse
.pipe(zlib.Unzip())
.pipe(tar.Parse())
.on('entry', function (entry) {
var fullpath = path.join(tmpDynamoLocalDirDest, entry.path);
if ('Directory' == entry.type) {
fs.mkdirSync(fullpath);
}
else {
mkdirp(path.dirname(fullpath), function (err) {
if (err) reject(err);
entry.pipe(fs.createWriteStream(fullpath));
});
}
})
.on('finish', function () {
resolve();
})
.on('error', function (err) {
reject(err);
});
})
.on('error', function (e) {
reject(e.message);
});
})
.on('error', function (e) {
reject(e.message);
});
});
}
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "dynamodb-local",
"description": "A wrapper for AWS DynamoDB Local, intended for use in testcases",
"version": "0.0.4",
"homepage": "https://github.com/doapp-ryanp/dynamodb-local",
"author": {
"name": "Ryan Pendergast (DoApp, Inc)",
"email": "[email protected]"
},
"main": "./index.js",
"engines": {
"iojs": ">= 1.6.3",
"npm": ">= 2.7.4"
},
"dependencies": {
"mkdirp": "^0.5.0",
"tar": "^2.0.0"
}
}
9 changes: 9 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

var DynamoDbLocal = require('./index');

DynamoDbLocal.launch(8000)
.then(function (ChildProcess) {
console.log("PID: ", ChildProcess.pid);
console.log("finished");
});

0 comments on commit 4897f79

Please sign in to comment.