Only available for Node.js.
Installation
Basic usage
Credentials and region
DynamoDB has no connection string. The AWS SDK resolves the credentials from its standard chain, in this order: environment variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY), a shared profile (AWS_PROFILE), then an IAM role. The region option sets the region, and AWS_REGION does the same when the option is absent.
The datasource calls
ListTables and DescribeTable to introspect, Query, Scan, GetItem and BatchGetItem to read, PutItem, UpdateItem, DeleteItem and BatchWriteItem to write, and ExecuteStatement for native queries. A read-only back-office needs only the read operations.
Choose the tables and adjust the collections
By default the datasource introspects every table of the account and region and names each collection after its table. List the tables to restrict the set:App becomes two collections and every other table keeps its own. Add tables: [] to expose the builder collections only.
Schema introspection
DescribeTable returns the keys and the indexes of a table. DynamoDB does not type the other attributes, so the datasource infers them from a sample of items.
Set both defaults for all tables with
introspectionConfig: { sampleSize, maxDepth }.
A table scan has no order, so the sample does not hold the latest items. Nested maps become columns named with @@@, such as address@@@city, which Forest shows as a path. An attribute absent or null in part of the sample is nullable. An attribute seen with several types becomes a Json column.
Save or write the introspection
introspect samples the tables and describes them as collection definitions. Save the result and give it back as the introspection of the datasource: the agent then starts without calling DescribeTable or sampling the tables.
introspection does not combine with a builder.
string, number, boolean, date, dateonly, timeonly, object, array, point and binary. date and dateonly hold ISO 8601 strings. binary holds base64 text and Forest filters it on the items the datasource reads. point holds GeoJSON, which Forest shows as Json. timeonly appears as Forest’s Time. An object without nested fields stays one Json column.
An index with a sort key leaves out the items that lack that attribute. The datasource reads such an index only when the filter has a condition on its sort key. Declare coversAllItems: true when every item holds the attribute, and the datasource reads the index whenever a filter pins its partition key.
A string or number set reads as an array. The datasource writes it back as a set when the introspection saw the attribute as a set, or when the file declares set: true on the array. Emptying a set in Forest removes the attribute, because DynamoDB holds no empty set. A number beyond 2^53 reads as text, which keeps every digit.
Composite keys
The partition key and the sort key are both primary key columns of the collection, so Forest identifies a record by the two. Keys do not change once a record exists: editing a key returns an error, and moving a record means deleting it and creating another. Forest greys the key fields in the creation form. When a string partition key or sort key stays empty, the datasource generates a UUID, and a sort key keeps its entity prefix when the collection has one. A numeric key has no generated value, so the record needs it from another source.Single-table design
Several collections can share one table, each restricted to its own items. Pick the items with a sort key prefix or with a discriminator attribute:Arrays as collections
Expose an array of maps as a collection of its own withvirtualCollections:
index in the array. Creating a record appends an element, updating sets the attributes of the element, and deleting removes it, so the index of the following elements shifts.
arrayField points inside a map with @@@, as in meta@@@items. An array inside an array becomes a collection whose parent is the first virtual collection, declared after it. The position in the outer array becomes a key column named <parent>Index, and creating an inner element needs it:
What Forest filters and sorts on
DynamoDB reads fast by key and scans otherwise, and it cannot sort, count or aggregate on the server. The datasource declares to Forest only what DynamoDB does itself, so that nothing in the interface reads a whole table to answer in memory.
DynamoDB orders and compares dates as text. Store dates in one offset, UTC, when a date is a sort key or when you filter on ranges of it.
Allow in-memory processing
SetinMemoryProcessing: true to bring back every operator Forest knows (IContains, Like, relative dates), a sort on any attribute and filters on the attributes of array elements. The datasource then reads the matching items and does the work itself, bounded by maxScannedItems and governed by scanPolicy. The setting is off by default because it reads more than it returns.
Control read costs
A request that no key or index serves reads the whole table.scanPolicy decides what happens:
The datasource refuses any other value at startup. Set
scanPolicy on the datasource or on a single collection.
maxScannedItems (default 1000000) stops a request that reads more items than the limit. The request ends with an error, never with a partial result. Set it on the datasource or on a collection.
logQueries logs the expressions sent to DynamoDB, and logConsumedCapacity logs the capacity units each request consumed. The AWS SDK retries throttled requests in adaptive mode, up to 10 attempts.
Native queries
SetliveQueryConnection to offer PartiQL SELECT statements as native queries, with $name placeholders:
SELECT. PartiQL cannot aggregate, so build charts from the aggregations of a collection instead.
Limitations
- Collections backed by a secondary index are not available. The datasource still reads an index as an access path of its table’s collection.
- Updates have no optimistic locking: two users who edit one record at the same time both succeed, and the last write wins.
- Binary partition keys and sort keys are not supported.
- Counts and charts read the items. An exact count on a large table reads the whole table.
Source code
This connector is open source. Browse the code or contribute on GitHub:@forestadmin/datasource-dynamodb.