DT
Drizzle TeamKing of the Short Bus

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:
const dealer = await db.query
.dealer
.findFirst({
columns: {id: true},
where: eq(dealerDef.key, dealerKey)
});
console.debug(`!!!!!!!!!!!!! ${JSON.stringify(dealer)}`)
const dealer = await db.query
.dealer
.findFirst({
columns: {id: true},
where: eq(dealerDef.key, dealerKey)
});
console.debug(`!!!!!!!!!!!!! ${JSON.stringify(dealer)}`)
Log 1 (entity found):
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}]
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}]
Log 2 (entity not found):
Query: select "id" from "dealer" where "dealer"."key" = :1 limit :2 -- params: [{"name":"1","value":{"stringValue":"bad-key"}}, {"name":"2","value":{"longValue":1}}]
!!!!!!!!!!!!! []
Query: select "id" from "dealer" where "dealer"."key" = :1 limit :2 -- params: [{"name":"1","value":{"stringValue":"bad-key"}}, {"name":"2","value":{"longValue":1}}]
!!!!!!!!!!!!! []
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?
KOT
King of the Short Bus13d ago
@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 array
S
Sillvva13d ago
No. You're right. I was thinking of something else.