Skip to content

Single Table

SingleTable provides table configuration and reduces boilerplate for single-table designs. Create one instance per table.

Overview

SingleTable is the second layer that adds:

  • Table Configuration: Centralized setup for table name, keys, and indexes
  • Key Management: Automatic partition and sort key handling
  • Type System: Entity type tracking via typeIndex
  • Property Cleanup: Removes internal keys from returned items
  • TTL Support: Built-in expiration handling
  • Index Support: Simplified secondary index operations

For complex entity relationships, consider the Schema layer.

Requirements

Requires a DynamodbProvider instance:

typescript
import { DynamodbProvider, SingleTable } from 'dynamodb-provider';

const provider = new DynamodbProvider({
  dynamoDB: {
    target: 'v3',
    instance: documentClient,
    commands: { /* ... */ }
  }
});

const table = new SingleTable({
  dynamodbProvider: provider,
  table: 'YOUR_TABLE_NAME',
  partitionKey: 'pk',
  rangeKey: 'sk'
});

Quick Example

typescript
const table = new SingleTable({
  dynamodbProvider: provider,
  table: 'AppData',
  partitionKey: 'pk',
  rangeKey: 'sk',
  keySeparator: '#',
  typeIndex: {
    name: 'TypeIndex',
    partitionKey: '_type',
    rangeKey: '_timestamp'
  }
});

// Create a user
await table.create({
  key: {
    partitionKey: ['USER', '12345'],
    rangeKey: '#DATA'
  },
  item: {
    userId: '12345',
    name: 'John Doe',
    email: 'john@example.com'
  },
  type: 'USER'
});

// Get the user
const user = await table.get({
  partitionKey: ['USER', '12345'],
  rangeKey: '#DATA'
});
// Returns: { userId: '12345', name: 'John Doe', email: 'john@example.com' }
// (pk, sk, _type, _timestamp automatically removed)

// Query users
const { items } = await table.query({
  partition: ['USER', '12345'],
  range: {
    operation: 'begins_with',
    value: 'ORDER#'
  }
});

Key Features

Automatic Key Management

typescript
// Array keys are automatically joined with keySeparator
await table.create({
  key: {
    partitionKey: ['USER', userId],  // Becomes: USER#userId
    rangeKey: ['ORDER', orderId]     // Becomes: ORDER#orderId
  },
  item: { /* ... */ }
});

Property Cleanup

Internal properties are removed from results:

typescript
// In DynamoDB:
{
  pk: 'USER#123',
  sk: '#DATA',
  _type: 'USER',
  _timestamp: '2024-01-15T10:30:00Z',
  userId: '123',
  name: 'John'
}

// Returned to you:
{
  userId: '123',
  name: 'John'
}

Type-Based Queries

With typeIndex configured:

typescript
// Get all users
const users = await table.listAllFromType('USER');

// Query users with pagination
const { items, paginationToken } = await table.listType({
  type: 'USER',
  limit: 100
});

Available Methods

Data Operations

  • get - Retrieve item by keys
  • batchGet - Retrieve multiple items
  • create - Create item with automatic key handling
  • update - Update item with index management
  • delete - Delete item

Query Operations

Batch Operations

Helpers

Benefits Over Provider

FeatureProviderSingleTable
Table nameSpecified per callConfigured once
Key columnsManualAutomatic
Key joiningManualAutomatic (keySeparator)
Property cleanupManualAutomatic
Type trackingManualBuilt-in (typeIndex)
TTL handlingManualBuilt-in (expiresAt)
Index managementManualConfigured once

Next Steps

  1. Configuration - Set up SingleTable with all options
  2. Methods - Learn about operations
  3. Schema - For advanced entity modeling
  4. Examples - See complete patterns

See Also