2.2.2 • Published 5 months ago

rivia-dynamodb v2.2.2

Weekly downloads
-
License
ISC
Repository
bitbucket
Last release
5 months ago

DynamoDB Wrapper

A simple and efficient wrapper for AWS DynamoDB operations.

Features

  • Simplified CRUD operations
  • Support for consistent reads
  • Batch operations
  • Query and scan operations with pagination
  • Filter expressions
  • Sort key prefix queries
  • Sort key range queries (between)
  • Consumed capacity tracking
  • Configurable primary and sort key field names

Installation

npm install @rivia/dynamo

Configuration

The wrapper can be configured through environment variables or programmatically:

Environment Variables

REGION=us-east-1
TABLE_NAME=your-table-name
DYNAMODB_PK_FIELD_NAME=pk
DYNAMODB_SK_FIELD_NAME=sk
DYNAMODB_CONSISTENT_READ=false
DYNAMODB_RETURN_CONSUMED_CAPACITY=false

Programmatic Configuration

const Dynamo = require('@rivia/dynamo');

const dynamo = new Dynamo();
dynamo.set({
    region: 'us-east-1',
    tableName: 'your-table-name',
    pkField: 'pk',
    skField: 'sk',
    consistentRead: true,
    returnConsumedCapacity: true
});

Usage

Basic Operations

// Insert
await dynamo.insert({ pk: 'user#1', sk: 'profile', name: 'John' });

// Find
const { item, consumedCapacity } = await dynamo.find('user#1', 'profile');

// Update
await dynamo.update('user#1', 'profile', { name: 'John Doe' });

// Remove
await dynamo.remove('user#1', 'profile');

Query Operations

// Query with filters
const { items, count, nextPageId, consumedCapacity } = await dynamo.query(
    'user#1',
    [{ name: 'status', value: 'active' }],
    null,
    10,
    { consistentRead: true }
);

// Query by prefix
const { items } = await dynamo.findByPrefix(
    'user#1',
    'order#',
    [{ name: 'status', value: 'pending' }]
);

// Query by range (between)
const { items } = await dynamo.findByBetween(
    'user#1',
    'order#2023-01-01',
    'order#2023-12-31',
    [{ name: 'status', value: 'completed' }]
);

// Get all items
const { items, count } = await dynamo.getAll('user#1');

Batch Operations

// Batch insert
await dynamo.batchInsert([
    { pk: 'user#1', sk: 'order#1', status: 'pending' },
    { pk: 'user#1', sk: 'order#2', status: 'completed' }
]);

Scan Operations

// Scan with attributes
const { items, consumedCapacity } = await dynamo.scanTable(
    10,
    ['pk', 'sk', 'status'],
    { consistentRead: true }
);

Consistency and Performance

Consistent Reads

By default, DynamoDB uses eventually consistent reads for better performance. However, you can enable strongly consistent reads when needed:

  1. Globally through configuration:
dynamo.set({ consistentRead: true });
  1. Per-operation through options:
const { item } = await dynamo.find('user#1', 'profile', { consistentRead: true });

Consumed Capacity

You can track the consumed capacity of DynamoDB operations:

  1. Globally through configuration:
dynamo.set({ returnConsumedCapacity: true });
  1. The consumed capacity will be returned in the operation results:
const { item, consumedCapacity } = await dynamo.find('user#1', 'profile');
console.log('Consumed capacity:', consumedCapacity);

Filter Expressions

The wrapper supports various filter expressions:

// Equality
{ name: 'status', value: 'active' }

// Contains
{ name: 'tags', value: 'important', operator: 'contains' }

// Custom operators
{ name: 'age', value: 18, operator: '>' }

Error Handling

The wrapper throws errors for:

  • Invalid value types
  • Missing required parameters
  • DynamoDB operation failures
try {
    await dynamo.insert({ pk: 'user#1' }); // Missing sk
} catch (error) {
    console.error('Operation failed:', error);
}

License

MIT

2.2.1

5 months ago

2.0.3

8 months ago

2.2.0

5 months ago

2.1.1

5 months ago

2.0.2

8 months ago

2.0.5

8 months ago

2.2.2

5 months ago

2.0.4

8 months ago

2.0.7

8 months ago

2.0.6

8 months ago

2.0.9

6 months ago

2.0.8

8 months ago

2.1.0

5 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.11.0

8 months ago

1.10.4

11 months ago

1.10.3

11 months ago

1.10.2

11 months ago

1.10.1

11 months ago

1.10.0

11 months ago

1.9.1

1 year ago

1.9.0

1 year ago

1.8.2

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.0

1 year ago

1.6.0

1 year ago

1.5.0

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago