Skip to content

Commit a2c26d4

Browse files
authored
Convert string to Date for TIMESTAMP fields (#29)
1 parent 8757b1a commit a2c26d4

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

.changeset/funny-geese-retire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"kysely-data-api": minor
3+
---
4+
5+
Convert string for fields marked as TIMESTAMP to Date

src/data-api-driver.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,27 @@ class DataApiConnection implements DatabaseConnection {
111111
?.filter((r) => r.length !== 0)
112112
.map((rec) =>
113113
Object.fromEntries(
114-
rec.map((val, i) => [
115-
r.columnMetadata![i].label || r.columnMetadata![i].name,
116-
val.isNull
114+
rec.map((val, i) => {
115+
const { label, name, typeName } = r.columnMetadata![i]
116+
const key = label || name;
117+
let value = val.isNull
117118
? null
118119
: val.stringValue ??
119120
val.doubleValue ??
120121
val.longValue ??
121122
val.booleanValue ??
122123
this.#unmarshallArrayValue(val.arrayValue) ??
123124
val.blobValue ??
124-
null, // FIXME: should throw an error here?
125-
])
125+
null; // FIXME: should throw an error here?
126+
127+
if (typeof(value) === 'string' && typeName && ["timestamp", "timestamptz", "date"].includes(
128+
typeName.toLocaleLowerCase()
129+
)) {
130+
value = new Date(value);
131+
}
132+
133+
return [key, value];
134+
})
126135
)
127136
);
128137
const result: QueryResult<O> = {

0 commit comments

Comments
 (0)