To count all rows in table you can use count() function or sql operator like below:
index.ts
schema.ts
To count rows where the specified column contains non-NULL values you can use count() function with a column:
Drizzle has simple and flexible API, which lets you create your custom solutions. In PostgreSQL and MySQL count() function returns bigint, which is interpreted as string by their drivers, so it should be casted to integer:
In SQLite, count() result returns as integer.
IMPORTANT
By specifying sql<number>, you are telling Drizzle that the expected type of the field is number.
If you specify it incorrectly (e.g. use sql<string> for a field that will be returned as a number), the runtime value won’t match the expected type.
Drizzle cannot perform any type casts based on the provided type generic, because that information is not available at runtime.
If you need to apply runtime transformations to the returned value, you can use the .mapWith() method.
To count rows that match a condition you can use .where() method:
This is how you can use count() function with joins and aggregations: