Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"license": "Apache-2.0",
"sideEffects": false,
"dependencies": {
"@prisma/driver-adapter-utils": "6.12.0"
"@prisma/driver-adapter-utils": "6.17.0"
},
"devDependencies": {
"@tidbcloud/serverless": "^0.1.0",
Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 92 additions & 1 deletion src/conversion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { ColumnTypeEnum, type ColumnType } from "@prisma/driver-adapter-utils";
import {
ArgType,
ColumnTypeEnum,
type ColumnType,
} from "@prisma/driver-adapter-utils";

export type TiDBCloudColumnType =
| "NULL"
Expand Down Expand Up @@ -117,3 +121,90 @@ function hexToUint8Array(hexString: string): Uint8Array {
}
return uint8Array;
}

export function mapArg<A>(
arg: A | Date,
argType: ArgType
): null | BigInt | string | Uint8Array | A {
Comment thread
jacek-prisma marked this conversation as resolved.
if (arg === null) {
return null;
}

if (typeof arg === "string" && argType.scalarType === "bigint") {
return BigInt(arg);
}

if (typeof arg === "string" && argType.scalarType === "datetime") {
arg = new Date(arg);
}

if (arg instanceof Date) {
switch (argType.dbType) {
case "TIME":
case "TIME2":
return formatTime(arg);
case "DATE":
case "NEWDATE":
return formatDate(arg);
default:
return formatDateTime(arg);
Comment thread
jacek-prisma marked this conversation as resolved.
}
}

if (typeof arg === "string" && argType.scalarType === "bytes") {
return Buffer.from(arg, "base64");
}

if (Array.isArray(arg) && argType.scalarType === "bytes") {
return Buffer.from(arg);
}

if (ArrayBuffer.isView(arg)) {
return Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength);
}

return arg;
}

function formatDateTime(date: Date): string {
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
const ms = date.getUTCMilliseconds();
return (
date.getUTCFullYear() +
"-" +
pad(date.getUTCMonth() + 1) +
"-" +
pad(date.getUTCDate()) +
" " +
pad(date.getUTCHours()) +
":" +
pad(date.getUTCMinutes()) +
":" +
pad(date.getUTCSeconds()) +
(ms ? "." + String(ms).padStart(3, "0") : "")
);
}

function formatDate(date: Date): string {
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
return (
date.getUTCFullYear() +
"-" +
pad(date.getUTCMonth() + 1) +
"-" +
pad(date.getUTCDate())
);
}

function formatTime(date: Date): string {
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
const ms = date.getUTCMilliseconds();
return (
pad(date.getUTCHours()) +
":" +
pad(date.getUTCMinutes()) +
":" +
pad(date.getUTCSeconds()) +
(ms ? "." + String(ms).padStart(3, "0") : "")
);
}
15 changes: 10 additions & 5 deletions src/tidbcloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
type TiDBCloudColumnType,
fieldToColumnType,
customDecoder,
mapArg,
} from "./conversion";
import { name as packageName } from "../package.json";

Expand Down Expand Up @@ -89,11 +90,15 @@ class TiDBCloudQueryable<ClientT extends TiDBCloud.Connection | TiDBCloud.Tx>
const { sql, args: values } = query;

try {
const result = await this.client.execute(sql, values, {
arrayMode: true,
fullResult: true,
decoders: customDecoder,
});
const result = await this.client.execute(
sql,
values.map((val, i) => mapArg(val, query.argTypes[i])),
{
arrayMode: true,
fullResult: true,
decoders: customDecoder,
}
);
return result as TiDBCloud.FullResult;
} catch (e) {
const error = e as Error;
Expand Down
Loading