drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Allow bigint mode to be set for `$count()`

Open
#3,557 0 comments 0 reactions 0 assignees View on GitHub
enhancement qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [X] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

Drizzle does support a configurable mode for bigint column values in table schemas, which is great.

https://orm.drizzle.team/docs/column-types/pg#bigint

But if I use the `db.$count()` method, the result is always a string, never a number.

I was trying to return a few counts of related table by specifying a few extra columns
```
function fieldUsage(fieldId: number) {
return db.query.fieldsTable.findFirst({
columns: { id: true },
extras: {
ideaCount: db.$count(ideasTable, eq(ideasTable.fieldId, fieldId)).as('idea_count'),
customerCount: db.$count(customersTable, eq(customersTable.fieldId, fieldId)).as('customer_count'),
},
where: eq(fieldsTable.id, fieldId),
};
}
```

The inferred return type of `fieldUsage` is:
```
{
id: number;
ideaCount: number;
customerCount: number;
} | undefined
```

But at run time, the results from drizzle are actually strings for the $count() results:
```
{
id: number;
ideaCount: string;
customerCount: string;
} | undefined
```

It would be great if `$count()` could be extended with an optional generic type to set its bigint mode.

`ideaCount: db.$count(ideasTable, eq(ideasTable.fieldId, fieldId)).as('idea_count'),`

Maybe even make `number` the default bigint mode for `$count()` since most DBs won't exceed 2^53 rows.

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.