New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

serverless-dynamodb-local

Package Overview
Dependencies
Maintainers
1
Versions
56
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

serverless-dynamodb-local - npm Package Compare versions

Comparing version 0.2.10 to 0.2.11

416

index.js

@@ -9,167 +9,132 @@ 'use strict';

module.exports = function (S) {
class DynamodbLocal extends S.classes.Plugin {
constructor() {
super();
this.name = 'serverless-dynamodb-local'; // Define your plugin's name
}
/**
* Register Actions
* - If you would like to register a Custom Action or overwrite a Core Serverless Action, add this function.
* - If you would like your Action to be used programatically, include a "handler" which can be called in code.
* - If you would like your Action to be used via the CLI, include a "description", "context", "action" and any options you would like to offer.
* - Your custom Action can be called programatically and via CLI, as in the example provided below
*/
registerActions() {
S.addAction(this.create.bind(this), {
handler: 'dynamodbCreate',
description: 'Create new migration template with the given name',
context: 'dynamodb',
contextAction: 'create',
options: [{
option: 'name',
shortcut: 'n',
description: 'Create a migration template inside the directlry given in s-project.json with the given name.'
}]
});
S.addAction(this.execute.bind(this), {
handler: 'dynamodbExecute',
description: 'Execute a migration template with the given name',
context: 'dynamodb',
contextAction: 'execute',
options: [{
option: 'name',
shortcut: 'n',
description: 'Execute a migration template with the given name'
}, {
option: 'region',
shortcut: 'r',
description: 'Region that dynamodb should be remotely executed'
}, {
option: 'stage',
shortcut: 's',
description: 'Stage that dynamodb should be remotely executed'
}, {
option: 'table_prefix',
shortcut: 't',
description: 'Table name prefix'
}, {
option: 'table_suffix',
shortcut: 'x',
description: 'Table name suffix'
},
{
option: 'profile',
shortcut: 'p',
description: 'aws profile'
class ServerlessDynamodbLocal {
constructor(serverless, options) {
this.serverless = serverless;
this.service = serverless.service;
this.options = options;
this.provider = 'aws';
this.commands = {
dynamodb: {
commands: {
create: {
lifecycleEvents: ['createHandler'],
options: {
name: {
required: true,
shortcut: 'n',
usage: 'Create a migration template inside the directlry given in s-project.json with the given name.',
}
}
},
execute: {
lifecycleEvents: ['executeHandler'],
options: {
name: {
required: true,
shortcut: 'n',
usage: 'Execute a migration template with the given name'
},
region: {
shortcut: 'r',
usage: 'Region that dynamodb should be remotely executed'
},
stage: {
shortcut: 's',
usage: 'Stage that dynamodb should be remotely executed'
}
}
},
executeAll: {
lifecycleEvents: ['executeAllHandler']
},
start: {
lifecycleEvents: ['startHandler'],
options: {
port: {
shortcut: 'p',
usage: 'The port number that DynamoDB will use to communicate with your application. If you do not specify this option, the default port is 8000'
},
cors: {
shortcut: 'c',
usage: '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: {
shortcut: 'i',
usage: '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: {
shortcut: 'd',
usage: '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/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.'
},
sharedDb: {
shortcut: 'h',
usage: '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: {
shortcut: 't',
usage: '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: {
shortcut: 'o',
usage: 'Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.'
},
migration: {
shortcut: 'm',
usage: 'After starting dynamodb local, run dynamodb migrations'
}
}
},
remove: {
lifecycleEvents: ['removeHandler']
},
install: {
lifecycleEvents: ['installHandler'],
options: {
localPath: {
shortcut: 'x',
usage: 'Local dynamodb install path'
}
}
}
}
]
});
S.addAction(this.executeAll.bind(this), {
handler: 'dynamodbExecuteAll',
description: 'Execute all migration templates',
context: 'dynamodb',
contextAction: 'executeAll',
options: [{
option: 'region',
shortcut: 'r',
description: 'Region that dynamodb should be remotely executed'
}, {
option: 'stage',
shortcut: 's',
description: 'Stage that dynamodb should be remotely executed'
}, {
option: 'table_prefix',
shortcut: 't',
description: 'Table name prefix'
}, {
option: 'table_suffix',
shortcut: 'x',
description: 'Table name suffix'
},
{
option: 'profile',
shortcut: 'p',
description: 'aws profile'
}]
});
S.addAction(this.remove.bind(this), {
handler: 'dynamodbRemove',
description: 'Remove dynamodb local database. This is needed if the installed version is currupted or needs to be upgraded.',
context: 'dynamodb',
contextAction: 'remove'
});
S.addAction(this.install.bind(this), {
handler: 'dynamodbInstall',
description: 'Install dynamodb local database. This is a single time operation.',
context: 'dynamodb',
contextAction: 'install'
});
S.addAction(this.start.bind(this), {
handler: 'dynamodbStart',
description: 'Start dynamodb local database',
context: 'dynamodb',
contextAction: 'start',
options: [{
option: 'port',
shortcut: 'p',
description: 'The port number that DynamoDB will use to communicate with your application. If you do not specify this option, the default port is 8000'
}, {
option: 'cors',
shortcut: 'c',
description: '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.'
}, {
option: 'inMemory',
shortcut: 'i',
description: '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.'
}, {
option: 'dbPath',
shortcut: 'd',
description: '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/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.'
}, {
option: 'sharedDb',
shortcut: 'h',
description: '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.'
}, {
option: 'delayTransientStatuses',
shortcut: 't',
description: '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.)'
}, {
option: 'optimizeDbBeforeStartup',
shortcut: 'o',
description: 'Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.'
}, {
option: 'migration',
shortcut: 'm',
description: 'After starting dynamodb local, run dynamodb migrations'
}]
});
}
};
return BbPromise.resolve();
}
this.hooks = {
'dynamodb:create:createHandler': this.createHandler.bind(this),
'dynamodb:execute:executeHandler': this.executeHandler.bind(this),
'dynamodb:executeAll:executeAllHandler': this.executeAllHandler.bind(this),
'dynamodb:remove:removeHandler': this.removeHandler.bind(this),
'dynamodb:install:installHandler': this.installHandler.bind(this),
'dynamodb:start:startHandler': this.startHandler.bind(this),
};
}
createHandler() {
let self = this,
options = this.options;
return new BbPromise(function(resolve, reject) {
let dynamodb = self.dynamodbOptions(),
tableOptions = self.tableOptions();
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.create(options.name).then(resolve, reject);
});
}
dynamodbOptions(stage, region) {
let credentials, config = S.getProject().custom.dynamodb || {},
dynamodbOptions(region) {
let self = this;
let credentials, config = self.service.custom.dynamodb || {},
port = config.start && config.start.port || 8000,
dynamoOptions;
if (stage && region) {
credentials = S.getProvider('aws').getCredentials(stage, region);
if(region){
AWS.config.update({
region: region,
accessKeyId: credentials.accessKeyId,
secretAccessKey: credentials.secretAccessKey,
sessionToken: credentials.sessionToken
region: region
});
} else {
dynamoOptions = {
}else{
dynamoOptions = {
endpoint: 'http://localhost:' + port,
region: 'localhost'
};
}
return {
}
return {
raw: new AWS.DynamoDB(dynamoOptions),

@@ -181,5 +146,6 @@ doc: new AWS.DynamoDB.DocumentClient(dynamoOptions)

tableOptions(table_prefix, table_suffix) {
let config = S.getProject().custom.dynamodb,
let self = this;
let config = self.service.custom.dynamodb,
migration = config && config.migration || {},
rootPath = S.getProject().getRootPath(),
rootPath = self.serverless.config.servicePath,
path = rootPath + '/' + (migration.dir || 'dynamodb'),

@@ -196,89 +162,61 @@ suffix = table_suffix || migration.table_suffix || '',

/**
* Custom Action Example
* - Here is an example of a Custom Action. Include this and modify it if you would like to write your own Custom Action for the Serverless Framework.
* - Be sure to ALWAYS accept and return the "evt" object, or you will break the entire flow.
* - The "evt" object contains Action-specific data. You can add custom data to it, but if you change any data it will affect subsequent Actions and Hooks.
* - You can also access other Project-specific data @ this.S Again, if you mess with data on this object, it could break everything, so make sure you know what you're doing ;)
*/
executeHandler() {
let self = this,
options = this.options;
return new BbPromise(function(resolve, reject) {
let dynamodb = self.dynamodbOptions(options.stage, options.region),
tableOptions = self.tableOptions();
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.execute(options.name, tableOptions).then(resolve, reject);
});
}
remove() {
return new BbPromise(function (resolve) {
dynamodbLocal.remove(resolve);
});
}
executeAllHandler() {
let self = this;
return new BbPromise(function(resolve, reject) {
let dynamodb = self.dynamodbOptions(self.service.provider.region),
tableOptions = self.tableOptions();
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.executeAll(tableOptions).then(resolve, reject);
});
}
install() {
return new BbPromise(function (resolve) {
dynamodbLocal.install(resolve);
});
}
removeHandler() {
return new BbPromise(function(resolve) {
dynamodbLocal.remove(resolve);
});
}
execute(evt) {
let self = this,
options = evt.options;
if(options.profile) {
AWS.config.credentials =
new AWS.SharedIniFileCredentials({ profile: options.profile });
}
return new BbPromise(function (resolve, reject) {
let dynamodb = self.dynamodbOptions(options.stage, options.region),
tableOptions = self.tableOptions(options.table_prefix, options.table_suffix);
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.execute(options.name, tableOptions).then(resolve, reject);
});
}
installHandler() {
let self = this,
options = this.options;
return new BbPromise(function(resolve) {
dynamodbLocal.install(resolve, options.localPath);
});
}
executeAll(evt) {
let self = this,
options = evt.options;
if(options.profile) {
AWS.config.credentials =
new AWS.SharedIniFileCredentials({ profile: options.profile });
startHandler() {
let self = this,
options = this.options;
return new BbPromise(function(resolve) {
let config = self.service.custom.dynamodb,
options = _.merge({
sharedDb: self.options.sharedDb || true
},
self.options,
config && config.start
);
if (options.migration) {
dynamodbLocal.start(options);
console.log(""); // seperator
self.executeAllHandler();
resolve();
} else {
dynamodbLocal.start(options);
console.log("");
resolve();
}
return new BbPromise(function (resolve, reject) {
let dynamodb = self.dynamodbOptions(options.stage, options.region),
tableOptions = self.tableOptions(options.table_prefix, options.table_suffix);
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.executeAll(tableOptions).then(resolve, reject);
});
}
create(evt) {
let self = this,
options = evt.options;
return new BbPromise(function (resolve, reject) {
let dynamodb = self.dynamodbOptions(),
tableOptions = self.tableOptions();
dynamodbMigrations.init(dynamodb, tableOptions.path);
dynamodbMigrations.create(options.name).then(resolve, reject);
});
}
start(evt) {
let self = this;
return new BbPromise(function (resolve) {
let config = S.getProject().custom.dynamodb,
options = _.merge({
sharedDb: evt.options.sharedDb || true
},
evt.options,
config && config.start
);
if (options.migration) {
dynamodbLocal.start(options);
console.log(""); // seperator
self.executeAll(evt);
resolve();
} else {
dynamodbLocal.start(options);
console.log("");
resolve();
}
});
}
});
}
return DynamodbLocal;
};
}
module.exports = ServerlessDynamodbLocal;
{
"name": "serverless-dynamodb-local",
"version": "0.2.10",
"version": "0.2.11",
"engines": {

@@ -5,0 +5,0 @@ "node": ">=4.0"

@@ -10,3 +10,3 @@ serverless-dynamodb-local

## This Plugin Requires
* Serverless V0.5 or newer
* serverless@v1-rc.1
* Java Runtime Engine (JRE) version 6.x or newer

@@ -22,4 +22,7 @@

Then in `s-project.json` add following entry to the plugins array: `serverless-dynamodb-local`
e.g `"plugins": ["serverless-dynamodb-local"]`
Then in `serverless.yml` add following entry to the plugins array: `serverless-dynamodb-local`
```yml
plugins:
- serverless-dynamodb-local
```

@@ -43,5 +46,2 @@ ## Using the Plugin

* Execute migration(s) in remote DynamoDB use additional parameters(region and stage) after execute/executeAll. e.g.
`sls dynamodb executeAll -r us-west-1 -s dev`
Note: Read the detailed section for more information on advanced options and configurations. Open a browser and go to the url http://localhost:8000/shell to access the web shell for dynamodb local.

@@ -58,3 +58,3 @@

```
--port -p Port to listen on. Default: 8000
--port -p Port to listen on. Default: 8000
--cors -c 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.

@@ -69,54 +69,34 @@ --inMemory -i 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.

All the above options can be added to s-project.json to set default configuration: e.g
All the above options can be added to serverless.yml to set default configuration: e.g
```json
"custom": {
"dynamodb": {
"start": {
"port": "8000",
"inMemory": true,
"migration": true
}
}
}
```yml
custom:
dynamodb:
start:
port: 8000
inMemory: true
migration: true
migration:
dir: ./offline/migrations
```
## Migrations: sls dynamodb <migration-command>
Migration-Commands create, execute, executeAll
## Migrations: sls dynamodb create/execute/executeAll
### Configurations
In `s-project.json` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
```json
"custom": {
"dynamodb": {
"migration": {
"dir": "dynamodbMigrations",
"table_prefix": "",
"table_suffix": ""
}
}
}
In `serverless.yml` add following to customize DynamoDB Migrations file directory and table prefixes/suffixes
```yml
custom:
dynamodb:
migration:
dir: dynamodbMigrations
table_prefix: prefix
table_suffix": suffix
```
In `s-project.json` add following to execute all the migration upon DynamoDB Local Start
```json
"custom": {
"dynamodb": {
"start": {
"migration": true
}
}
}
In `serverless.yml` add following to execute all the migration upon DynamoDB Local Start
```yml
custom:
dynamodb:
start:
migration: true
```
For Migration-Commands execute/executeAll following optional parameters can be used
```
--region -r Region that dynamodb should be remotely executed.
--stage -s Stage that dynamodb should be remotely executed.
--table_prefix -t Dynamodb Table name prefixs (E.g. for prefix = production- and abstract table-name = users, after adding the prefix it will be production-users)
--table_suffix -x Table name suffix (E.g for suffix = -test and abstract table-name = users, after adding the suffix it will be users-test )
--profile -p Use another AWS Profile to execute migration
--name -n Execute a migration template with the given name (This is only for execute command and not applicable for executeAll).
```
### Migration Template

@@ -183,3 +163,2 @@ ```json

}
```

@@ -186,0 +165,0 @@ Before modifying the migration template, refer the (Dynamodb Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property and (Dynamodb Document Client SDK): http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property links.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc