sam-goodwin / punchcard

Type-safe AWS infrastructure.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

consolidate HashTable and SortedTable

sam-goodwin opened this issue · comments

commented

We currently have two implementations of a DynamoDB Table: HashTable and SortedTable and based on feedback, this is "leaky" and potentially confusing.

The primary reason for the two separate implementations is that their key shape and client interfaces are different - if you provide a sort key, then query API calls are also possible. But, mapped types could be used to compute the same results without requiring the subclasses.

There is already the Table abstract class that generalizes most of the logic, we would have to adapt it to support both use-cases:

abstract class Table<C extends TableClient<S, K>, S extends Shape, K extends Shape>

There are two approaches for representing a hash/sorted table as a single Table type:

Given a table shape:

const myShape = {
  key: string(),
  count: integer()
}

We could have two type parameters, one for the partition key and one for the sorted key:

// HashTable
Table<typeof MyShape, 'key', undefined>

// SortedTable
Table<typeof MyShape, 'key', 'count'>

Or have a type that represents the different keys:

// HashTable
Table<typeof MyShape, HashKey<'key'>>

// SortedTable
Table<typeof MyShape, CompositeKey<'key', 'count'>>