findFirst returns array instead of entity - using aws-data-api/pg
Hi folks -- I'm using
aws-data-api/pg
, running the latest version ("drizzle-orm": "^0.30.7"
).
I'm seeing something where my query.<table>.findOne()
call is correctly typed as <entity>|undefined
, and the LIMIT 1
clause is showing up in the SQL, but I'm actually getting back results of the form [<entity>]
or []
, as I would expect from findMany()
.
Code:
Log 1 (entity found):
Log 2 (entity not found):
Changing the code to findMany()
changes the type expected by my IDE, but the logs show the same output (same SQL minus the LIMIT 1
, and exact same resulting data).
Am I doing something wrong here, or should I file a bug?2 Replies
@Sillvva Hmm, if that's the case, then the typing for
findFirst()
is incorrect.
* The return type for findMany()
is PgRelationalQuery<BuildQueryResult<TSchema, TFields, TConfig>[]>
* The return type for findFirst()
is PgRelationalQuery<BuildQueryResult<TSchema, TFields, TSelection> | undefined>
See https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/query-builders/query.ts#L34 and https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/query-builders/query.ts#L50 to confirm.
The documentation for https://orm.drizzle.team/docs/rqb#find-many and https://orm.drizzle.team/docs/rqb#find-first also explicitly indicates that findFirst()
should return a single entity, not an arrayNo. You're right. I was thinking of something else.