npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dynamomo

v0.1.0-beta.6

Published

Dynamodb query helper

Downloads

155

Readme

Dynamomo


An interface for AWS DynamoDB with helper functions

npm install --save dynamomo 

Module level

  1. config - configure the table with stage or other variables
  2. client - a DynamoDB client
  3. getPolicy - get an output of what policy your lambda will need to interface with dynamodb

Table Instance

  1. client - a DynamoDB client
  2. getTableName - get the object's table name
  3. scan - get all records for a table
  4. getAll - alias for scan
  5. getById - get a record by the primary key
  6. getAllById - get a records from a list of primary keys
  7. updateById - update a record by the primary key
  8. deleteById - delete a record by the primary key
  9. getByKey - like getById, but allows any Key configuration to be set
  10. update - update a record by calling client.update()
  11. query - DynamoDB query with provided params

Usage


// import
import dynamomo from 'dynamomo'

const myDynamomo = dynamomo({ region: 'us-east-1' })

const itemsTable = myDynamomo.create('items')
const myItem = itemsTable.getById(1)

config({ tablePrefix, debug })


// import
import dynamomo from 'dynamomo'

// Configure dynamomo with table prefix and logging 
dynamomo.config({
  debug: true,         // outputs dynamodb usage information as you query

  tablePrefix: 'prod', // takes care of prefixing for tables.
                       // eg: table name 'items' will be 'prod-items' 
})

create(tableName, { primaryKey, indexName })


Create a new table instance. This does not "create" the table. Only the binding to the table.

Options available

  1. primaryKey - defaults to Id if not specified
  2. indexName - table index to be used with query
// Access the root table by name. For the prod-items table, just user the name items 
// This will handle prefixing for prod-items, int-items, and dev-items on it's own
const items = dynamomo.create('items')

// With a specified primary key
const items = dynamomo.create('items', { primaryKey; 'ItemPublicKey' })

// With a specified index key
const items = dynamomo.create('items', { indexName; 'ItemPublicKey-index' })

getById(id)


Retrieve a record using the Id field of the table, or other named Id field

NOTE: If item is not found, the promise will resolve successfully with a response of undefined

// Uses the primary key Id by default
items.getById(1)

getAllById(idArray)


Retrieve all the records of a table from an array of Ids. Uses DynamoDB batch get to retrieve the records. DyanamoDB limits the result to 100 records, so if more than 100 IDs are requested, the function will make a separate request for every set of 100 IDs.

"Yo dawg, I heard you like batch requests. So we put a batch request on your batch request so you can get all your records while getting some records"

items.getAllById([1, 2, 3, 4])

updateById(id, attributes, addParams)


Update a record's data by specifying it's Id and the attributes to update. Provide additional dynamo client parameters as needed

const id = 1
const updateKeys = { EmailAddress: '[email protected]' }
const addParams = { ReturnValues: 'UPDATED_NEW' }

items.updateById(id, updateKeys, addParams)

queryByKeys(keys, addParams)


Get all records that match the list of keys provided. This often requires the use of the proper index of the table. The params will be packaged into the KeyConditionExpression DynamoDB needs to make the request.

const keys = { categoryName: 'toys' }
const addParams = { IndexName: 'CategoryName-index' }

items.queryByKeys(keys, addParams)

deleteById(id, addParams)


Delete a record's data by specifying its Id. Provide additional dynamo client parameters as needed

items.deleteById(1)

getAll(scanParams) - Alias for scan


Retrieve all the records for a table. This handles the recursive actions needed for DynamoDB to get all records

items.getAll() // alias for scan

query(params)


DynamoDB query operation.

items.query(params)

update(params)


DynamoDB update operation.

items.update(dynamoUpdateParams)

scan(params)


DyanamoDB scan operation. This handles the recursive actions needed for DynamoDB to get all records.

items.scan(params)

MaxLimit - special limit config property

Any batch commands such as scan or query can take a specialized MaxLimit property to control the amount of records pulled. This is different than the Limit property used by Dynamodb. MaxLimit will make recursive calls until it retrieves the MaxLimit value, or if it reaches the end of the table rows.

Since MaxLimit is not a property allowed by Dynamodb, the property is removed from the config when the passed to the Dynamodb client.

Example with getAll/scan

const itemList = await items.getAll({ 
  MaxLimit: 300, 
  ProjectionExpression: 'ItemName, Category' 
})

// the items result will be an object
{
  Items: [ ... ], // 300 items from the database
  LastEvaluatedKey: { Id: 123 }, // Where the query ended when it reached the limit. LastEvaluatedKey can change as the table size changes.
  RowCount: 300 // Total rows return 
}

Example with queryByKeys

const itemList = await items.queryByKeys({
  CategoryId: '2'
}, { 
  MaxLimit: 2, 
  ProjectionExpression: 'ItemName, Category' 
})