Skip to content

Commit

Permalink
fix(driver): return Object for JSON columns and imply UTC for timesta…
Browse files Browse the repository at this point in the history
…mpz (#43)
  • Loading branch information
omikader authored Sep 25, 2024
1 parent 063cc6f commit f1de155
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2,993 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-ways-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kysely-data-api": major
---

fix(driver): return Object for JSON columns and imply UTC for timestampz
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"license": "MIT",
"scripts": {
"clean": "rm -rf dist && rm -rf test/dist",
"release": "yarn build && yarn changeset publish",
"build": "yarn clean && yarn build:esm && yarn build:cjs && ./module-fixup.sh",
"release": "pnpm build && pnpm changeset publish",
"build": "pnpm clean && pnpm build:esm && pnpm build:cjs && ./module-fixup.sh",
"test": "AWS_SDK_LOAD_CONFIG=1 vitest",
"build:esm": "tsc -p tsconfig-esm.json",
"build:cjs": "tsc -p tsconfig-cjs.json"
Expand Down
21 changes: 12 additions & 9 deletions src/data-api-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class DataApiConnection implements DatabaseConnection {
.map((rec) =>
Object.fromEntries(
rec.map((val, i) => {
const { label, name, typeName } = r.columnMetadata![i]
const { label, name, typeName } = r.columnMetadata![i];
const key = label || name;
let value = val.isNull
? null
Expand All @@ -124,20 +124,23 @@ class DataApiConnection implements DatabaseConnection {
val.blobValue ??
null; // FIXME: should throw an error here?

if (typeof(value) === 'string' && typeName && ["timestamp", "timestamptz", "date"].includes(
typeName.toLocaleLowerCase()
)) {
value = new Date(value);
if (typeof value === "string" && typeName) {
const typeNameSafe = typeName.toLocaleLowerCase();
if (["timestamp", "date"].includes(typeNameSafe)) {
value = new Date(value);
} else if (typeNameSafe === "timestamptz") {
value = new Date(`${value}Z`);
} else if (["json", "jsonb"].includes(typeNameSafe)) {
value = JSON.parse(value);
}
}

return [key, value];
})
)
);
const result: QueryResult<O> = {
rows: rows || [],
};
return result;

return { rows: rows ?? [] };
}

async *streamQuery<O>(
Expand Down
Loading

0 comments on commit f1de155

Please sign in to comment.