Skip to content

[BUG]: Column name conversion not working when using sql.js #3642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
rockmagma02 opened this issue Nov 28, 2024 · 1 comment · May be fixed by #3643 or #4403
Open
1 task done

[BUG]: Column name conversion not working when using sql.js #3642

rockmagma02 opened this issue Nov 28, 2024 · 1 comment · May be fixed by #3643 or #4403
Labels
bug Something isn't working priority Will be worked on next rqb relational queries

Comments

@rockmagma02
Copy link

rockmagma02 commented Nov 28, 2024

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.4

What version of drizzle-kit are you using?

0.28.1

Other packages

No response

Describe the Bug

Column name conversion not working when using sql.js

Description

Here is a simple example can show this bug:

import initSqlJs from "sql.js";
import fs from "node:fs";
import { drizzle } from "drizzle-orm/sql-js";
import { numeric, sqliteTable, text } from "drizzle-orm/sqlite-core";

const testTable = sqliteTable("test", {
	id: numeric().primaryKey().notNull(),
	testSnakeName: text("test_snake_name").notNull(),
});

async function main() {
	const SQL = await initSqlJs();
        // // this a simple sqlite database, which is used to demonstrate this bug
	const dbFile = fs.readFileSync("./test.sqlite");
	const dbOrigin = new SQL.Database(dbFile);
	const db = drizzle(dbOrigin, {
		schema: { testTable },
		logger: true,
	});
	const result = await db.query.testTable.findFirst();
	console.log("result:", result);
	console.log("property:", result?.testSnakeName);
}

await main();

As you can observe, the table contains a column named test_snake_name. We utilize the testSnakeName to refer to this column in the Drizzle schema. However, there is an issue:

Query: select "id", "test_snake_name" from "test" "testTable" limit ? -- params: [1]
result: { id: 1, test_snake_name: 'test' }
property: undefined

The query was successfully executed, and the result was retrieved. However, the retrieved data was not converted to the appropriate object type suitable for use in TypeScript or JavaScript code. Instead, it retains the name stored in the database.

Other info

I discovered similar (or identical) issues in #2189, which was opened on April 22, 2024. However, due to the high volume of issues, it appears that your team may have overlooked this matter. Therefore, I am re-reporting it here for your attention.

@rockmagma02 rockmagma02 added the bug Something isn't working label Nov 28, 2024
@rockmagma02
Copy link
Author

rockmagma02 commented Nov 28, 2024

Incidentally, I believe I have identified the source of the issue.

prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
isResponseInArrayMode: boolean,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query, this.logger, fields, executeMethod, isResponseInArrayMode);
}
override prepareOneTimeQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
isResponseInArrayMode: boolean,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(
stmt,
query,
this.logger,
fields,
executeMethod,
isResponseInArrayMode,
customResultMapper,
true,
);
}

It is evident that the prepareQuery method does not accept a customResultMapper, which is essential for converting column names.

I am passionate to open a new Pull Request to fix this issue.

rockmagma02 added a commit to rockmagma02/drizzle-orm that referenced this issue Nov 28, 2024
rockmagma02 added a commit to rockmagma02/drizzle-orm that referenced this issue Nov 28, 2024
rockmagma02 added a commit to rockmagma02/drizzle-orm that referenced this issue Nov 28, 2024
rockmagma02 added a commit to rockmagma02/drizzle-orm that referenced this issue Nov 28, 2024
@L-Mario564 L-Mario564 added rqb relational queries priority Will be worked on next labels Nov 28, 2024
rockmagma02 added a commit to rockmagma02/drizzle-orm that referenced this issue Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority Will be worked on next rqb relational queries
Projects
None yet
2 participants