Distinct (generated API)¶
Lists the distinct values of a column, or the distinct combinations of several, sorted the way you ask.
| Method | GET |
| URL | /api/gen/admin/mysql8/inventory/customers/distinct/:field[/:order] |
| Body | none |
| Query params | find |
| Answer | data : an array of objects, one per distinct value or combination |
| Databases | MongoDB, MySQL, MariaDB, PostgreSQL, SQL Server, Oracle, TiDB, Percona |
| Cached | yes, with enableCaching on the table |
| From code | g.sys.db.gen.distinctGen |
| API id | GEN_GET_DISTINCT (groups, settings, hooks, WebSocket subscriptions) |
| The other family | Distinct as a schema API |
Schemaless : the data passes as it is
The generated APIs never read the schema of the table. Nothing is converted, validated, defaulted, encrypted or numbered : what you send is what the database gets, and a filter matches the type you send ("22" does not match the number 22). On MongoDB, strings which look like ObjectIds are converted. Every table has these APIs, with or without a schema ; the same operations under /api/schema apply the schema.
The URL¶
/api/gen/admin/mysql8/inventory/customers/distinct/:field[/:order] : replace admin with the user path of your account, mysql8 with your instance, inventory with the database and customers with the table. The examples use this table :
| customer_id | first_name | last_name | last_update | pincode | isActive |
|---|---|---|---|---|---|
| 1 | Bob | lin | 2022-11-14 04: 34: 58 | 382345 | 1 |
| 2 | Alice | Page | 2022-10-15 02: 10: 40 | 382346 | 1 |
| 3 | Mallory | Brown | 2022-09-13 03: 44: 05 | 382347 | 1 |
| 4 | Eve | Mathly | 2022-11-12 01: 59: 33 | 382348 | 1 |
| 5 | Eve | Page | 2022-11-12 01: 59: 33 | 382349 | 1 |
One column¶
{ "success": true, "statusCode": 200, "data": [ { "pincode": 382345 }, { "pincode": 382346 }, { "pincode": 382347 } ] }
- Ascending by default. Descending :
/api/gen/admin/mysql8/inventory/customers/distinct/pincode/desc(also-1,dsc,0,false; ascending isasc,1,true).
Several columns¶
GET /api/gen/admin/mysql8/inventory/customers/distinct/first_name,pincode
GET /api/gen/admin/mysql8/inventory/customers/distinct/first_name,pincode/asc,desc
{ "success": true, "statusCode": 200, "data": [ { "first_name": "Alice", "pincode": 382346 }, { "first_name": "Bob", "pincode": 382345 }, { "first_name": "Eve", "pincode": 382349 }, { "first_name": "Eve", "pincode": 382348 } ] }
- One order per column, in the same position. A missing order is ascending.
With a filter¶
- The same
findas get all. For a filter in the body, use distinct with query.
Good to know¶
- A column which is empty everywhere gives
[]. - Field permissions apply : a column the group can not read can not be listed.
From your code¶
g.sys.db.gen.distinctGen makes the same call from custom APIs, hooks, events, schedulers and test cases, with the same headers and params. Pass true as the second argument to get the whole response envelope instead of data.
Headers¶
Every call takes the request headers : the tokens, x-am-response-case, x-am-content-type-response (JSON, XML, YAML…), x-am-response-object-type: make_flat, x-am-cache-control, x-am-get-encrypted-data, x-am-internationalization, x-am-tenant-username, x-am-meta.
Errors¶
| Code | When |
|---|---|
401 |
The token in x-am-authorization is missing or invalid. |
403 |
No group of the API user grants this API of this table, or a field of the request. |
404 |
The instance, database or table of the URL does not exist. |
400 |
The query or the body is wrong : the error messages say which key and why. |