Skip to content

Commit 52aa6d9

Browse files
committed
pass mapping information to model batch item in order to be able to parse custom mappings
1 parent 1b62fc4 commit 52aa6d9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/mapping/model-batch-item.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ModelBatchItem {
9090
// Either all queries are counter mutation or we let it fail at server level
9191
isCounter = q.isCounter;
9292

93-
arr.push({ query: q.query, params: q.paramsGetter(this.doc, this.docInfo) });
93+
arr.push({ query: q.query, params: q.paramsGetter(this.doc, this.docInfo, this.getMappingInfo()) });
9494
});
9595

9696
return { isIdempotent, isCounter };

test/unit/mapping/mapper-tests.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,39 @@ describe('Mapper', () => {
213213
assert.strictEqual(clientInfo.batchExecutions[0].options.isIdempotent, isIdempotent);
214214
});
215215
})));
216+
217+
it('should be able to batch using custom field parser', () => {
218+
const modelWithCustomParser = {
219+
'Sample': {
220+
tables: [ 'table1' ],
221+
columns: {
222+
key: 'key',
223+
field: {
224+
fromModel: JSON.stringify,
225+
toModel: JSON.parse,
226+
}
227+
}
228+
}
229+
};
230+
231+
232+
const clientInfo = mapperTestHelper.getClient(['key', 'field'], [ 1 ]);
233+
const mapper = mapperTestHelper.getMapper(clientInfo, modelWithCustomParser);
234+
const modelMapper = mapper.forModel('Sample');
235+
236+
const someFakeJson = { value: 'someValue' };
237+
238+
const items = [
239+
modelMapper.batching.insert({ key: 1, field: someFakeJson }),
240+
modelMapper.batching.update({ key: 2, field: someFakeJson }),
241+
];
242+
243+
mapper.batch(items)
244+
.then(() => {
245+
assert.lengthOf(clientInfo.batchExecutions[0].queries, 2);
246+
assert.isTrue(clientInfo.batchExecutions[0].queries[0].params[1] === JSON.stringify(someFakeJson));
247+
assert.isTrue(clientInfo.batchExecutions[0].queries[1].params[0] === JSON.stringify(someFakeJson));
248+
});
249+
});
216250
});
217251
});

0 commit comments

Comments
 (0)