Skip to content

Commit

Permalink
Fix docker, seed, noStart support
Browse files Browse the repository at this point in the history
Fixes #13
Fixes #15
  • Loading branch information
domdomegg committed Oct 15, 2023
1 parent 16689ba commit f78ae34
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 45 deletions.
74 changes: 55 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,35 +127,71 @@ Note: This is useful if the serverless dynamodb install failed in between to com

### Start: serverless dynamodb start

This starts the DynamoDB Local instance, either as a local Java program or, if the `--docker` flag is set,
by running it within a docker container. The default is to run it as a local Java program.
This starts the DynamoDB Local instance, either as a local Java program or, if the `--docker` flag is set, by running it within a docker container.

All CLI options are optional:

```
--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.
--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.
--dbPath -d 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/aws-dynamodb-local/dynamodb. For example to create <projectroot>/node_modules/aws-dynamodb-local/dynamodb/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.
--sharedDb -h 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 -t 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 -o Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.
--migration -m After starting dynamodb local, run dynamodb migrations.
--heapInitial The initial heap size
--heapMax The maximum heap size
--migrate -m After starting DynamoDB local, create DynamoDB tables from the Serverless configuration.
--seed -s After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.
--convertEmptyValues -e Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.
--docker Run DynamoDB inside docker container instead of as a local Java program
--dockerImage Specify custom docker image. Default: amazon/dynamodb-local
```ts
interface StartOptions {
/** Port to listen on. @default 8000 */
port: number,
/** Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. @default "*", which allows public access. */
cors: string,
/** Whether to 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. @default true */
inMemory: boolean,
/** 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/aws-dynamodb-local/dynamodb. For example to create <projectroot>/node_modules/aws-dynamodb-local/dynamodb/<mypath> you should specify '<mypath>/' with a forward slash at the end. @default undefined */
dbPath: string | undefined,
/** 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. @default true */
sharedDb: boolean,
/** 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.) @default true */
delayTransientStatuses: boolean,
/** Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter. @default true */
optimizeDbBeforeStartup: boolean,
/** Prints a usage summary and options. */
help: boolean,
/** A string which sets the initial heap size e.g. '2G'. This is input to the java -Xms argument. @default undefined */
heapInitial: string | undefined,
/** A string which sets the maximum heap size e.g. '4G'. This is input to the java -Xmx argument. @default undefined */
heapMax: string | undefined,
/** Run DynamoDB inside docker container instead of as a local Java program. @default false */
docker: boolean,
/** If docker enabled, custom docker path to use. @default "docker" */
dockerPath: string,
/** If docker enabled, docker image to run. @default "amazon/dynamodb-local" */
dockerImage: string,
/** Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB. **/
convertEmptyValues: boolean,
/** Do not start DynamoDB local (e.g. for use cases where it is already running) */
noStart: boolean,
/** After starting DynamoDB local, create DynamoDB tables from the Serverless configuration. */
migrate: boolean,
/** After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload. */
seed: boolean,
}
```

All the above options can be added to serverless.yml to set default configuration: e.g.

```yaml
custom:
serverless-dynamodb:
# If you only want to use DynamoDB Local in some stages, declare them here
# If you only want to use DynamoDB Local in some stages, declare them here
stages:
- dev
start:
Expand Down
64 changes: 38 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ class ServerlessDynamodbLocal {
shortcut: "o",
usage: "Will connect to the tables online to do an online seed run",
type: "boolean"
}
},
seed: {
shortcut: "s",
usage: "After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.",
// NB: no `type` intentionally to allow both boolean and string values
},
}
},
start: {
Expand Down Expand Up @@ -74,20 +79,9 @@ class ServerlessDynamodbLocal {
usage: "Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.",
type: "boolean"
},
migrate: {
shortcut: "m",
usage: "After starting dynamodb local, create DynamoDB tables from the current serverless configuration.",
type: "boolean"
},
seed: {
shortcut: "s",
usage: "After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.",
type: "boolean"
},
migration: {
shortcut: 'm',
usage: 'After starting dynamodb local, run dynamodb migrations',
type: "boolean"
help: {
usage: "Prints a usage summary and options.",
type: "boolean",
},
heapInitial: {
usage: 'The initial heap size. Specify megabytes, gigabytes or terabytes using m, b, t. E.g., "2m"',
Expand All @@ -97,18 +91,39 @@ class ServerlessDynamodbLocal {
usage: 'The maximum heap size. Specify megabytes, gigabytes or terabytes using m, b, t. E.g., "2m"',
type: "string"
},
docker: {
usage: 'Run DynamoDB inside docker container instead of as a local Java program.',
type: "boolean"
},
dockerPath: {
usage: 'If docker enabled, custom docker path to use.',
type: "string"
},
dockerImage: {
usage: 'If docker enabled, docker image to run.',
type: "string"
},
convertEmptyValues: {
shortcut: "e",
usage: "Set to true if you would like the document client to convert empty values (0-length strings, binary buffers, and sets) to be converted to NULL types when persisting to DynamoDB.",
type: "boolean"
}
},
noStart: {
usage: "Do not start DynamoDB local (e.g. for use cases where it is already running)",
type: "boolean",
},
migrate: {
shortcut: "m",
usage: "After starting dynamodb local, create DynamoDB tables from the current serverless configuration.",
type: "boolean"
},
seed: {
shortcut: "s",
usage: "After starting and migrating dynamodb local, injects seed data into your tables. The --seed option determines which data categories to onload.",
// NB: no `type` intentionally to allow both boolean and string values
},
}
},
noStart: {
shortcut: "n",
default: false,
usage: "Do not start DynamoDB local (in case it is already running)",
},
remove: {
lifecycleEvents: ["removeHandler"],
usage: "Removes local DynamoDB"
Expand Down Expand Up @@ -241,7 +256,7 @@ class ServerlessDynamodbLocal {
if (this.shouldExecute()) {
const options = _.merge({
sharedDb: this.options.sharedDb || true,
install_path: this.options.localPath
installPath: this.options.localPath
},
this.config.start,
this.options
Expand All @@ -255,10 +270,7 @@ class ServerlessDynamodbLocal {
options.dbPath = path.isAbsolute(dbPath) ? dbPath : path.join(this.serverless.config.servicePath, dbPath);
}

if (!options.noStart) {
dynamodbLocal.start(options);
}
return Promise.resolve()
(options.noStart ? Promise.resolve() : dynamodbLocal.start(options))
.then(() => options.migrate && this.migrateHandler())
.then(() => options.seed && this.seedHandler());
} else {
Expand Down

0 comments on commit f78ae34

Please sign in to comment.