Skip to content

Commit 3bf5080

Browse files
committed
feat(mapper): return an actual instance of model class
1 parent 3ffded4 commit 3bf5080

File tree

9 files changed

+51
-3
lines changed

9 files changed

+51
-3
lines changed

src/dynamo/batchget/batch-get.request.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ describe('batch get', () => {
209209
const result = await request.exec()
210210
expect(batchGetItemsSpy).toHaveBeenCalled()
211211
expect(result).toEqual({ [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] })
212+
expect(result[getTableName(SimpleWithPartitionKeyModel)][0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
212213
})
213214

214215
it('execFullResponse', async () => {
@@ -218,6 +219,7 @@ describe('batch get', () => {
218219
Responses: { [getTableName(SimpleWithPartitionKeyModel)]: [jsItem] },
219220
UnprocessedKeys: {},
220221
})
222+
expect(result.Responses[getTableName(SimpleWithPartitionKeyModel)][0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
221223
})
222224
})
223225
})

src/dynamo/request/batchgetsingletable/batch-get-single-table.request.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ describe('batch get', () => {
109109
expect(result).toBeDefined()
110110
expect(result.Items).toBeDefined()
111111
expect(result.Items).toEqual([jsItem])
112+
expect(result.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
112113
})
113114
it('exec', async () => {
114115
const result = await req.exec()
115116
expect(result).toEqual([jsItem])
117+
expect(result[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
116118
})
117119
})
118120

src/dynamo/request/get/get.request.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ describe('GetRequest', () => {
6565
req = new GetRequest(<any>{ getItem: getItemSpy }, SimpleWithPartitionKeyModel, 'my-id')
6666
})
6767
it('exec', async () => {
68-
expect(await req.exec()).toEqual(jsItem)
68+
const res = await req.exec()
69+
expect(res).toEqual(jsItem)
70+
expect(res).toBeInstanceOf(SimpleWithPartitionKeyModel)
6971
})
7072
it('execFullResponse', async () => {
71-
expect(await req.execFullResponse()).toEqual({ Item: jsItem })
73+
const res = await req.execFullResponse()
74+
expect(res).toEqual({ Item: jsItem })
75+
expect(res.Item).toBeInstanceOf(SimpleWithPartitionKeyModel)
7276
})
7377
})
7478

src/dynamo/request/query/query.request.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,10 @@ describe('query request', () => {
176176
expect(req.params.ScanIndexForward).toBeFalsy()
177177
})
178178
})
179+
180+
// describe('exec', () => {
181+
// it('maps the item', async () => {
182+
//
183+
// })
184+
// })
179185
})

src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ describe('TransactGetSingleTableRequest', () => {
4848
id: 'myId',
4949
age: 20,
5050
})
51+
expect(resp[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
5152
})
5253

5354
it('execNoMap should return the raw value', async () => {
@@ -64,6 +65,7 @@ describe('TransactGetSingleTableRequest', () => {
6465
id: 'myId',
6566
age: 20,
6667
})
68+
expect(resp.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
6769
})
6870
})
6971

src/dynamo/request/update/update.request.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ describe('update request', () => {
201201
})
202202
})
203203

204+
describe('exec', async () => {
205+
const updateItemSpy = jasmine.createSpy().and.returnValue(Promise.resolve({
206+
Attributes: {
207+
id: { S: 'partKey' },
208+
creationDate: { S: new Date().toISOString() },
209+
},
210+
}))
211+
const res = await new UpdateRequest(<any>{ updateItem: updateItemSpy }, UpdateModel, 'myId')
212+
.updateAttribute('counter').set(25, true)
213+
.returnValues('ALL_OLD')
214+
.exec()
215+
216+
expect(res).toBeInstanceOf(UpdateModel)
217+
218+
})
219+
204220
describe('logger', () => {
205221
const sampleResponse: DynamoDB.UpdateItemOutput = { Attributes: undefined }
206222
let logReceiver: jasmine.Spy

src/dynamo/transactget/transact-get.request.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,12 @@ describe('TransactGetRequest', () => {
114114
const result = await req2.exec()
115115
expect(Array.isArray(result)).toBeTruthy()
116116
expect(result.length).toBe(2)
117+
expect(result[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
117118
expect(result[0]).toEqual({
118119
id: 'myId',
119120
age: 20,
120121
})
122+
expect(result[1]).toBeInstanceOf(SimpleWithCompositePartitionKeyModel)
121123
expect(result[1]).toEqual({
122124
id: 'myId',
123125
age: 22,
@@ -130,10 +132,12 @@ describe('TransactGetRequest', () => {
130132
expect(result).toBeDefined()
131133
expect(result.ConsumedCapacity).toEqual([])
132134
expect(result.Items).toBeDefined()
135+
expect(result.Items[0]).toBeInstanceOf(SimpleWithPartitionKeyModel)
133136
expect(result.Items[0]).toEqual({
134137
id: 'myId',
135138
age: 20,
136139
})
140+
expect(result.Items[1]).toBeInstanceOf(SimpleWithCompositePartitionKeyModel)
137141
expect(result.Items[1]).toEqual({
138142
id: 'myId',
139143
age: 22,

src/mapper/mapper.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@ describe('Mapper', () => {
846846
product = fromDb(productFromDb, Product)
847847
})
848848

849+
it('instance', () => {
850+
expect(product).toBeInstanceOf(Product)
851+
})
852+
849853
it('nested value', () => {
850854
expect(product.nestedValue).toBeDefined()
851855
expect(Object.getOwnPropertyNames(product.nestedValue).length).toBe(1)
@@ -862,6 +866,10 @@ describe('Mapper', () => {
862866
organization = fromDb(organizationFromDb, Organization)
863867
})
864868

869+
it('instance', () => {
870+
expect(organization).toBeInstanceOf(Organization)
871+
})
872+
865873
it('id', () => {
866874
expect(organization.id).toBe('myId')
867875
})
@@ -976,6 +984,10 @@ describe('Mapper', () => {
976984

977985
const fromDbVal: ModelWithNonDecoratedEnum = fromDb(attributes, ModelWithNonDecoratedEnum)
978986

987+
it('instance', () => {
988+
expect(fromDbVal).toBeInstanceOf(ModelWithNonDecoratedEnum)
989+
})
990+
979991
it('should map all properties', () => {
980992
expect(fromDbVal).toBeDefined()
981993

src/mapper/mapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export function fromDb<T>(attributeMap: Attributes<T>, modelConstructor?: ModelC
278278
}
279279
})
280280

281-
return model
281+
return modelConstructor ? Object.assign(new modelConstructor(), model) : model
282282
}
283283

284284
/**

0 commit comments

Comments
 (0)