Skip to content
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

[BUG]: incorrect types for execute with pglite #3975

Open
1 task done
jamesarosen opened this issue Jan 20, 2025 · 4 comments
Open
1 task done

[BUG]: incorrect types for execute with pglite #3975

jamesarosen opened this issue Jan 20, 2025 · 4 comments
Labels
bug Something isn't working

Comments

@jamesarosen
Copy link

jamesarosen commented Jan 20, 2025

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.38.2

What version of drizzle-kit are you using?

0.30.1

Other packages

@electric-sql/[email protected],@types/[email protected],[email protected]

Describe the Bug

When using

const result = db.execute(sql`select 8 as a_number`)

the return type is declared as postgres.RowList<{ a_number: number; }[]>.

But when using pglite, the actual result is

Object {
  "affectedRows": 0,
  "fields": Array [
    Object {
      "dataTypeID": 21,
      "name": "a_number",
    },
  ],
  "rows": Array [
    Object {
      "a_number": 8,
    },
  ],
}

I'm not yet sure where this bug exists. It may be in pglite or @types/pg.

Here's how we're setting up pglite with Drizzle per these docs:

// test-db.ts

import { PGlite } from '@electric-sql/pglite';
import { drizzle } from 'drizzle-orm/pglite';

const client = new PGlite({
  extensions: { uuid_ossp },
  debug: 0,
  database: 'testdb',
  username: 'postgres',
});

export const db = drizzle(client); // also tried drizzle({ client })
@jamesarosen jamesarosen added the bug Something isn't working label Jan 20, 2025
@jamesarosen
Copy link
Author

jamesarosen commented Jan 20, 2025

I also tried this:

const result = db.select({
  a_number: sql<number>`8`
})

but that caused the query to hang indefinitely, with no additional debug output even with debug: 5, possibly because there's no .from.

@jamesarosen
Copy link
Author

jamesarosen commented Jan 22, 2025

This may be because of the type incompatibilities between pg (aka node-postgres) and postgres.js.

@jamesarosen
Copy link
Author

Here's a workaround for now:

const client = await PGlite.create(...);
const db = drizzle({ client });

// Workaround for https://github.com/drizzle-team/drizzle-orm/issues/3975
const originalExecute = db.execute;
// @ts-expect-error drizzle has incorrect types for pglite
db.execute = async function executeUnwrappingRows(...args) {
  const result = await originalExecute.apply(this, args);
  return result.rows;
};

@castarco
Copy link

castarco commented Feb 1, 2025

The types seem to be wrong for better-sqlite3 as well. The execute function appears as not available in the db instance type, but there are plenty of examples in Drizzle documentation showing the call db.execute('select 1'); as an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants