-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for dynamic attributes
Finally added support for dynamic attributes. You must give the people what they want. Fixes #669, #628, #638
- Loading branch information
1 parent
5e66e54
commit 423cbd0
Showing
8 changed files
with
144 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect } from 'chai' | ||
import { TestableTable } from '../../setup-tests.spec' | ||
|
||
describe('AttributeType/Dynamic', () => { | ||
let record: TestableTable | ||
|
||
beforeEach(() => { | ||
record = TestableTable.new() | ||
}) | ||
|
||
it('should store values based on the object type', () => { | ||
expect(record.dynamic).eq(null) | ||
record.dynamic = 'some value' | ||
expect(record.dynamic).eq('some value') | ||
expect(record.getAttributeDynamoValue('dynamic')).to.deep.eq({ S: 'some value' }) | ||
record.dynamic = 150 | ||
expect(record.dynamic).eq(150) | ||
expect(record.getAttributeDynamoValue('dynamic')).to.deep.eq({ N: '150' }) | ||
}) | ||
|
||
it('should support dynamic maps', () => { | ||
expect(record.dynamic).eq(null) | ||
record.dynamic = { a: 'A', b: 'B' } as any | ||
expect(record.dynamic).deep.eq({ a: 'A', b: 'B' }) | ||
expect(record.getAttributeDynamoValue('dynamic')).to.deep.eq({ M: { a: { S: 'A' }, b: { S: 'B' } } }) | ||
}) | ||
|
||
it('should support dynamic lists', () => { | ||
expect(record.dynamic).eq(null) | ||
record.dynamic = [1, 2, 3] as any | ||
expect(record.dynamic).deep.eq([1, 2, 3]) | ||
expect(record.getAttributeDynamoValue('dynamic')).to.deep.eq({ L: [{ N: '1' }, { N: '2' }, { N: '3' }] }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { type AttributeValue } from '@aws-sdk/client-dynamodb' | ||
import { type IAttributeType } from '../../interfaces' | ||
import { type DynamicAttributeValue, type DynamicAttributeMetadata } from '../../metadata/attribute-types/dynamic.metadata' | ||
import { type Table } from '../../table' | ||
import { AttributeType } from '../../tables/attribute-type' | ||
import { marshall, unmarshall } from '@aws-sdk/util-dynamodb' | ||
import { DynamoAttributeType } from '../../dynamo-attribute-types' | ||
|
||
export class DynamicAttributeType extends AttributeType<DynamicAttributeValue, DynamicAttributeMetadata> | ||
implements IAttributeType<DynamicAttributeValue> { | ||
/** | ||
* Dynamic types can be any type, but we set it to string. | ||
* This is really only used when creating indexes and the type needs to be specified. | ||
*/ | ||
type = DynamoAttributeType.String | ||
|
||
constructor(record: Table, propertyName: string, protected metadata?: DynamicAttributeMetadata) { | ||
super(record, propertyName, metadata) | ||
} | ||
|
||
toDynamo(value: DynamicAttributeValue): AttributeValue { | ||
const marshallOptions = { | ||
convertEmptyValues: true, | ||
removeUndefinedValues: true, | ||
...this.metadata?.marshallOptions ?? {}, | ||
} | ||
|
||
return marshall({ value }, marshallOptions).value | ||
} | ||
|
||
fromDynamo(attributeValue: AttributeValue): DynamicAttributeValue { | ||
return unmarshall({ value: attributeValue }, this.metadata?.unmarshallOptions).value | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { type marshallOptions, type unmarshallOptions } from '@aws-sdk/util-dynamodb' | ||
import { type AttributeMetadata } from '../attribute' | ||
|
||
export type DynamicAttributeValue = any | ||
|
||
export interface DynamicAttributeMetadata extends AttributeMetadata<DynamicAttributeValue> { | ||
marshallOptions?: marshallOptions | ||
unmarshallOptions?: unmarshallOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters