Skip to content

Conversation

Chriztiaan
Copy link
Contributor

@Chriztiaan Chriztiaan commented Jan 28, 2025

Description

At the moment duplicate column names in a query result are overwritten with only the last column being the one returned. This causes issues with join queries (a simple case is where multiple id columns are returned) and is most prominent when doing joins with one of the ORM drivers (Kysely/Drizzle). This issue is covered by the newly added tests.

This PR introduces executeRaw which processes the sqlite results differently and allows the Drizzle ORM driver to correctly map the resulting columns.

An existing approach for Drizzle-orm packages can be seen here:
https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/op-sqlite/session.ts#L165
https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/better-sqlite3/session.ts#L156

Sqlite Adapters Affected

  1. RNQS
    Opted to not implement executeRaw in the RNQS package for now, the adapter falls back to execute - resulting in the initial behaviour. Users who need this functionality are encouraged to migrate to OPSQLite at the moment.
  2. OPSQLite
    Using OPSQLite executeRaw function, which is also used for their Drizzle integration.
  3. Web
    Updated WASQLiteConnection to expose a raw execute function, simply handling the result differently.
  4. Node
    Using BetterSqlite3's raw() - which is also used for the BSQLite3 Drizzle integration.

Original Workaround

For Drizzle users using joins the following example shows how to side-step the problem in the interim:

wait db
      .select({ id: drizzleUsers.id, postsId: sql`"posts"."id"`.as('postsId') }) // or postsId: drizzlePosts.id.getSQL().as('postsId')
      .from(drizzleUsers)
      .leftJoin(drizzlePosts, eq(drizzleUsers.id, drizzlePosts.user_id));

Reported in #473

Copy link

changeset-bot bot commented Jan 28, 2025

🦋 Changeset detected

Latest commit: dbe68fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@powersync/react-native Minor
@powersync/drizzle-driver Minor
@powersync/op-sqlite Minor
@powersync/common Minor
@powersync/node Minor
@powersync/web Minor
@powersync/tanstack-react-query Patch
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Chriztiaan Chriztiaan marked this pull request as ready for review March 20, 2025 06:45
@Chriztiaan Chriztiaan merged commit f8fd814 into main Mar 26, 2025
6 checks passed
@Chriztiaan Chriztiaan deleted the feat/execute-raw branch March 26, 2025 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants