Skip to content

Commit

Permalink
return number | string from mysql execlastid
Browse files Browse the repository at this point in the history
  • Loading branch information
yshrsmz committed Mar 4, 2024
1 parent 1a42ab0 commit 402b9f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/node-mysql2/src/db/query_sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface CreateAuthorReturnIdArgs {
bio: string | null;
}

export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number> {
export async function createAuthorReturnId(client: Client, args: CreateAuthorReturnIdArgs): Promise<number | string> {
const [result] = await client.query<ResultSetHeader>({
sql: createAuthorReturnIdQuery,
values: [args.name, args.bio]
Expand Down
20 changes: 14 additions & 6 deletions src/drivers/mysql2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ function funcParamsDecl(iface: string | undefined, params: Parameter[]) {
}

export class Driver {
private readonly options: Mysql2Options
private readonly options: Mysql2Options;

constructor(options?: Mysql2Options) {
this.options = options ?? {}
this.options = options ?? {};
}

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

if (this.options.support_big_numbers) {
if (this.options.big_number_strings) {
typ = factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
typ = factory.createKeywordTypeNode(SyntaxKind.StringKeyword);
} else {
typ = factory.createUnionTypeNode([
factory.createKeywordTypeNode(SyntaxKind.NumberKeyword),
factory.createKeywordTypeNode(SyntaxKind.StringKeyword)
])
factory.createKeywordTypeNode(SyntaxKind.StringKeyword),
]);
}
}

Expand Down Expand Up @@ -655,6 +655,14 @@ export class Driver {
) {
const funcParams = funcParamsDecl(argIface, params);

let returnType: TypeNode = factory.createTypeReferenceNode("number", undefined);
if (this.options.support_big_numbers) {
returnType = factory.createUnionTypeNode([
factory.createTypeReferenceNode("number", undefined),
factory.createTypeReferenceNode("string", undefined),
]);
}

return factory.createFunctionDeclaration(
[
factory.createToken(SyntaxKind.ExportKeyword),
Expand All @@ -665,7 +673,7 @@ export class Driver {
undefined,
funcParams,
factory.createTypeReferenceNode(factory.createIdentifier("Promise"), [
factory.createTypeReferenceNode("number", undefined),
returnType,
]),
factory.createBlock(
[
Expand Down

0 comments on commit 402b9f3

Please sign in to comment.