1.3.2 • Published 2 years ago

@pidz-developer/dynamo-light v1.3.2

Weekly downloads
14
License
ISC
Repository
bitbucket
Last release
2 years ago

DynamoDB Abstraction package

Import and Instantiate DynamoLight

import DynamoLight from "@pidz-developer/dynamo-light";

const client = new DynamoLight(options: {});

options can be found aws docs


Supported methods

getItem execute

  • getItem
  • getItems
  • putItem
  • putItems
  • deleteItem
  • deleteItems
  • update
  • query
  • scan
  • execute

#GetItem ###Description:

Get a single item given the key.

###Params:

table: Table to retrieve item from
key: Partition key and optional sort key
options?: Additional options

  • Attr?: Item attributes to return
  • Consistent?: False (default): eventual consistency read, True: strong consistency read

###Returns:

Promise<AttributeMap | null>: The item if its found otherwise null

public async client.getItem(table: string, key: Key, options?: GetItemOptions): Promise<AttributeMap | null> {}
// Most simple example
await client.getItem('Name', { pKey: { uuid: '12345' } });
// Usage with additional options
await client.getItem(
  'Product', 
  { pKey: { uuid: '692308' }, sKey: { type: 'Book' } }, 
  { Attr: ['productName', 'size', 'price'], Consistent: true },
});

#GetItems

###Description GetItem returns Items given their primary keys.

The items array together with the key create the "Keys" used in the BatchGet operation
Example: given an array of Freelancers and a key { pKey: 'uuid' } means that the uuid property value will be used to create the Keys being used in the batchGet request. Here we do assume that Freelancers does indeed have a uuid property.
Example: given an array of strings or numbers we assume that those are the partition keys values to search for. A list of uuids would be an example of this

###Params

table: Table to retrieve items from
items: Array of objects, string or number to be used to construct the keys
key: Name of the partition key amd optional sort key
options?: Additional options

  • Attr?: Item attributes to return
  • Consistent?: False (default): eventual consistency read, True: strong consistency read

###Returns:

Promise: List of retrieved items

public async getItems(table: string, items: BatchItems, key: KeyAttributes, options?: GetItemOptions): Promise<ItemList> {}
// Most simple example

const freelancers = [
    { uuid: '123', 'name': 'Henk', ... },
    { uuid: '456', 'name': 'Bob', ... },
    { uuid: '789', 'name': 'Ponk', ... },
    ...,
]
    
await client.getItems('Invoice', freelancers, { pKey: 'uuid' });
// Example with advanced options

const freelancerUUID = ['123', '456', '789', ... ]
    
await client.getItems(
    'Invoice', 
    freelancers, 
    { pKey: 'uuid' },
    { Attr: ['invoiceNumber', 'creditor', 'debtor'], Consistent: true }
);

#PutItem

Description

Delete a single item given the key

Params

table: Table to retrieve item from
key: Partition key and optional sort key
condition: Condition statement which decides whether to proceed with the put operation

Returns

Promise

#PutItems

Description

Params

Returns

#DeleteItem

Description

Params

Returns

#DeleteItems

Description

Params

Returns

#Update

Description

Params

Returns

#Query

Description

Params

Returns

#Scan

Description

Params

Returns

#Execute

Description

Params

Returns

###How to define DynamoDB Expressions DynamoDB has various expressions like: ConditionExpression, ProjectionExpression, KeyConditionExpression, FilterExpression In DynamoLight defining them has been simplefied so you don't have to worry about the ExpressionAttributeValues & ExpressionAttributeNames

Condition

Condition in DynamoLight is used to define KeyConditionExpressions and ConditionExpressions. A Condition has the following structure:

interface Condition { condition: string; values: { [ key: string]: unknown } }

condition is a string where attributes are defined with a # and values with a :
See the aws docs for the possible function and operators which can be used
Example: #ProductCategory IN (:cat1, :cat2)) and (#Price between :lo and :hi)
values is an object which defines the values in condition
Example: { cat: 'Electronics', cat2: 'Books', lo: 100, hi: 200 }

Action

Action in DynamoDB is used to simplify the UpdateExpression in the update operation. There a for different Operations which can be perfomed:

  • SET
    • ATTRIBUTE: Adds an attribute and value to an item. Overrides the attribute value if it already exists.
      { SET: { ATTRIBUTE: { name: 'Bob' }}}
    • ATTRIBUTESET: Adds attribute of type SET to an item.
      { SET: { ATTRIBUTESET: { numbers: [1,2,3] }}}
    • LISTAPPENDSTART: Append list of items to the start of an existing list attribute
      { SET: { LISTAPPENDSTART: { numbers: [1,2] }}}
    • LISTAPPENDEND: Append list of items to the end of an existing list attribute
      { SET: { LISTAPPENDEND: { numbers: [5,6] }}}
    • IFNOTEXISTS: Add attribute and value to Item only if attribute does not exist yet
      { SET: { IFNOTEXISTS: { number: 5 }}}
    • ADDITION: Perform numeric addition on existing number attribute
      { SET: { ADDITION: { count: 1 }}}
    • SUBTRACTION: Perform numeric subtraction on existing number attribute
      { SET: { SUBTRACTION: { 'count': 1 }}}

  • REMOVE: Remove one of more attributes from an item
    ['middleName', 'IBAN']
  • ADD: Add one or more elements to a SET. Add can only be used on an attribute of type SET
    { ADD: { numbers: [5,6,7] }}
  • DELETE: Delete one or more elements from a SET. DELETE can only be performed on an attribute of type SET
    { DELETE: { numbers: [5,6,7] }}
1.3.2

2 years ago

1.3.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.1.10

2 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.2.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.0

4 years ago