Skip to content

Commit b64b132

Browse files
committed
return number | string from mysql execlastid
1 parent 54496e9 commit b64b132

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

examples/node-mysql2/src/db/query_sql.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export interface CreateAuthorReturnIdArgs {
9191
bio: string | null;
9292
}
9393

94-
export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number> {
94+
export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number | string> {
9595
const [result] = await client.query<ResultSetHeader>({
9696
sql: createAuthorReturnIdQuery,
9797
values: [args.name, args.bio]

src/drivers/mysql2.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ function funcParamsDecl(iface: string | undefined, params: Parameter[]) {
4545
}
4646

4747
export class Driver {
48-
private readonly options: Mysql2Options
48+
private readonly options: Mysql2Options;
4949

5050
constructor(options?: Mysql2Options) {
51-
this.options = options ?? {}
51+
this.options = options ?? {};
5252
}
5353

5454
columnType(column?: Column): TypeNode {
@@ -63,12 +63,12 @@ export class Driver {
6363

6464
if (this.options.support_big_numbers) {
6565
if (this.options.big_number_strings) {
66-
typ = factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
66+
typ = factory.createKeywordTypeNode(SyntaxKind.StringKeyword);
6767
} else {
6868
typ = factory.createUnionTypeNode([
6969
factory.createKeywordTypeNode(SyntaxKind.NumberKeyword),
70-
factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
71-
])
70+
factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
71+
]);
7272
}
7373
}
7474

@@ -655,6 +655,14 @@ export class Driver {
655655
) {
656656
const funcParams = funcParamsDecl(argIface, params);
657657

658+
let returnType: TypeNode = factory.createTypeReferenceNode("number", undefined);
659+
if (this.options.support_big_numbers) {
660+
returnType = factory.createUnionTypeNode([
661+
factory.createTypeReferenceNode("number", undefined),
662+
factory.createTypeReferenceNode("string", undefined),
663+
]);
664+
}
665+
658666
return factory.createFunctionDeclaration(
659667
[
660668
factory.createToken(SyntaxKind.ExportKeyword),
@@ -665,7 +673,7 @@ export class Driver {
665673
undefined,
666674
funcParams,
667675
factory.createTypeReferenceNode(factory.createIdentifier("Promise"), [
668-
factory.createTypeReferenceNode("number", undefined),
676+
returnType,
669677
]),
670678
factory.createBlock(
671679
[

0 commit comments

Comments
 (0)