drizzle-team / drizzle-team/drizzle-orm
[BUG]: `findFirst()` returns array instead of single entity
- 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
Assessment
This issue has not been assessed yet.