Skip to content

Commit 724f4cb

Browse files
dyeliseyevDzmitry Yeliseyeu
and
Dzmitry Yeliseyeu
authored
Handle case when sqlStatementResults doesn't have records field (#70)
Co-authored-by: Dzmitry Yeliseyeu <[email protected]>
1 parent b553551 commit 724f4cb

File tree

3 files changed

+170
-20
lines changed

3 files changed

+170
-20
lines changed

Diff for: __tests__/__snapshots__/resolvers.test.js.snap

+28
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,34 @@ exports[`rds resolvers toJsonObject 1`] = `
259259
]
260260
`;
261261

262+
exports[`rds resolvers toJsonObject insert or update 1`] = `
263+
[
264+
[
265+
{},
266+
],
267+
]
268+
`;
269+
270+
exports[`rds resolvers toJsonObject update and select 1`] = `
271+
[
272+
[
273+
{},
274+
],
275+
[
276+
{
277+
"ISBN-13": "978-1948132817",
278+
"author": "Mark Twain",
279+
"title": "Adventures of Huckleberry Finn",
280+
},
281+
{
282+
"ISBN-13": "978-1948132275",
283+
"author": "Jack London",
284+
"title": "The Call of the Wild",
285+
},
286+
],
287+
]
288+
`;
289+
262290
exports[`rds resolvers typehints TIMESTAMP 1`] = `
263291
{
264292
"type": "TIMESTAMP",

Diff for: __tests__/resolvers.test.js

+120
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,126 @@ describe("rds resolvers", () => {
209209

210210
await checkResolverValid(code, responseContext, "response");
211211
});
212+
test("toJsonObject insert or update", async () => {
213+
const responseContext = {
214+
"result": JSON.stringify({
215+
"sqlStatementResults": [{
216+
"numberOfRecordsUpdated": 1,
217+
"generatedFields": []
218+
}]
219+
})
220+
};
221+
222+
const code = `
223+
export function request(ctx) {}
224+
225+
export function response(ctx) {
226+
return rds.toJsonObject(ctx.result);
227+
}
228+
`;
229+
230+
await checkResolverValid(code, responseContext, "response");
231+
})
232+
233+
test("toJsonObject update and select", async () => {
234+
const responseContext = {
235+
"result": JSON.stringify({
236+
"sqlStatementResults": [{
237+
"numberOfRecordsUpdated": 1,
238+
"generatedFields": []
239+
},
240+
{
241+
"numberOfRecordsUpdated": 0,
242+
"records": [
243+
[
244+
{
245+
"stringValue": "Mark Twain"
246+
},
247+
{
248+
"stringValue": "Adventures of Huckleberry Finn"
249+
},
250+
{
251+
"stringValue": "978-1948132817"
252+
}
253+
],
254+
[
255+
{
256+
"stringValue": "Jack London"
257+
},
258+
{
259+
"stringValue": "The Call of the Wild"
260+
},
261+
{
262+
"stringValue": "978-1948132275"
263+
}
264+
]
265+
],
266+
"columnMetadata": [
267+
{
268+
"isSigned": false,
269+
"isCurrency": false,
270+
"label": "author",
271+
"precision": 200,
272+
"typeName": "VARCHAR",
273+
"scale": 0,
274+
"isAutoIncrement": false,
275+
"isCaseSensitive": false,
276+
"schemaName": "",
277+
"tableName": "Books",
278+
"type": 12,
279+
"nullable": 0,
280+
"arrayBaseColumnType": 0,
281+
"name": "author"
282+
},
283+
{
284+
"isSigned": false,
285+
"isCurrency": false,
286+
"label": "title",
287+
"precision": 200,
288+
"typeName": "VARCHAR",
289+
"scale": 0,
290+
"isAutoIncrement": false,
291+
"isCaseSensitive": false,
292+
"schemaName": "",
293+
"tableName": "Books",
294+
"type": 12,
295+
"nullable": 0,
296+
"arrayBaseColumnType": 0,
297+
"name": "title"
298+
},
299+
{
300+
"isSigned": false,
301+
"isCurrency": false,
302+
"label": "ISBN-13",
303+
"precision": 15,
304+
"typeName": "VARCHAR",
305+
"scale": 0,
306+
"isAutoIncrement": false,
307+
"isCaseSensitive": false,
308+
"schemaName": "",
309+
"tableName": "Books",
310+
"type": 12,
311+
"nullable": 0,
312+
"arrayBaseColumnType": 0,
313+
"name": "ISBN-13"
314+
}
315+
]
316+
}
317+
]
318+
})
319+
};
320+
321+
const code = `
322+
export function request(ctx) {}
323+
324+
export function response(ctx) {
325+
return rds.toJsonObject(ctx.result);
326+
}
327+
`;
328+
329+
await checkResolverValid(code, responseContext, "response");
330+
})
331+
212332

213333
describe("mysql", () => {
214334
test("raw string", async () => {

Diff for: rds/index.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ export function toJsonObject(inputStr) {
99
let perStatement = [];
1010
for (const { records, columnMetadata } of input.sqlStatementResults) {
1111
const statement = [];
12+
if(typeof records === 'undefined' ){
13+
statement.push({});
14+
} else {
15+
for (const record of records) {
16+
const row = {};
17+
if (record.length !== columnMetadata.length) {
18+
// TODO: what to do here?!
19+
throw new Error("TODO");
20+
}
1221

13-
for (const record of records) {
14-
const row = {};
15-
if (record.length !== columnMetadata.length) {
16-
// TODO: what to do here?!
17-
throw new Error("TODO");
18-
}
22+
for (const colNo in record) {
1923

20-
for (const colNo in record) {
24+
// TODO: what if the column is not a string?
25+
const { stringValue } = record[colNo];
26+
const { label } = columnMetadata[colNo];
2127

22-
// TODO: what if the column is not a string?
23-
const { stringValue } = record[colNo];
24-
const { label } = columnMetadata[colNo];
28+
row[label] = stringValue;
29+
}
2530

26-
row[label] = stringValue;
31+
statement.push(row);
2732
}
28-
29-
statement.push(row);
3033
}
31-
3234
perStatement.push(statement);
3335
}
3436
return perStatement;
@@ -311,37 +313,37 @@ export function createMySQLStatement(...statements) {
311313

312314

313315
export const typeHint = {
314-
DECIMAL: function(value) {
316+
DECIMAL: function (value) {
315317
return {
316318
type: "DECIMAL",
317319
value,
318320
};
319321
},
320-
JSON: function(value) {
322+
JSON: function (value) {
321323
return {
322324
type: "JSON",
323325
value,
324326
};
325327
},
326-
TIME: function(value) {
328+
TIME: function (value) {
327329
return {
328330
type: "TIME",
329331
value,
330332
};
331333
},
332-
DATE: function(value) {
334+
DATE: function (value) {
333335
return {
334336
type: "DATE",
335337
value,
336338
};
337339
},
338-
UUID: function(value) {
340+
UUID: function (value) {
339341
return {
340342
type: "UUID",
341343
value,
342344
};
343345
},
344-
TIMESTAMP: function(value) {
346+
TIMESTAMP: function (value) {
345347
return {
346348
type: "TIMESTAMP",
347349
value: value.toISOString(),

0 commit comments

Comments
 (0)