drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
22.1k stars 512 forks source link

[BUG]: `findFirst()` returns array instead of single entity #2107

Open zacronos opened 3 months ago

zacronos commented 3 months ago

What version of drizzle-orm are you using?

0.30.7

What version of drizzle-kit are you using?

--

Describe the Bug

The findFirst() query method returns an array.

Example code (I assume the table definition is irrelevant here):

  const dealer = await db.query
    .dealer
    .findFirst({
      columns: {id: true},
      where: eq(dealerDef.key, dealerKey)
    });
  console.debug(`!!!!!!!!!!!!! ${JSON.stringify(dealer)}`)

With logging turned on, a query that finds an entity shows:

Query: select "id" from "dealer" where "dealer"."key" = :1 limit :2 -- params: [{"name":"1","value":{"stringValue":"valid-key"}}, {"name":"2","value":{"longValue":1}}]
!!!!!!!!!!!!! [{"id":100001}]

A query that does not find an entity shows:

Query: select "id" from "dealer" where "dealer"."key" = :1 limit :2 -- params: [{"name":"1","value":{"stringValue":"bad-key"}}, {"name":"2","value":{"longValue":1}}]
!!!!!!!!!!!!! []

Expected behavior

I expect to get an entity (in the "found" case) or undefined (in the "not found case"), as indicated in the return type for findFirst() (see the return type for findMany() for contrast), and as documented for findFirst() (documentation for findMany() for constast).

Environment & setup

I'm using aws-data-api/pg, not sure if the bug is specific to that client/db

marceloverdijk commented 4 weeks ago

With drizzle-orm/d1 the object (entity) or undefined is returned, so doing what you exepcted.

I came here as was wondering if it's possible to return null instead of undefined or whether it is possible to extend drizzle so I can add a findFirstOrNull method?