Skip to content

Commit f37b4f8

Browse files
committed
database-specific replacers
1 parent 635259f commit f37b4f8

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

bin/query.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {mkdir, writeFile} from "node:fs/promises";
44
import {dirname, join} from "node:path";
55
import {parseArgs} from "node:util";
66
import {getDatabase, getDatabaseConfig, getQueryCachePath} from "../src/databases/index.js";
7+
import {getReplacer} from "../src/databases/index.js";
78

89
if (process.argv[1] === import.meta.filename) run();
910

@@ -52,20 +53,14 @@ export default async function run(args?: string[]): Promise<void> {
5253
const database = await getDatabase(config);
5354
const results = await database.call(null, strings, ...params);
5455
await mkdir(dirname(cachePath), {recursive: true});
55-
await writeFile(cachePath, JSON.stringify(results, replace));
56+
await writeFile(cachePath, JSON.stringify(results, await getReplacer(config)));
5657
console.log(join(values.root, cachePath));
5758
} catch (error) {
5859
console.error(getErrorMessage(error));
5960
process.exit(1);
6061
}
6162
}
6263

63-
// Force dates to be serialized as ISO 8601 UTC, undoing this:
64-
// https://github.com/snowflakedb/snowflake-connector-nodejs/blob/a9174fb7/lib/connection/result/sf_timestamp.js#L177-L179
65-
function replace(this: {[key: string]: unknown}, key: string, value: unknown): unknown {
66-
return this[key] instanceof Date ? Date.prototype.toJSON.call(this[key]) : value;
67-
}
68-
6964
function getErrorMessage(error: unknown): string {
7065
return error instanceof Error &&
7166
"errors" in error && // AggregateError

src/databases/bigquery.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {BigQuery} from "@google-cloud/bigquery";
2+
import {BigQueryDate, BigQueryDatetime, BigQueryTimestamp} from "@google-cloud/bigquery";
23
import type {TableField, TableSchema} from "@google-cloud/bigquery";
34
import type {BigQueryConfig, QueryTemplateFunction} from "./index.js";
45
import type {ColumnSchema} from "../runtime/index.js";
@@ -55,3 +56,12 @@ function getColumnType({type, mode}: TableField): ColumnSchema["type"] {
5556
return "string";
5657
}
5758
}
59+
60+
export function replacer(this: {[key: string]: unknown}, key: string, value: unknown): unknown {
61+
const v = this[key];
62+
return v instanceof BigQueryDate ||
63+
v instanceof BigQueryDatetime ||
64+
v instanceof BigQueryTimestamp
65+
? new Date(v.value).toJSON()
66+
: value;
67+
}

src/databases/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ export async function getDatabase(config: DatabaseConfig): Promise<QueryTemplate
106106
}
107107
}
108108

109+
export type Replacer = (this: {[key: string]: unknown}, key: string, value: unknown) => unknown;
110+
111+
export async function getReplacer(config: DatabaseConfig): Promise<Replacer | undefined> {
112+
switch (config.type) {
113+
case "bigquery":
114+
return (await import("./bigquery.js")).replacer;
115+
case "snowflake":
116+
return (await import("./snowflake.js")).replacer;
117+
}
118+
}
119+
109120
export async function getQueryCachePath(
110121
sourcePath: string,
111122
databaseName: string,

src/databases/snowflake.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ function getColumnType(column: Column): ColumnSchema["type"] {
111111
return "other";
112112
}
113113
}
114+
115+
// Force dates to be serialized as ISO 8601 UTC, undoing this:
116+
// https://github.com/snowflakedb/snowflake-connector-nodejs/blob/a9174fb7/lib/connection/result/sf_timestamp.js#L177-L179
117+
export function replacer(this: {[key: string]: unknown}, key: string, value: unknown): unknown {
118+
const v = this[key];
119+
return v instanceof Date ? Date.prototype.toJSON.call(v) : value;
120+
}

0 commit comments

Comments
 (0)