Skip to content

Commit d785ecd

Browse files
authored
fix: additions for prisma 6.17 and engineType = "client" compat (#39)
1 parent 7ba03d3 commit d785ecd

4 files changed

Lines changed: 114 additions & 13 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"license": "Apache-2.0",
3535
"sideEffects": false,
3636
"dependencies": {
37-
"@prisma/driver-adapter-utils": "6.12.0"
37+
"@prisma/driver-adapter-utils": "6.17.0"
3838
},
3939
"devDependencies": {
4040
"@tidbcloud/serverless": "^0.1.0",

pnpm-lock.yaml

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/conversion.ts

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { ColumnTypeEnum, type ColumnType } from "@prisma/driver-adapter-utils";
1+
import {
2+
ArgType,
3+
ColumnTypeEnum,
4+
type ColumnType,
5+
} from "@prisma/driver-adapter-utils";
26

37
export type TiDBCloudColumnType =
48
| "NULL"
@@ -117,3 +121,90 @@ function hexToUint8Array(hexString: string): Uint8Array {
117121
}
118122
return uint8Array;
119123
}
124+
125+
export function mapArg<A>(
126+
arg: A | Date,
127+
argType: ArgType
128+
): null | BigInt | string | Uint8Array | A {
129+
if (arg === null) {
130+
return null;
131+
}
132+
133+
if (typeof arg === "string" && argType.scalarType === "bigint") {
134+
return BigInt(arg);
135+
}
136+
137+
if (typeof arg === "string" && argType.scalarType === "datetime") {
138+
arg = new Date(arg);
139+
}
140+
141+
if (arg instanceof Date) {
142+
switch (argType.dbType) {
143+
case "TIME":
144+
case "TIME2":
145+
return formatTime(arg);
146+
case "DATE":
147+
case "NEWDATE":
148+
return formatDate(arg);
149+
default:
150+
return formatDateTime(arg);
151+
}
152+
}
153+
154+
if (typeof arg === "string" && argType.scalarType === "bytes") {
155+
return Buffer.from(arg, "base64");
156+
}
157+
158+
if (Array.isArray(arg) && argType.scalarType === "bytes") {
159+
return Buffer.from(arg);
160+
}
161+
162+
if (ArrayBuffer.isView(arg)) {
163+
return Buffer.from(arg.buffer, arg.byteOffset, arg.byteLength);
164+
}
165+
166+
return arg;
167+
}
168+
169+
function formatDateTime(date: Date): string {
170+
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
171+
const ms = date.getUTCMilliseconds();
172+
return (
173+
date.getUTCFullYear() +
174+
"-" +
175+
pad(date.getUTCMonth() + 1) +
176+
"-" +
177+
pad(date.getUTCDate()) +
178+
" " +
179+
pad(date.getUTCHours()) +
180+
":" +
181+
pad(date.getUTCMinutes()) +
182+
":" +
183+
pad(date.getUTCSeconds()) +
184+
(ms ? "." + String(ms).padStart(3, "0") : "")
185+
);
186+
}
187+
188+
function formatDate(date: Date): string {
189+
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
190+
return (
191+
date.getUTCFullYear() +
192+
"-" +
193+
pad(date.getUTCMonth() + 1) +
194+
"-" +
195+
pad(date.getUTCDate())
196+
);
197+
}
198+
199+
function formatTime(date: Date): string {
200+
const pad = (n: number, z = 2) => String(n).padStart(z, "0");
201+
const ms = date.getUTCMilliseconds();
202+
return (
203+
pad(date.getUTCHours()) +
204+
":" +
205+
pad(date.getUTCMinutes()) +
206+
":" +
207+
pad(date.getUTCSeconds()) +
208+
(ms ? "." + String(ms).padStart(3, "0") : "")
209+
);
210+
}

src/tidbcloud.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type TiDBCloudColumnType,
1616
fieldToColumnType,
1717
customDecoder,
18+
mapArg,
1819
} from "./conversion";
1920
import { name as packageName } from "../package.json";
2021

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

9192
try {
92-
const result = await this.client.execute(sql, values, {
93-
arrayMode: true,
94-
fullResult: true,
95-
decoders: customDecoder,
96-
});
93+
const result = await this.client.execute(
94+
sql,
95+
values.map((val, i) => mapArg(val, query.argTypes[i])),
96+
{
97+
arrayMode: true,
98+
fullResult: true,
99+
decoders: customDecoder,
100+
}
101+
);
97102
return result as TiDBCloud.FullResult;
98103
} catch (e) {
99104
const error = e as Error;

0 commit comments

Comments
 (0)