@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 97 lines 3.16 KB
Raw
1 import type * as rdsdata from "@distilled.cloud/aws/rds-data";
2
3 /**
4 * JS value → Data API `SqlParameter` marshalling, matching the Postgres
5 * wire expectations: integers as `longValue`, floats as `doubleValue`,
6 * `Date`s as `TIMESTAMP`-hinted strings, `Uint8Array` as blobs, and
7 * anything else JSON-stringified.
8 */
9 export const toSqlParameter = (name: string, value: unknown): rdsdata.SqlParameter => {
10 if (value === null || value === undefined) {
11 return { name, value: { isNull: true } };
12 }
13 if (typeof value === "string") {
14 return { name, value: { stringValue: value } };
15 }
16 if (typeof value === "number") {
17 return Number.isInteger(value)
18 ? { name, value: { longValue: value } }
19 : { name, value: { doubleValue: value } };
20 }
21 if (typeof value === "bigint") {
22 return { name, value: { longValue: Number(value) } };
23 }
24 if (typeof value === "boolean") {
25 return { name, value: { booleanValue: value } };
26 }
27 if (value instanceof Date) {
28 return {
29 name,
30 typeHint: "TIMESTAMP",
31 value: {
32 stringValue: value.toISOString().replace("T", " ").replace("Z", ""),
33 },
34 };
35 }
36 if (value instanceof Uint8Array) {
37 return { name, value: { blobValue: value } as rdsdata.Field };
38 }
39 return { name, value: { stringValue: JSON.stringify(value) } };
40 };
41
42 const TIMESTAMP_TYPES = new Set(["timestamp", "timestamptz", "date", "datetime"]);
43
44 /**
45 * Data API `Field` → JS value. Timestamp-typed columns (per the response's
46 * `columnMetadata.typeName`) revive as `Date`s — the Data API returns UTC
47 * timestamps as `"YYYY-MM-DD HH:MM:SS[.FFF]"` strings.
48 */
49 export const fromField = (field: rdsdata.Field, typeName: string | undefined): unknown => {
50 const f = field;
51 if (f.isNull) {
52 return null;
53 }
54 if (f.stringValue !== undefined) {
55 if (typeName !== undefined && TIMESTAMP_TYPES.has(typeName.toLowerCase())) {
56 const iso = f.stringValue.replace(" ", "T");
57 return new Date(/[Z+]/.test(iso.slice(10)) ? iso : `${iso}Z`);
58 }
59 return f.stringValue;
60 }
61 if (f.longValue !== undefined) return f.longValue;
62 if (f.doubleValue !== undefined) return f.doubleValue;
63 if (f.booleanValue !== undefined) return f.booleanValue;
64 if (f.blobValue !== undefined) return f.blobValue;
65 if (f.arrayValue !== undefined) return fromArrayValue(f.arrayValue);
66 return null;
67 };
68
69 const fromArrayValue = (value: rdsdata.ArrayValue): unknown[] =>
70 value.arrayValues?.map(fromArrayValue) ??
71 value.booleanValues ??
72 value.longValues ??
73 value.doubleValues ??
74 value.stringValues ??
75 [];
76
77 /**
78 * Map an `executeStatement` response (with `includeResultMetadata: true`)
79 * to rows keyed by column label.
80 */
81 export const toRows = (response: {
82 records?: rdsdata.Field[][];
83 columnMetadata?: rdsdata.ColumnMetadata[];
84 }): Record<string, unknown>[] => {
85 const metadata = response.columnMetadata ?? [];
86 return (response.records ?? []).map((record) => {
87 const row: Record<string, unknown> = {};
88 record.forEach((field, index) => {
89 const column = metadata[index];
90 row[column?.label ?? column?.name ?? `column${index}`] = fromField(
91 field,
92 column?.typeName ?? undefined,
93 );
94 });
95 return row;
96 });
97 };