1.0.3 • Published 2 years ago

@brightsole/sleep-talk v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

sleep-talk

What is it?

DynamoDB is a complicated beast at the best of times. I've striven to make a wrapper around it that works like many other ORMs. It should make it easier to folks coming from places like mongo where { property: { $contains: "partialWord" } } is something thrown around.

1.0.0 breaking change. You may pass in withMetadata into the context options to get the ItemResponse<T> type of response. It will include things straight from DynamoDB like count & ConsumedCapacity. This info isn't always super useful however, so the default return value is now just the T/T[] item or items with none of the object wrapping. This plays better with restful routes and a clearer interface.

Method nameInputResponse
getItem(T.id, { hashKey, withMetadata })Promise<T | ItemResponse<T>>
createItem(partial<T>, { hashKey, withMetadata })Promise<T | ItemResponse<T>>
updateItem(Partial<T>, { hashKey, withMetadata })Promise<T | ItemResponse<T>>
getAll({ hashKey, withMetadata })Promise<T[] | ItemsResponse<T>>
query(query, { withMetadata })Promise<T[] | ItemsResponse<T>>
deleteItem(T.id, { hashKey, withMetadata })Promise<null | ItemResponse<null>>

There is a query language at work in sleep-talk it allows for more nuanced scanning than direct equality. There are also parameters that aide in pagination. All of these special properties begin with $ to create a clear separation of concerns.

You may pass any property, with a 1:1 matching being the assumption (ie: { name: 'bob' }) and it will fetch all records named bob)

You may also pass in an array, and it will match any records that match ANY property in that list: (ie: { name: ['tina', 'louise'] } will return all records named tina OR louise)

In addition: nearly all filter behaviour that is useful from DynamoDB is recreated as follows:

query parametersInputTypeExpression conversion
$containsstringcontains(input, property_name)
$notContainsstringnot contains(input, property_name)
$notNullanythingattribute_not_exists(property_name)
$nullanythingattribute_exists(property_name)
$notEqvalueinput <> property_name
$gtvalueinput > property_name
$ltvalueinput < property_name
$limitintegerLimit: input
$startFromIdstringExclusiveStartKey: input
$isAscendingbooleanScanIndexForward: input
ANYTHINGanythinginput = property_name
ANYTHINGarray of anythinginput IN (p1, p2, pn...)

How to use it?

STEPS

Instantiated like so

const itemSource = new Database({
    tableName,
    region,
    getId: nanoid, // for example
  });

CHOICES THAT HAVE BEEN MADE

  1. T is assumed to have a unique identifier id
  2. the hashKey isn't a unique reference, but is a required property that makes getAlling work. It's understood that it will be used to narrow the querying pool to something manageable, since most groupings of items should be reasonably small.
  3. getId was pulled into a function, since most id generation libraries play up with Lambdas and serialisation.

TODO:

  1. full pagination support (all lastScannedId returns are null)
  2. finish filling out tests for the createFilterExpression

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.3.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.2.2

3 years ago

0.1.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago