From a1c563c4573c5ccb75c1c0f0af475e8d7a925977 Mon Sep 17 00:00:00 2001 From: Etienne Noel Date: Thu, 1 Feb 2024 19:52:48 -0800 Subject: [PATCH] - Updated to use pristine-metadata --- package-lock.json | 14 +++--- package.json | 2 +- .../src/attribute.spec.ts | 44 +++++++++---------- .../src/attribute.ts | 6 ++- 4 files changed, 34 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3d3a3118..137c3c1f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ ], "dependencies": { "@aws-sdk/client-dynamodb": "^3.10.0", - "@pristine-ts/metadata": "^1.0.6", + "@pristine-ts/metadata": "^1.0.7", "reflect-metadata": "^0.1.13" }, "devDependencies": { @@ -3942,9 +3942,9 @@ } }, "node_modules/@pristine-ts/metadata": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@pristine-ts/metadata/-/metadata-1.0.6.tgz", - "integrity": "sha512-gwg798vs13PHzZgLtj0Pyq7MJg11NthMYMW3kn3gxSIBQQQiwFRPFqBUe6pTDuwS5b5ytnS7kJxOqlksa2Xxlw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@pristine-ts/metadata/-/metadata-1.0.7.tgz", + "integrity": "sha512-sPrWeZu+l3XZ7WoUWrz5tWrc23qeqt8w/HtX/n77I+tK9AeLXrhkF3I8daSlOmPktduR2fMfhy9nV36fAR2wMw==", "dependencies": { "reflect-metadata": "^0.2.1" } @@ -17410,9 +17410,9 @@ } }, "@pristine-ts/metadata": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@pristine-ts/metadata/-/metadata-1.0.6.tgz", - "integrity": "sha512-gwg798vs13PHzZgLtj0Pyq7MJg11NthMYMW3kn3gxSIBQQQiwFRPFqBUe6pTDuwS5b5ytnS7kJxOqlksa2Xxlw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/@pristine-ts/metadata/-/metadata-1.0.7.tgz", + "integrity": "sha512-sPrWeZu+l3XZ7WoUWrz5tWrc23qeqt8w/HtX/n77I+tK9AeLXrhkF3I8daSlOmPktduR2fMfhy9nV36fAR2wMw==", "requires": { "reflect-metadata": "^0.2.1" }, diff --git a/package.json b/package.json index f8c0686a..bff38d00 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ }, "dependencies": { "@aws-sdk/client-dynamodb": "^3.10.0", - "@pristine-ts/metadata": "^1.0.6", + "@pristine-ts/metadata": "^1.0.7", "reflect-metadata": "^0.1.13" }, "peerDependencies": { diff --git a/packages/dynamodb-data-mapper-annotations/src/attribute.spec.ts b/packages/dynamodb-data-mapper-annotations/src/attribute.spec.ts index 9b9c18dc..56636f5c 100644 --- a/packages/dynamodb-data-mapper-annotations/src/attribute.spec.ts +++ b/packages/dynamodb-data-mapper-annotations/src/attribute.spec.ts @@ -143,7 +143,7 @@ describe('attribute', () => { decorator(target, 'property'); const reflectionCalls = (Reflect.getMetadata as any).mock.calls; - expect(reflectionCalls.length).toBe(1); + expect(reflectionCalls.length).toBe(2); expect(reflectionCalls[0][0]).toBe(METADATA_TYPE_KEY); expect(reflectionCalls[0][1]).toBe(target); expect(reflectionCalls[0][2]).toBe('property'); @@ -153,7 +153,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of String as a string`, () => { - (Reflect.getMetadata as any).mockImplementation(() => String); + (Reflect.getMetadata as any).mockImplementationOnce(() => String); const decorator = attribute(); const target = Object.create(null); @@ -167,7 +167,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of Number as a number`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Number); + (Reflect.getMetadata as any).mockImplementationOnce(() => Number); const decorator = attribute(); const target = Object.create(null); @@ -181,7 +181,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of Boolean as a boolean`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Boolean); + (Reflect.getMetadata as any).mockImplementationOnce(() => Boolean); const decorator = attribute(); const target = Object.create(null); @@ -195,7 +195,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of Date as a date`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Date); + (Reflect.getMetadata as any).mockImplementationOnce(() => Date); const decorator = attribute(); const target = Object.create(null); @@ -210,7 +210,7 @@ describe('attribute', () => { `should recognize values with a constructor that subclasses Date as a date`, () => { class MyDate extends Date {} - (Reflect.getMetadata as any).mockImplementation(() => MyDate); + (Reflect.getMetadata as any).mockImplementationOnce(() => MyDate); const decorator = attribute(); const target = Object.create(null); @@ -224,7 +224,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of BinarySet as a set of binary values`, () => { - (Reflect.getMetadata as any).mockImplementation(() => BinarySet); + (Reflect.getMetadata as any).mockImplementationOnce(() => BinarySet); const decorator = attribute(); const target = Object.create(null); @@ -239,7 +239,7 @@ describe('attribute', () => { `should recognize values with a constructor that subclasses BinarySet as a set of binary values`, () => { class MyBinarySet extends BinarySet {} - (Reflect.getMetadata as any).mockImplementation(() => MyBinarySet); + (Reflect.getMetadata as any).mockImplementationOnce(() => MyBinarySet); const decorator = attribute(); const target = Object.create(null); @@ -253,7 +253,7 @@ describe('attribute', () => { it( `should recognize values with a constructor of NumberValueSet as a set of number values`, () => { - (Reflect.getMetadata as any).mockImplementation(() => NumberValueSet); + (Reflect.getMetadata as any).mockImplementationOnce(() => NumberValueSet); const decorator = attribute(); const target = Object.create(null); @@ -268,7 +268,7 @@ describe('attribute', () => { `should recognize values with a constructor that subclasses NumberValueSet as a set of number values`, () => { class MyNumberValueSet extends NumberValueSet {} - (Reflect.getMetadata as any).mockImplementation(() => MyNumberValueSet); + (Reflect.getMetadata as any).mockImplementationOnce(() => MyNumberValueSet); const decorator = attribute(); const target = Object.create(null); @@ -280,7 +280,7 @@ describe('attribute', () => { ); it(`should recognize values with a constructor of Set as a set`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Set); + (Reflect.getMetadata as any).mockImplementationOnce(() => Set); const decorator = attribute({memberType: 'String'}); const target = Object.create(null); @@ -294,7 +294,7 @@ describe('attribute', () => { `should recognize values with a constructor that subclasses Set as a set`, () => { class MySet extends Set {} - (Reflect.getMetadata as any).mockImplementation(() => MySet); + (Reflect.getMetadata as any).mockImplementationOnce(() => MySet); const decorator = attribute({memberType: 'Number'}); const target = Object.create(null); @@ -308,7 +308,7 @@ describe('attribute', () => { it( `should throw on values with a constructor of Set that lack a memberType declaration`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Set); + (Reflect.getMetadata as any).mockImplementationOnce(() => Set); const decorator = attribute(); expect(() => decorator({}, 'property')) @@ -317,7 +317,7 @@ describe('attribute', () => { ); it(`should recognize values with a constructor of Map as a map`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Map); + (Reflect.getMetadata as any).mockImplementationOnce(() => Map); const memberType: SchemaType = { type: 'Document', members: {}, @@ -335,7 +335,7 @@ describe('attribute', () => { `should recognize values with a constructor that subclasses Map as a map`, () => { class MyMap extends Map {} - (Reflect.getMetadata as any).mockImplementation(() => MyMap); + (Reflect.getMetadata as any).mockImplementationOnce(() => MyMap); const memberType: SchemaType = { type: 'Tuple', members: [ @@ -356,7 +356,7 @@ describe('attribute', () => { it( `should throw on values with a constructor of Map that lack a memberType declaration`, () => { - (Reflect.getMetadata as any).mockImplementation(() => Map); + (Reflect.getMetadata as any).mockImplementationOnce(() => Map); const decorator = attribute(); expect(() => decorator({}, 'property')) @@ -373,7 +373,7 @@ describe('attribute', () => { } } - (Reflect.getMetadata as any).mockImplementation(() => Document); + (Reflect.getMetadata as any).mockImplementationOnce(() => Document); const decorator = attribute(); const target = Object.create(null); decorator(target, 'property'); @@ -387,7 +387,7 @@ describe('attribute', () => { ); it('should treat arrays as collection types', () => { - (Reflect.getMetadata as any).mockImplementation(() => Array); + (Reflect.getMetadata as any).mockImplementationOnce(() => Array); const decorator = attribute(); const target = Object.create(null); decorator(target, 'property'); @@ -400,7 +400,7 @@ describe('attribute', () => { it( 'should treat arrays with a declared memberType as list types', () => { - (Reflect.getMetadata as any).mockImplementation(() => Array); + (Reflect.getMetadata as any).mockImplementationOnce(() => Array); const memberType: SchemaType = {type: 'String'}; const decorator = attribute({memberType}); @@ -417,7 +417,7 @@ describe('attribute', () => { it( 'should treat arrays with members as tuple types', () => { - (Reflect.getMetadata as any).mockImplementation(() => Array); + (Reflect.getMetadata as any).mockImplementationOnce(() => Array); const members: Array = [ {type: 'Boolean'}, @@ -438,7 +438,7 @@ describe('attribute', () => { 'should constructors that descend from Array as collection types', () => { class MyArray extends Array {} - (Reflect.getMetadata as any).mockImplementation(() => MyArray); + (Reflect.getMetadata as any).mockImplementationOnce(() => MyArray); const decorator = attribute(); const target = Object.create(null); decorator(target, 'property'); @@ -452,7 +452,7 @@ describe('attribute', () => { it( 'should treat values with an unrecognized constructor as an "Any" type', () => { - (Reflect.getMetadata as any).mockImplementation(() => Object); + (Reflect.getMetadata as any).mockImplementationOnce(() => Object); const decorator = attribute(); const target = Object.create(null); decorator(target, 'property'); diff --git a/packages/dynamodb-data-mapper-annotations/src/attribute.ts b/packages/dynamodb-data-mapper-annotations/src/attribute.ts index 57a4a52a..23960295 100644 --- a/packages/dynamodb-data-mapper-annotations/src/attribute.ts +++ b/packages/dynamodb-data-mapper-annotations/src/attribute.ts @@ -61,8 +61,7 @@ export function attribute( parameters: Partial = {} ): PropertyAnnotation { return (target, propertyKey) => { - // Mark the property as seen for @pristine-ts/metadata to automatically register the types. - PropertyMetadata.propertySeen(target, propertyKey); + if (!Object.prototype.hasOwnProperty.call(target, DynamoDbSchema)) { Object.defineProperty( @@ -96,6 +95,9 @@ export function attribute( } (target as any)[DynamoDbSchema][propertyKey] = schemaType; + + // Mark the property as seen for @pristine-ts/metadata to automatically register the types. + PropertyMetadata.propertySeen(target, propertyKey); }; }