core
core copied to clipboard
Add Count functions in database
Sometimes, it's useful to have a count of how many records there is in a collection with or without filters.
Adding a Count functions in all 3 database engine supported would involves the following:
- Adding the function in the
Persisterinterface in thedatabasepackage - Implement it for the
postgresql,mongo, andmemorydatabase engine - Add an endpoint in the backend API
Depending if you write your tests first or not, but having a tests in the main package as well as inside the different db engine ones would be nice.
Since the return type would be int64, error there's no need to implement a generic function in the backend's Database[T] functions set.
I'd see the following function prototype:
// Count returns the numbers of entries in a collection based on optional filters
Count(auth model.Auth, dbName, col string, filters map[string]interface{})` (int64, error)
The filters would be optional. See how the UpdateMany function also uses similar filters
For the endpoint, we could have a new route in server.go around the routes starting with /db and have a new one like /db/count/ which would call an handler defined in db.go that would accept POST request with the optional filters (look in db.go for example of the UpdateMany).
p.s. the typical database security apply, so the standard permission checks would happen before the optional filters