From 72b57cc75ab0aefcb18f54af31835dbd7159890d Mon Sep 17 00:00:00 2001 From: Benjamin Hutchins Date: Thu, 14 Sep 2023 14:56:29 -0400 Subject: [PATCH] feat: accept number and string arrays on set properties Updated to accept arrays for sets when using Table.new and Table.set methods. --- src/decorator/attribute-types/number-set.spec.ts | 9 ++++++++- src/decorator/attribute-types/string-set.spec.ts | 2 +- src/setup-tests.spec.ts | 2 +- src/table.spec.ts | 6 +++++- src/table.ts | 4 ++-- src/tables/properties.ts | 8 +++++++- 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/decorator/attribute-types/number-set.spec.ts b/src/decorator/attribute-types/number-set.spec.ts index bd7e62d1..fb195687 100644 --- a/src/decorator/attribute-types/number-set.spec.ts +++ b/src/decorator/attribute-types/number-set.spec.ts @@ -8,7 +8,7 @@ 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]) @@ -16,6 +16,13 @@ describe('AttributeType/NumberSet', () => { 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) diff --git a/src/decorator/attribute-types/string-set.spec.ts b/src/decorator/attribute-types/string-set.spec.ts index dc1a3e54..142f7c29 100644 --- a/src/decorator/attribute-types/string-set.spec.ts +++ b/src/decorator/attribute-types/string-set.spec.ts @@ -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']) diff --git a/src/setup-tests.spec.ts b/src/setup-tests.spec.ts index a8900910..7870bcf2 100644 --- a/src/setup-tests.spec.ts +++ b/src/setup-tests.spec.ts @@ -69,7 +69,7 @@ export class TestableTable extends Dyngoose.Table { public testNumber: number @Dyngoose.Attribute.NumberSet() - public testNumberSet?: Set | null + public testNumberSet: Set | null @Dyngoose.Attribute.NumberSet({ default: () => new Set([42, 420]) }) public testNumberSetWithDefaults: Set diff --git a/src/table.spec.ts b/src/table.spec.ts index db95e418..0c313cfc 100644 --- a/src/table.spec.ts +++ b/src/table.spec.ts @@ -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' @@ -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])) } }) }) @@ -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() diff --git a/src/table.ts b/src/table.ts index bc1f7ffe..d48e47f1 100644 --- a/src/table.ts +++ b/src/table.ts @@ -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' @@ -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

>(propertyName: P | string, value: this[P] extends Set ? Set | E[] : this[P], params?: SetPropParams): this { + public set

>(propertyName: P | string, value: TablePropertyValue, params?: SetPropParams): this { const attribute = this.table.schema.getAttributeByPropertyName(propertyName as string) return this.setByAttribute(attribute, value, params) } diff --git a/src/tables/properties.ts b/src/tables/properties.ts index a72d7962..e84b28eb 100644 --- a/src/tables/properties.ts +++ b/src/tables/properties.ts @@ -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 = Exclude +export type TablePropertyValue

= + P extends NumberSetValue ? NumberSetValue : + P extends StringSetValue ? StringSetValue : P + export type TableProperties = { - [key in TableProperty]?: T[key] + [key in TableProperty]?: TablePropertyValue }