drizzle-team / drizzle-team/drizzle-orm

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

Open
#2,107 4 comments 1 reaction 0 assignees View on GitHub
improvement priority rqb
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### 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):
```typescript
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()](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/query-builders/query.ts#L50) (see the [return type for findMany()](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/query-builders/query.ts#L34) for contrast), and as [documented for findFirst()](https://orm.drizzle.team/docs/rqb#find-first) ([documentation for findMany()](https://orm.drizzle.team/docs/rqb#find-many) for constast).

### Environment & setup

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.