Skip to content

Commit

Permalink
feat(mapper): return an actual instance of model class
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmumenthaler committed Jun 4, 2020
1 parent 3ffded4 commit 3bf5080
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/dynamo/batchget/batch-get.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -218,6 +219,7 @@ describe('batch get', () => {
Responses: { [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] },
UnprocessedKeys: {},
})
expect(result.Responses[getTableName(SimpleWithPartitionKeyModel)][0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@kresimirfranin

kresimirfranin Jan 20, 2022

finally

This comment has been minimized.

Copy link
@kresimirfranin

kresimirfranin Jan 20, 2022

:)

When do you guys plan to release this version?

})
it('exec', async () => {
const result = await req.exec()
expect(result).toEqual([jsItem])
expect(result[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
})
})

Expand Down
8 changes: 6 additions & 2 deletions src/dynamo/request/get/get.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,14 @@ describe('GetRequest', () => {
req = new GetRequest(<any>{ 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)
})
})

Expand Down
6 changes: 6 additions & 0 deletions src/dynamo/request/query/query.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,10 @@ describe('query request', () => {
expect(req.params.ScanIndexForward).toBeFalsy()
})
})

// describe('exec', () => {
// it('maps the item', async () => {
//
// })
// })
})
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ describe('TransactGetSingleTableRequest', () => {
id: 'myId',
age: 20,
})
expect(resp[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
})

it('execNoMap should return the raw value', async () => {
Expand All @@ -64,6 +65,7 @@ describe('TransactGetSingleTableRequest', () => {
id: 'myId',
age: 20,
})
expect(resp.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
})
})

Expand Down
16 changes: 16 additions & 0 deletions src/dynamo/request/update/update.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<any>{ 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
Expand Down
4 changes: 4 additions & 0 deletions src/dynamo/transactget/transact-get.request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/mapper/mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -862,6 +866,10 @@ describe('Mapper', () => {
organization = fromDb(organizationFromDb, Organization)
})

it('instance', () => {
expect(organization).toBeInstanceOf(Organization)
})

it('id', () => {
expect(organization.id).toBe('myId')
})
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/mapper/mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export function fromDb<T>(attributeMap: Attributes<T>, modelConstructor?: ModelC
}
})

return model
return modelConstructor ? Object.assign(new modelConstructor(), model) : model

This comment has been minimized.

Copy link
@mariobittencourt

mariobittencourt Jun 12, 2020

what if the object at hand has a private constructor? Would that still work?

This comment has been minimized.

Copy link
@simonmumenthaler

simonmumenthaler Jun 12, 2020

Author Contributor

since private modifiers only apply at dev/build time... yes

This comment has been minimized.

Copy link
@simonmumenthaler

simonmumenthaler Jun 12, 2020

Author Contributor

as mentioned in comment on issue 278 - there's a typing problem when using private constructor

}

/**
Expand Down

0 comments on commit 3bf5080

Please sign in to comment.