Skip to content

Commit fab13d3

Browse files
author
Sylvestre
authored
Timestamp to UTC (#64)
* coeercing timestamp to UTC * callback
1 parent 77a4c66 commit fab13d3

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

lib/databricks.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {json} from "micro";
2-
import {Readable} from "node:stream";
2+
import {Readable, Transform} from "node:stream";
33
import JSONStream from "JSONStream";
44

55
import {
@@ -226,13 +226,19 @@ export async function queryStream(req, res, connection) {
226226
try {
227227
query = await session.executeStatement(sql, {runAsync: true});
228228
const rows = await query.fetchAll();
229+
const timestampFieldMap = new Map();
229230
const schema = await query.getSchema();
230231

231232
const responseSchema = {
232233
type: "array",
233234
items: {
234235
type: "object",
235-
properties: schema.columns.reduce((schema, col, idx) => {
236+
properties: schema.columns.reduce((schema, col) => {
237+
// If the column is a timestamp, we need keep track of its key.
238+
// We will use this later to convert the timestamp to a UTC ISO-96801 format.
239+
if (col.typeDesc.types[0].primitiveEntry.type === 8) {
240+
timestampFieldMap.set(col.columnName, "TIMESTAMP");
241+
}
236242
return {
237243
...schema,
238244
...{
@@ -261,7 +267,32 @@ export async function queryStream(req, res, connection) {
261267

262268
stream.on("error", reject);
263269

264-
stream.pipe(JSONStream.stringify("", "\n", "\n")).pipe(res);
270+
stream
271+
.pipe(
272+
new Transform({
273+
objectMode: true,
274+
transform(chunk, encoding, callback) {
275+
let row = null;
276+
try {
277+
row = Object.entries(chunk).reduce((row, [key, value]) => {
278+
return {
279+
...row,
280+
...{
281+
[key]: timestampFieldMap.has(key) ? `${value}Z` : value,
282+
},
283+
};
284+
}, {});
285+
} catch (e) {
286+
console.error("row has unexpected format");
287+
// TODO: Add error handling once server supports handling error for in flight streamed response
288+
// cb(new Error(e));
289+
}
290+
callback(null, row);
291+
},
292+
})
293+
)
294+
.pipe(JSONStream.stringify("", "\n", "\n"))
295+
.pipe(res);
265296
});
266297
} catch (error) {
267298
if (!error.statusCode) error.statusCode = 400;

0 commit comments

Comments
 (0)