Skip to main content

dynamodb

The depot.start() method, starts

  • Each data store in its own container
  • Local Depot configured with each data store
const depot = new DepotLocal({
depot: {},
dynamodb: {}
});

beforeAll(async () => {
await depot.start();
});

afterAll(async () => {
await depot.stop();
});

Once started, you can deploy a new Environment to your Depot local instance, using depot.deploy(). Use a helper method to easily construct an environment, for example from a namespace.

const namespace = Schemas.fromYamlFiles("test/schema/pet.yml");
const env = depot.deploy(Environment.fromNamespace(namespace));

Each deployed dataset has its own DepotApi, for simple tests there will be only one.

const api = env.api(0);

The api can then be used to make requests to your environment.

const create = await api.create({
schema: "pet.Pet",
data: {
name: "Ren",
age: 2
}
});

Jest matchers can then be used to assert the response.

expect(create).toMatchObject({
data: {
name: "Ren",
age: 2,
version: 1
},
status: {
code: 201
}
});

You can access data with certainty by letting the compiler remove the possibility of an error:$

if (isFailedResponse(create)) {
throw new Error("Failed to create pet");
}
// at this point, create.data.id is valid, create.data.schema is valid, etc.

For working examples, see the test examples in compositeElastic.test.ts.

caution

the env and api values should not be shared, but private to each individual test. This will ensure Depot-Local provides an empty dataset at each execution.

Enabling test scan

It is generally not possible to use DynamoDB scans in a deployed environment, since objects of all schemas and across all datasets are located in the same underlying DynamoDB tables. Depot indexes should be used to limit the amount of DynamoDB results processed, or another query engine should be used (e.g. a DDB + Elasticsearch composite)

However, during a test it might be convenient to run an open list query or a list query that cannot usually be satisfied by a DynamoDB index, in this case, use the dynamoDbWithTestScan location instead of the default dynamoDb location:

const namespace = Schemas.fromYamlFiles("test/schema/pet.yml");
const env = depot
.deploy(new EnvironmentBuilder(pet).location(LocationBuilder.dynamoDbWithTestScan()).build())
.api(0);

Now, open list queries can be performed on a given schema as follows:

const list = await api.invoke({ schema: "pet.Pet", operation: Operation.list });

Reuse Containers

For faster development, you can keep Depot local containers alive between tests by setting the DEPOT_LOCAL_KEEP_ALIVE environment variable.

// For development use only, DO NOT commit
process.env.DEPOT_LOCAL_KEEP_ALIVE = "true";
await depot.start();