Skip to content

Commit

Permalink
Expand return types of key generators
Browse files Browse the repository at this point in the history
PK and SK values can be any _scalar_ value, not just strings.

Therefore change return type of PK, SK, and GSI PK + SK, generator functions to `NativeScalarAttributeValue`

Thanks to @ghbuck for issue #2
  • Loading branch information
mikebroberts committed Nov 12, 2023
1 parent 630306c commit c41a1e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions documentation/Entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Each _Entity_ object must satisfy the [`Entity` interface](https://symphoniaclou
```typescript
interface Entity<TItem extends TPKSource & TSKSource, TPKSource, TSKSource> {
type: string
pk(source: TPKSource): string
sk(source: TSKSource): string
pk(source: TPKSource): NativeScalarAttributeValue
sk(source: TSKSource): NativeScalarAttributeValue
convertToDynamoFormat?: EntityFormatter<TItem>
parse: EntityParser<TItem>
gsis?: Record<string, GsiGenerators>
Expand Down Expand Up @@ -86,7 +86,7 @@ This occurs during many operations, including `put` and `get`.
> If your table only has a Partition Key, and doesn't have a Sort Key, see the section _PK-only Entities_ below.
Each of `pk()` and `sk()` is passed an argument of type `TPKSource` or `TSKSource`, as defined earlier, and should return a string.
Each of `pk()` and `sk()` is passed an argument of type `TPKSource` or `TSKSource`, as defined earlier, and should return a "scalar" value (specifically [`NativeScalarAttributeValue`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-util-dynamodb/TypeAlias/NativeScalarAttributeValue/)).
Let's go back to our example of `Sheep` from earlier. Let's say we have a particular sheep object that is internally represented as follows:
Expand Down
10 changes: 5 additions & 5 deletions src/lib/entities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NativeAttributeValue } from '@aws-sdk/util-dynamodb/dist-types/models'
import { NativeAttributeValue, NativeScalarAttributeValue } from '@aws-sdk/util-dynamodb/dist-types/models'

export type DynamoDBValues = Record<string, NativeAttributeValue>

Expand Down Expand Up @@ -76,7 +76,7 @@ export interface Entity<TItem extends TPKSource & TSKSource, TPKSource, TSKSourc
* often be some kind of encoding of one more fields
* @param source
*/
pk(source: TPKSource): string
pk(source: TPKSource): NativeScalarAttributeValue

/**
* Given a subset of the fields of the entity type, generate the sort key value for the stored version of the object.
Expand All @@ -87,7 +87,7 @@ export interface Entity<TItem extends TPKSource & TSKSource, TPKSource, TSKSourc
* often be some kind of encoding of one more fields
* @param source
*/
sk(source: TSKSource): string
sk(source: TSKSource): NativeScalarAttributeValue

/**
* A map of GSI ID to generator functions, similar to pk() and sk()
Expand All @@ -104,7 +104,7 @@ export type PKOnlyEntity<TItem extends TPKSource, TPKSource> = Omit<Entity<TItem

export interface GsiGenerators {
// ToMaybe - better types for these?
pk(source: unknown): string
pk(source: unknown): NativeScalarAttributeValue

sk?(source: unknown): string
sk?(source: unknown): NativeScalarAttributeValue
}

0 comments on commit c41a1e3

Please sign in to comment.