Skip to content

Commit

Permalink
feat: accept number and string arrays on set properties
Browse files Browse the repository at this point in the history
Updated to accept arrays for sets when using Table.new and Table.set
methods.
  • Loading branch information
benhutchins committed Sep 14, 2023
1 parent ae04049 commit 72b57cc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/decorator/attribute-types/number-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ describe('AttributeType/NumberSet', () => {
record = new TestableTable()
})

it('should store values as an array of numbers', () => {
it('should accept setting values as a Set', () => {
expect(record.testNumberSet).eq(null)
record.testNumberSet = new Set([10, 100])
expect(Array.from(record.testNumberSet)).deep.eq([10, 100])
expect(Array.from(record.get('testNumberSet')!)).deep.eq([10, 100])
expect(record.getAttributeDynamoValue('testNumberSet')).deep.eq({ NS: ['10', '100'] })
})

it('should support setting values as an Array using Table.set', () => {
record.set('testNumberSet', [10, 100])
expect(Array.from(record.testNumberSet!)).deep.eq([10, 100])
expect(Array.from(record.get('testNumberSet')!)).deep.eq([10, 100])
expect(record.getAttributeDynamoValue('testNumberSet')).deep.eq({ NS: ['10', '100'] })
})

it('supports BigInt values', () => {
const int = BigInt('9007199254740991')
expect(record.testNumberSet).eq(null)
Expand Down
2 changes: 1 addition & 1 deletion src/decorator/attribute-types/string-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('AttributeType/StringSet', () => {
expect(Array.from(record.testStringSet)).deep.eq(['some value'])
})

it('should allow values to be an Array', () => {
it('should allow values to be set as an Array using Table.set', () => {
expect(record.testStringSet).eq(null)
record.set('testStringSet', ['some value'])
expect(Array.from(record.testStringSet)).deep.eq(['some value'])
Expand Down
2 changes: 1 addition & 1 deletion src/setup-tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class TestableTable extends Dyngoose.Table {
public testNumber: number

@Dyngoose.Attribute.NumberSet()
public testNumberSet?: Set<bigint | number> | null
public testNumberSet: Set<bigint | number> | null

@Dyngoose.Attribute.NumberSet({ default: () => new Set([42, 420]) })
public testNumberSetWithDefaults: Set<number>
Expand Down
6 changes: 5 additions & 1 deletion src/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe('Table', () => {

describe('.remove', () => {
it('should allow attributes to be removed', async () => {
const card = TestableTable.new()
const card = TestableTable.new({
testNumberSet: [1, 2, 3],
})
card.id = 101
card.title = '101'
card.generic = 'something'
Expand All @@ -46,6 +48,7 @@ describe('Table', () => {
expect(reloadedCard.generic).to.eq(null)
expect(reloadedCard.defaultedString).to.eq('SomeDefault')
expect(reloadedCard.testString).to.eq('value is set')
expect(reloadedCard.testNumberSet).to.deep.eq(new Set([1, 2, 3]))
}
})
})
Expand All @@ -57,6 +60,7 @@ describe('Table', () => {
testString: 'some value',
testNumber: 11,
testNumberSet: new Set([1, 2, 3]),
testStringSet: ['1', '2', '3'],
testAttributeNaming: 'test',
})
await card.save()
Expand Down
4 changes: 2 additions & 2 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createTable } from './tables/create-table'
import { deleteTable } from './tables/delete-table'
import { describeTable } from './tables/describe-table'
import { migrateTable } from './tables/migrate-table'
import { type TableProperties, type TableProperty } from './tables/properties'
import { type TablePropertyValue, type TableProperties, type TableProperty } from './tables/properties'
import { Schema } from './tables/schema'
import { isTrulyEmpty } from './utils/truly-empty'

Expand Down Expand Up @@ -484,7 +484,7 @@ export class Table {
* @see {@link Table.setAttribute} To set an attribute value by an attribute name.
* @see {@link Table.setAttributes} To set several attribute values by attribute names.
*/
public set<P extends TableProperty<this>>(propertyName: P | string, value: this[P] extends Set<infer E> ? Set<E> | E[] : this[P], params?: SetPropParams): this {
public set<P extends TableProperty<this>>(propertyName: P | string, value: TablePropertyValue<this[P]>, params?: SetPropParams): this {
const attribute = this.table.schema.getAttributeByPropertyName(propertyName as string)
return this.setByAttribute(attribute, value, params)
}
Expand Down
8 changes: 7 additions & 1 deletion src/tables/properties.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { type NumberSetValue } from '../metadata/attribute-types/number-set.metadata'
import { type StringSetValue } from '../metadata/attribute-types/string-set.metadata'
import { type Table } from '../table'

export type TableProperty<T> = Exclude<keyof T, keyof Table>

export type TablePropertyValue<P> =
P extends NumberSetValue ? NumberSetValue :
P extends StringSetValue ? StringSetValue : P

export type TableProperties<T> = {
[key in TableProperty<T>]?: T[key]
[key in TableProperty<T>]?: TablePropertyValue<T[key]>
}

0 comments on commit 72b57cc

Please sign in to comment.