dbfixtures-dynamodb-driver
v1.0.1
Published
A DynamoDb driver for the NPM dbfixtures package.
Downloads
75
Maintainers
Readme
Fixtures Manager DynamoDb Driver
An abstraction layer for the dynamodb client of the AWS SDK to facilitate handling database fixtures for testing purposes, in a DynamoDb database. This package is ment to be used in conjunction with the dbfixtures package, but can also be used by itself.
Installation
npm install --save-dev dbfixtures-dynamodb-driver
Usage
This package exposes the
create({
clientConfiguration: dynamodbClient.ClientConfiguration,
tableConfigs: { [key: string]: dynamodbClient.CreateTableInput }
}): Promise<IDriver>
function that returns a Promise that resolves with an instance of the driver.
Note1: For detailed information about the ClientConfiguration
argument, please consult the AWS JS SDK DynamoDb constructor documentation.
Note2: For detailed information about the CreateTableInput
argument, please consult the AWS JS SDK DynamoDb createTable documentation.
An instance of the driver exposes the following interface
// truncates the tables with the supplied names
truncate: (tableNames: string[]) => Promise<void>
// inserts the supplied items into the specified table
insertFixtures: (tableName: string, fixtures: [{}]) => Promise<void>
// does any necessary cleanup
close: () => Promise<void>
Example
This example uses Mocha as the potential test runner.
const dbfixtures = require('dbfixtures');
const fixturesDynamodbDriver = require('dbfixtures-dynamodb-driver');
const dynamodbConfig = {
region: 'eu-west-1',
credentials: {
accessKeyId: 'test key',
secretAccessKey: 'test key',
},
endpoint: 'http://localhost:4566'
};
const tableConfigs = {
roles: {
TableName: 'roles',
AttributeDefinitions: [
{
AttributeName: "id",
AttributeType: "N",
},
],
KeySchema: [
{
AttributeName: "id",
KeyType: "HASH",
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
},
},
users: {
TableName: 'users',
AttributeDefinitions: [
{
AttributeName: "id",
AttributeType: "N",
},
],
KeySchema: [
{
AttributeName: "id",
KeyType: "HASH",
},
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
},
},
};
const fixtures = {
'roles': [
{ id: { N: '1' }, name: { S: 'role 1' } },
{ id: { N: '2' }, name: { S: 'role 2' } },
],
'users': [
{ id: { N: '1' }, email: { S: '[email protected]' }, role_id: { N: '2' } },
{ id: { N: '2' }, email: { S: '[email protected]' }, role_id: { N: '1' } },
{ id: { N: '3' }, email: { S: '[email protected]' }, role_id: { N: '1' } },
],
};
describe('fixtures example', function () {
before(async function () {
const dynamodbDriver = await fixturesDynamodbDriver.create({
clientConfiguration: dynamodbConfig,
tableConfigs
});
dbfixtures.setDrivers(dynamodbDriver);
});
after(async function () {
await dbfixtures.closeDrivers();
});
beforeEach(async function () {
await dbfixtures.insertFixtures(fixtures);
});
it('should have the database seeded with the fixtures', function () {
// ...
});
});
Testing This Package
cd
into the package's directoryrun
npm install
run
npm run build
for unit tests run
npm test -- test\unit\
for integration tests run
npm test -- test\integration\
NOTE: requires an active DynamoDb server available atlocalhost:4566
for end-to-end tests run
npm test -- test\e2e\
NOTE: requires an active DynamoDb server available atlocalhost:4566
Suggestion to setting up a DynamoDb server on your local machine
If you are using Docker
, you can run the CLI command docker run --name testlocalstack -it -p 4566:4566 -e SERVICES=dynamodb localstack/localstack
to raise a container with the Localstack image and make an instance of DynamoDb available through localhost:4566
.