From 3bf5080cbf3ffddedc87f0dc75b5d8575f9d11c7 Mon Sep 17 00:00:00 2001 From: Simon Mumenthaler Date: Thu, 4 Jun 2020 08:35:25 +0200 Subject: [PATCH] feat(mapper): return an actual instance of model class --- src/dynamo/batchget/batch-get.request.spec.ts | 2 ++ .../batch-get-single-table.request.spec.ts | 2 ++ src/dynamo/request/get/get.request.spec.ts | 8 ++++++-- src/dynamo/request/query/query.request.spec.ts | 6 ++++++ .../transact-get-single-table.request.spec.ts | 2 ++ src/dynamo/request/update/update.request.spec.ts | 16 ++++++++++++++++ .../transactget/transact-get.request.spec.ts | 4 ++++ src/mapper/mapper.spec.ts | 12 ++++++++++++ src/mapper/mapper.ts | 2 +- 9 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/dynamo/batchget/batch-get.request.spec.ts b/src/dynamo/batchget/batch-get.request.spec.ts index 8f8597c1b..25c128515 100644 --- a/src/dynamo/batchget/batch-get.request.spec.ts +++ b/src/dynamo/batchget/batch-get.request.spec.ts @@ -209,6 +209,7 @@ describe('batch get', () => { const result = await request.exec() expect(batchGetItemsSpy).toHaveBeenCalled() expect(result).toEqual({ [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] }) + expect(result[getTableName(SimpleWithPartitionKeyModel)][0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) it('execFullResponse', async () => { @@ -218,6 +219,7 @@ describe('batch get', () => { Responses: { [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] }, UnprocessedKeys: {}, }) + expect(result.Responses[getTableName(SimpleWithPartitionKeyModel)][0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) }) }) diff --git a/src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts b/src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts index c8e056aae..808050eb2 100644 --- a/src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts +++ b/src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts @@ -109,10 +109,12 @@ describe('batch get', () => { expect(result).toBeDefined() expect(result.Items).toBeDefined() expect(result.Items).toEqual([jsItem]) + expect(result.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) it('exec', async () => { const result = await req.exec() expect(result).toEqual([jsItem]) + expect(result[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) }) diff --git a/src/dynamo/request/get/get.request.spec.ts b/src/dynamo/request/get/get.request.spec.ts index ce4a14c7b..4d176504e 100644 --- a/src/dynamo/request/get/get.request.spec.ts +++ b/src/dynamo/request/get/get.request.spec.ts @@ -65,10 +65,14 @@ describe('GetRequest', () => { req = new GetRequest({ getItem: getItemSpy }, SimpleWithPartitionKeyModel, 'my-id') }) it('exec', async () => { - expect(await req.exec()).toEqual(jsItem) + const res = await req.exec() + expect(res).toEqual(jsItem) + expect(res).toBeInstanceOf(SimpleWithPartitionKeyModel) }) it('execFullResponse', async () => { - expect(await req.execFullResponse()).toEqual({ Item: jsItem }) + const res = await req.execFullResponse() + expect(res).toEqual({ Item: jsItem }) + expect(res.Item).toBeInstanceOf(SimpleWithPartitionKeyModel) }) }) diff --git a/src/dynamo/request/query/query.request.spec.ts b/src/dynamo/request/query/query.request.spec.ts index 5ea90bc5b..6493c589d 100644 --- a/src/dynamo/request/query/query.request.spec.ts +++ b/src/dynamo/request/query/query.request.spec.ts @@ -176,4 +176,10 @@ describe('query request', () => { expect(req.params.ScanIndexForward).toBeFalsy() }) }) + + // describe('exec', () => { + // it('maps the item', async () => { + // + // }) + // }) }) diff --git a/src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts b/src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts index 6dd97d2ea..d984ebab7 100644 --- a/src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts +++ b/src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts @@ -48,6 +48,7 @@ describe('TransactGetSingleTableRequest', () => { id: 'myId', age: 20, }) + expect(resp[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) it('execNoMap should return the raw value', async () => { @@ -64,6 +65,7 @@ describe('TransactGetSingleTableRequest', () => { id: 'myId', age: 20, }) + expect(resp.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) }) }) diff --git a/src/dynamo/request/update/update.request.spec.ts b/src/dynamo/request/update/update.request.spec.ts index c45dbe6c9..db27b0d70 100644 --- a/src/dynamo/request/update/update.request.spec.ts +++ b/src/dynamo/request/update/update.request.spec.ts @@ -201,6 +201,22 @@ describe('update request', () => { }) }) + describe('exec', async () => { + const updateItemSpy = jasmine.createSpy().and.returnValue(Promise.resolve({ + Attributes: { + id: { S: 'partKey' }, + creationDate: { S: new Date().toISOString() }, + }, + })) + const res = await new UpdateRequest({ updateItem: updateItemSpy }, UpdateModel, 'myId') + .updateAttribute('counter').set(25, true) + .returnValues('ALL_OLD') + .exec() + + expect(res).toBeInstanceOf(UpdateModel) + + }) + describe('logger', () => { const sampleResponse: DynamoDB.UpdateItemOutput = { Attributes: undefined } let logReceiver: jasmine.Spy diff --git a/src/dynamo/transactget/transact-get.request.spec.ts b/src/dynamo/transactget/transact-get.request.spec.ts index 9ffa7ec31..0ad46df39 100644 --- a/src/dynamo/transactget/transact-get.request.spec.ts +++ b/src/dynamo/transactget/transact-get.request.spec.ts @@ -114,10 +114,12 @@ describe('TransactGetRequest', () => { const result = await req2.exec() expect(Array.isArray(result)).toBeTruthy() expect(result.length).toBe(2) + expect(result[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) expect(result[0]).toEqual({ id: 'myId', age: 20, }) + expect(result[1]).toBeInstanceOf(SimpleWithCompositePartitionKeyModel) expect(result[1]).toEqual({ id: 'myId', age: 22, @@ -130,10 +132,12 @@ describe('TransactGetRequest', () => { expect(result).toBeDefined() expect(result.ConsumedCapacity).toEqual([]) expect(result.Items).toBeDefined() + expect(result.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel) expect(result.Items[0]).toEqual({ id: 'myId', age: 20, }) + expect(result.Items[1]).toBeInstanceOf(SimpleWithCompositePartitionKeyModel) expect(result.Items[1]).toEqual({ id: 'myId', age: 22, diff --git a/src/mapper/mapper.spec.ts b/src/mapper/mapper.spec.ts index 3e1152a3c..319715847 100644 --- a/src/mapper/mapper.spec.ts +++ b/src/mapper/mapper.spec.ts @@ -846,6 +846,10 @@ describe('Mapper', () => { product = fromDb(productFromDb, Product) }) + it('instance', () => { + expect(product).toBeInstanceOf(Product) + }) + it('nested value', () => { expect(product.nestedValue).toBeDefined() expect(Object.getOwnPropertyNames(product.nestedValue).length).toBe(1) @@ -862,6 +866,10 @@ describe('Mapper', () => { organization = fromDb(organizationFromDb, Organization) }) + it('instance', () => { + expect(organization).toBeInstanceOf(Organization) + }) + it('id', () => { expect(organization.id).toBe('myId') }) @@ -976,6 +984,10 @@ describe('Mapper', () => { const fromDbVal: ModelWithNonDecoratedEnum = fromDb(attributes, ModelWithNonDecoratedEnum) + it('instance', () => { + expect(fromDbVal).toBeInstanceOf(ModelWithNonDecoratedEnum) + }) + it('should map all properties', () => { expect(fromDbVal).toBeDefined() diff --git a/src/mapper/mapper.ts b/src/mapper/mapper.ts index bdb7d381c..63912f43f 100644 --- a/src/mapper/mapper.ts +++ b/src/mapper/mapper.ts @@ -278,7 +278,7 @@ export function fromDb(attributeMap: Attributes, modelConstructor?: ModelC } }) - return model + return modelConstructor ? Object.assign(new modelConstructor(), model) : model } /**