1.0.0 • Published 6 years ago

@repit/lambda-query v1.0.0

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

Lambda Query

Simple & Opinionated DynamoDB Query API for AWS Lambda.

Installation

$ npm install @repit/lambda-query --save

Usage

'use strict'

// Load the Query class.
const Query = require('@repit/lambda-query')

// Create a new Query object.
const query = new Query()

// Configure AWS DynamoDB.
// Note: This is not required if you add proper IAM roles.
query.configure({
  region: '',
  accessKeyId: '',
  secretAccessKey: ''
})

// Change default table name.
// Note: Default table name is `Store`.
query.tableName('')

// Get current instance of `DocumentClient`.
query.client()

// Read a single item from DynamoDB which match: { Type: 'Error', Name: 'InvalidUser' },
// and include all attributes.
query.resolve('Error:InvalidUser:*') // Promise

// Read a single item from DynamoDB which match: { Type: 'Error', Name: 'InvalidUser' },
// and include only `en,fr,de` attributes.
query.resolve('Error:InvalidUser:en,fr,de') // Promise

// Read many items from DynamoDB which match: { Type: 'Error' },
// and include all attributes.
query.resolve('Error:*:*') // Promise

// Read many items from DynamoDB which match: { Type: 'Error' },
// and include only `en,fr,de` attributes.
query.resolve('Error:*:en,fr,de') // Promise

// Read many items from DynamoDB which match:
// { Type: 'Error', Name: 'InvalidUser' } and { Type: 'Error', Name: 'InvalidEmail' },
// and include only `en,fr,de` attributes.
query.resolve([
  'Error:InvalidUser:en',
  'Error:InvalidEmail:fr,de'
]) // Promise

Caching

This package requires @repit/lambda-cache to cache queries.

Usage is pretty simple:

'use strict'

const Query = require('@repit/lambda-query')
const Cache = require('@repit/lambda-cache')

const cache = new Cache()

exports.handler = (event, context, callback) => {
  const query = new Query(cache)
}

To learn how to invalidate cache visit: @repit/lambda-cache

Table Structure

Default table name is Store, which can be changed with query.tableName(name).

Primary key requirements:

  • Partition Key should be Type (String);
  • Sort Key should be Name (String);

License

MIT